eko / FeedBundle

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

add support of enclosure/media #18

Closed gido closed 11 years ago

gido commented 11 years ago

I know, this can be a bit tricky but I would love to have support of enclosure to add item image.

RSS 2.0

<enclosure 
         type="image/jpeg"
         url="http://www.example.org/myimage.jpg"
         length="1234" />

Atom ?

<link rel="enclosure" 
          type="image/jpeg"
          title="My nice picture"
          href="http://www.example.org/myimage.jpg"
          length="1234" />

I looked at the Field class and see that is not possible "Out of the box". Did you have any idea or advice on how to implement this ?

eko commented 11 years ago

Hi, I'm sure it's possible to do, I will have a look to find the better solution during this week-end.

eko commented 11 years ago

Hi @gido,

Support for enclosure is now added. Media enclosure can be added using the MediaItemField field type as below:

<?php
$feed = $this->get('eko_feed.feed.manager')->get('article');
$feed->add(new FakeEntity());
$feed->addItemField(new MediaItemField('getFeedMediaItem'));

The getFeedMediaItem() method must return an array with the following keys: type, length & value:

/**
 * Returns a custom media field
 *
 * @return string
 */
public function getFeedMediaItem()
{
    return array(
        'type'   => 'image/jpeg',
        'length' => 500,
        'value'  => 'http://website.com/image.jpg'
    );
}

This media items can also be grouped using GroupItemField:

<?php
$feed = $this->get('eko_feed.feed.manager')->get('article');
$feed->add(new FakeEntity());
$feed->addItemField(
    new GroupItemField('images', new MediaItemField('images', 'getFeedMediaMultipleItems'))
);

and an array returning multiple medias in your entity, like that:

/**
 * Returns a fake custom multiple media fields
 *
 * @return string
 */
public function getFeedMediaMultipleItems()
{
    return array(
        array(
            'type'   => 'image/jpeg',
            'length' => 500,
            'value'  => 'http://website.com/image.jpg'
        ),
        array(
            'type'   => 'image/png',
            'length' => 600,
            'value'  => 'http://website.com/image2.png'
        )
    );
}

This will generate the following XML:

<images>
    <link rel="enclosure" href="http://website.com/image.jpg" type="image/jpeg" length="500"/>
    <link rel="enclosure" href="http://website.com/image2.png" type="image/png" length="600"/>
</images>

Hope it can help you.

gido commented 11 years ago

Thanks! I will have a look

Le 8 sept. 2013 à 18:15, Vincent Composieux notifications@github.com a écrit :

Hi @gido,

Support for enclosure is now added. Media enclosure can be added using the MediaItemField field type as below:

<?php $feed = $this->get('eko_feed.feed.manager')->get('article'); $feed->add(new FakeEntity()); $feed->addItemField(new MediaItemField('getFeedMediaItem')); The getFeedMediaItem() method must return an array with the following keys: type, length & value:

/**

  • Returns a custom media field *
  • @return string */ public function getFeedMediaItem() { return array( 'type' => 'image/jpeg', 'length' => 500, 'value' => 'http://website.com/image.jpg' ); } This media items can also be grouped using GroupItemField:

<?php $feed = $this->get('eko_feed.feed.manager')->get('article'); $feed->add(new FakeEntity()); $feed->addItemField( new GroupItemField('images', new MediaItemField('images', 'getFeedMediaMultipleItems')) ); and an array returning multiple medias in your entity, like that:

/**

  • Returns a fake custom multiple media fields *
  • @return string */ public function getFeedMediaMultipleItems() { return array( array( 'type' => 'image/jpeg', 'length' => 500, 'value' => 'http://website.com/image.jpg' ), array( 'type' => 'image/png', 'length' => 600, 'value' => 'http://website.com/image2.png' ) ); } Hope it can help you.

— Reply to this email directly or view it on GitHub.

wodka commented 10 years ago

Hi, I think it is not fully fixed -> there might be entries within the result that have images while others have none.

Right now it is only supported (from what I can see) to have at least one image per entry

br.

eko commented 10 years ago

Hi @wodka,

Thank you for feedback, item fields can now render null to do not be displayed.

Indeed, in you do something like that:

<?php
$fakeEntityWithMedias = new FakeEntity();
$fakeEntityWithMedias->setFeedMediaItem(array(
    'type'   => 'image/jpeg',
    'length' => 500,
    'value'  => 'http://website.com/image.jpg'
));

$fakeEntityWithNoMedias = new FakeEntity();
$fakeEntityWithNoMedias->setFeedMediaItem(null);

$feed = $this->get('eko_feed.feed.manager')->get('article');
$feed->add($fakeEntityWithMedias);
$feed->add($fakeEntityWithNoMedias);
$feed->addItemField(
    new MediaItemField('getFeedMediaItem')
);

This will render the following XML:

    <item>
      <title><![CDATA[Fake title]]></title>
      <description><![CDATA[Fake description or content]]></description>
      <link>http://github.com/eko/FeedBundle/article/fake/url</link>
      <pubDate>Sat, 25 Jan 2014 18:50:08 +0100</pubDate>
      <enclosure url="http://website.com/image.jpg" type="image/jpeg" length="500"/>
    </item>
    <item>
      <title><![CDATA[Fake title]]></title>
      <description><![CDATA[Fake description or content]]></description>
      <link>http://github.com/eko/FeedBundle/article/fake/url</link>
      <pubDate>Sat, 25 Jan 2014 18:50:08 +0100</pubDate>
    </item>

So second entity which returns null will not have any media encapsulated.

Works with all item fields and groups too.