You are here

public function InstantArticleRssEncoder::encode in Facebook Instant Articles 3.x

Same name and namespace in other branches
  1. 8.2 src/Encoder/InstantArticleRssEncoder.php \Drupal\fb_instant_articles\Encoder\InstantArticleRssEncoder::encode()

Overrides XmlEncoder::encode

File

src/Encoder/InstantArticleRssEncoder.php, line 75

Class

InstantArticleRssEncoder
Facebook instant articles FBIA RSS encoder.

Namespace

Drupal\fb_instant_articles\Encoder

Code

public function encode($data, $format, array $context = []) {

  // Force $data into an array of numeric keys.
  if (!ctype_digit(implode('', array_keys($data)))) {
    $data = [
      $data,
    ];
  }

  // Render all each InstantArticle object.
  foreach ($data as $delta => $item) {
    if (!empty($item['content:encoded']) && $item['content:encoded'] instanceof InstantArticle) {
      $data[$delta]['content:encoded'] = $item['content:encoded']
        ->render();
    }
  }

  // Wrapping tags.
  $feed_title = $this
    ->t('Facebook Instant Articles RSS Feed');
  $feed_description = '';
  if (isset($context['views_style_plugin'])) {

    /** @var \Drupal\rest\Plugin\views\style\Serializer $style */
    $style = $context['views_style_plugin'];
    $feed_title = $style->view
      ->getTitle();
    $feed_description = $style->view->storage
      ->get('description');
  }
  $encoded = [
    '@version' => '2.0',
    '@xmlns:content' => 'http://purl.org/rss/1.0/modules/content/',
    'channel' => [
      'title' => $feed_title,
      'link' => $this
        ->getLink(),
      'lastBuildDate' => date('c', time()),
    ],
  ];
  if (!empty($feed_description)) {
    $encoded['channel']['description'] = $feed_description;
  }
  $encoded['channel']['item'] = $data;
  return parent::encode($encoded, $format, $context);
}