3 namespace Drupal\node\Plugin\views\row;
5 use Drupal\Core\Entity\EntityManagerInterface;
6 use Drupal\views\Plugin\views\row\RssPluginBase;
9 * Plugin which performs a node_view on the resulting object
10 * and formats it as an RSS item.
14 * title = @Translation("Content"),
15 * help = @Translation("Display the content with standard node view."),
16 * theme = "views_view_row_rss",
17 * register_theme = FALSE,
18 * base = {"node_field_data"},
19 * display_types = {"feed"}
22 class Rss extends RssPluginBase {
24 // Basic properties that let the row style follow relationships.
25 public $base_table = 'node_field_data';
27 public $base_field = 'nid';
29 // Stores the nodes loaded with preRender.
35 protected $entityTypeId = 'node';
40 * @var \Drupal\node\NodeStorageInterface
42 protected $nodeStorage;
45 * Constructs the Rss object.
47 * @param array $configuration
48 * A configuration array containing information about the plugin instance.
49 * @param string $plugin_id
50 * The plugin_id for the plugin instance.
51 * @param mixed $plugin_definition
52 * The plugin implementation definition.
53 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
56 public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $entity_manager) {
57 parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_manager);
58 $this->nodeStorage = $entity_manager->getStorage('node');
64 public function buildOptionsForm_summary_options() {
65 $options = parent::buildOptionsForm_summary_options();
66 $options['title'] = $this->t('Title only');
67 $options['default'] = $this->t('Use site default RSS settings');
71 public function summaryTitle() {
72 $options = $this->buildOptionsForm_summary_options();
73 return $options[$this->options['view_mode']];
76 public function preRender($values) {
78 foreach ($values as $row) {
79 $nids[] = $row->{$this->field_alias};
82 $this->nodes = $this->nodeStorage->loadMultiple($nids);
86 public function render($row) {
89 $nid = $row->{$this->field_alias};
90 if (!is_numeric($nid)) {
94 $display_mode = $this->options['view_mode'];
95 if ($display_mode == 'default') {
96 $display_mode = \Drupal::config('system.rss')->get('items.view_mode');
99 // Load the specified node:
100 /** @var \Drupal\node\NodeInterface $node */
101 $node = $this->nodes[$nid];
106 $node->link = $node->url('canonical', ['absolute' => TRUE]);
107 $node->rss_namespaces = [];
108 $node->rss_elements = [
111 'value' => gmdate('r', $node->getCreatedTime()),
114 'key' => 'dc:creator',
115 'value' => $node->getOwner()->getDisplayName(),
119 'value' => $node->id() . ' at ' . $base_url,
120 'attributes' => ['isPermaLink' => 'false'],
124 // The node gets built and modules add to or modify $node->rss_elements
125 // and $node->rss_namespaces.
127 $build_mode = $display_mode;
129 $build = node_view($node, $build_mode);
130 unset($build['#theme']);
132 if (!empty($node->rss_namespaces)) {
133 $this->view->style_plugin->namespaces = array_merge($this->view->style_plugin->namespaces, $node->rss_namespaces);
135 elseif (function_exists('rdf_get_namespaces')) {
136 // Merge RDF namespaces in the XML namespaces in case they are used
137 // further in the RSS content.
138 $xml_rdf_namespaces = [];
139 foreach (rdf_get_namespaces() as $prefix => $uri) {
140 $xml_rdf_namespaces['xmlns:' . $prefix] = $uri;
142 $this->view->style_plugin->namespaces += $xml_rdf_namespaces;
145 $item = new \stdClass();
146 if ($display_mode != 'title') {
147 // We render node contents.
148 $item->description = $build;
150 $item->title = $node->label();
151 $item->link = $node->link;
152 // Provide a reference so that the render call in
153 // template_preprocess_views_view_row_rss() can still access it.
154 $item->elements = &$node->rss_elements;
155 $item->nid = $node->id();
157 '#theme' => $this->themeFunctions(),
158 '#view' => $this->view,
159 '#options' => $this->options,