Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a list of objects #68

Closed
lisavd opened this issue Sep 24, 2018 · 4 comments
Closed

Adding a list of objects #68

lisavd opened this issue Sep 24, 2018 · 4 comments

Comments

@lisavd
Copy link

lisavd commented Sep 24, 2018

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
Copy link
Owner

eko commented Sep 24, 2018

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:

protected function formatGroupItemField(GroupItemField $field, ItemInterface $item)
{
$name = $field->getName();
$element = $this->dom->createElement($name);
$this->addAttributes($element, $field, $item);
$itemFields = $field->getItemFields();
foreach ($itemFields as $itemField) {
$class = get_class($itemField);
switch ($class) {
case 'Eko\FeedBundle\Field\Item\MediaItemField':
$itemElements = $this->formatMediaItemField($itemField, $item);
break;
case 'Eko\FeedBundle\Field\Item\ItemField':
$itemElements = $this->formatItemField($itemField, $item);
break;
}
foreach ($itemElements as $itemElement) {
$element->appendChild($itemElement);
}
}
return [$element];
}
).

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
Copy link
Author

lisavd commented Sep 25, 2018

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
Copy link
Owner

eko commented Sep 25, 2018

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 added a commit that referenced this issue Oct 1, 2018
Signed-off-by: Vincent Composieux <vincent@composieux.fr>
@eko eko closed this as completed in df8212d Oct 1, 2018
eko added a commit that referenced this issue Oct 1, 2018
Added ability to create nested group item fields (fixes #68)
@eko
Copy link
Owner

eko commented Oct 1, 2018

As discussed, nested GroupItemFields are now implemented :)

Thank you for your feedbacks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants