eko / FeedBundle

A Symfony bundle to build RSS feeds from your entities
http://vincent.composieux.fr
MIT License
141 stars 50 forks source link

Adding a list of objects #68

Closed lisavd closed 6 years ago

lisavd commented 6 years ago

Hi,

I have a news article rss feed.

One news item has an array of multiple links, containing a title + url. So I would like to add to my news item

<links> <link> <title>My title</title> <url>My Url<url> </link> ... </links>

In my action I do this: $news = $news_repo->getAllNews($locale); $feedManager = $this->get('eko_feed.feed.manager'); $feed = $feedManager->get('news'); $feed->addFromArray($news);

I can add extra ItemFields and MediaItemFields. But I don't know how I can add an array of multiple link objects containing title + url.

I have this in my entity:

   public function getLinkItems()
   {
    $items = array();

    foreach ($links as $link){
            $title = $link->getTitle();
            $url = $link->getUrl();

            $items[] =
               array(
                new ItemField('url', $url),
                new ItemField('title', $title),
            );
     }

    return $items;
}

And this in my action, but of course this is not correct: $feed->addItemField( new GroupItemField('links', 'getLinkItems') );

Is there a way to do this?

Thanks.

eko commented 6 years ago

Hello @lisavd,

Thank you for this interesting case. Indeed, it's not possible actually but you should be able to do the following:

public function getLinkItems()
{
    $items = array();

    foreach ($links as $link){
        $title = $link->getTitle();
        $url = $link->getUrl();

        $items[] = new GroupItemField('link', array(
            new ItemField('url', $url),
            new ItemField('title', $title)
        ));
    }

    return new GroupItemField('links', array($items));
}

The only issue actually is that a GroupItemField can only contain ItemField or a MediaItemField (see code here: https://github.com/eko/FeedBundle/blob/49388928bd2d6739c9e41cf3bf4736fb2408633c/Formatter/Formatter.php#L150-L178).

We should update the code in order to do a recursive call in case of a GroupItemField itself contains another GroupItemField.

Do you think you could work on this support? Elsewhere I can try to give it a look by the end of the week.

Thank you

lisavd commented 6 years ago

Hi,

I'm going to add inside the description tag. This will solve my issue in a better way.

I was making it to difficult when it is actually not :)

Thanks for the answer.

Lisa

eko commented 6 years ago

Great @lisavd, it's fine if you've found a neat solution :)

With pleasure, do not hesitate if you need help on another topics.

I am keeping this issue opened as I will have a look on how to fix the nested issue by the end of the week.

eko commented 6 years ago

As discussed, nested GroupItemFields are now implemented :)

Thank you for your feedbacks!