You are here

public function Rss::render in Drupal 9

Same name in this branch
  1. 9 core/modules/views/src/Plugin/views/style/Rss.php \Drupal\views\Plugin\views\style\Rss::render()
  2. 9 core/modules/node/src/Plugin/views/row/Rss.php \Drupal\node\Plugin\views\row\Rss::render()
  3. 9 core/modules/comment/src/Plugin/views/row/Rss.php \Drupal\comment\Plugin\views\row\Rss::render()
  4. 9 core/modules/aggregator/src/Plugin/views/row/Rss.php \Drupal\aggregator\Plugin\views\row\Rss::render()
Same name and namespace in other branches
  1. 8 core/modules/node/src/Plugin/views/row/Rss.php \Drupal\node\Plugin\views\row\Rss::render()

Render a row object. This usually passes through to a theme template of some form, but not always.

Parameters

object $row: A single row of the query result, so an element of $view->result.

Return value

string The rendered output of a single row, used by the style plugin.

Overrides RowPluginBase::render

File

core/modules/node/src/Plugin/views/row/Rss.php, line 74

Class

Rss
Plugin which performs a node_view on the resulting object and formats it as an RSS item.

Namespace

Drupal\node\Plugin\views\row

Code

public function render($row) {
  global $base_url;
  $nid = $row->{$this->field_alias};
  if (!is_numeric($nid)) {
    return;
  }
  $display_mode = $this->options['view_mode'];
  if ($display_mode == 'default') {
    $display_mode = \Drupal::config('system.rss')
      ->get('items.view_mode');
  }

  // Load the specified node:

  /** @var \Drupal\node\NodeInterface $node */
  $node = $this->nodes[$nid];
  if (empty($node)) {
    return;
  }
  $node->rss_namespaces = [];
  $node->rss_elements = [
    [
      'key' => 'pubDate',
      'value' => gmdate('r', $node
        ->getCreatedTime()),
    ],
    [
      'key' => 'dc:creator',
      'value' => $node
        ->getOwner()
        ->getDisplayName(),
    ],
    [
      'key' => 'guid',
      'value' => $node
        ->id() . ' at ' . $base_url,
      'attributes' => [
        'isPermaLink' => 'false',
      ],
    ],
  ];

  // The node gets built and modules add to or modify $node->rss_elements
  // and $node->rss_namespaces.
  $build_mode = $display_mode;
  $build = \Drupal::entityTypeManager()
    ->getViewBuilder('node')
    ->view($node, $build_mode);
  unset($build['#theme']);
  if (!empty($node->rss_namespaces)) {
    $this->view->style_plugin->namespaces = array_merge($this->view->style_plugin->namespaces, $node->rss_namespaces);
  }
  elseif (function_exists('rdf_get_namespaces')) {

    // Merge RDF namespaces in the XML namespaces in case they are used
    // further in the RSS content.
    $xml_rdf_namespaces = [];
    foreach (rdf_get_namespaces() as $prefix => $uri) {
      $xml_rdf_namespaces['xmlns:' . $prefix] = $uri;
    }
    $this->view->style_plugin->namespaces += $xml_rdf_namespaces;
  }
  $item = new \stdClass();
  if ($display_mode != 'title') {

    // We render node contents.
    $item->description = $build;
  }
  $item->title = $node
    ->label();
  $item->link = $node
    ->toUrl('canonical', [
    'absolute' => TRUE,
  ])
    ->toString();

  // Provide a reference so that the render call in
  // template_preprocess_views_view_row_rss() can still access it.
  $item->elements =& $node->rss_elements;
  $item->nid = $node
    ->id();
  $build = [
    '#theme' => $this
      ->themeFunctions(),
    '#view' => $this->view,
    '#options' => $this->options,
    '#row' => $item,
  ];
  return $build;
}