Version 1
[yaffs-website] / web / core / modules / node / src / Plugin / views / row / Rss.php
1 <?php
2
3 namespace Drupal\node\Plugin\views\row;
4
5 use Drupal\Core\Entity\EntityManagerInterface;
6 use Drupal\views\Plugin\views\row\RssPluginBase;
7
8 /**
9  * Plugin which performs a node_view on the resulting object
10  * and formats it as an RSS item.
11  *
12  * @ViewsRow(
13  *   id = "node_rss",
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"}
20  * )
21  */
22 class Rss extends RssPluginBase {
23
24   // Basic properties that let the row style follow relationships.
25   public $base_table = 'node_field_data';
26
27   public $base_field = 'nid';
28
29   // Stores the nodes loaded with preRender.
30   public $nodes = [];
31
32   /**
33    * {@inheritdoc}
34    */
35   protected $entityTypeId = 'node';
36
37   /**
38    * The node storage
39    *
40    * @var \Drupal\node\NodeStorageInterface
41    */
42   protected $nodeStorage;
43
44   /**
45    * Constructs the Rss object.
46    *
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
54    *   The entity manager.
55    */
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');
59   }
60
61   /**
62    * {@inheritdoc}
63    */
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');
68     return $options;
69   }
70
71   public function summaryTitle() {
72     $options = $this->buildOptionsForm_summary_options();
73     return $options[$this->options['view_mode']];
74   }
75
76   public function preRender($values) {
77     $nids = [];
78     foreach ($values as $row) {
79       $nids[] = $row->{$this->field_alias};
80     }
81     if (!empty($nids)) {
82       $this->nodes = $this->nodeStorage->loadMultiple($nids);
83     }
84   }
85
86   public function render($row) {
87     global $base_url;
88
89     $nid = $row->{$this->field_alias};
90     if (!is_numeric($nid)) {
91       return;
92     }
93
94     $display_mode = $this->options['view_mode'];
95     if ($display_mode == 'default') {
96       $display_mode = \Drupal::config('system.rss')->get('items.view_mode');
97     }
98
99     // Load the specified node:
100     /** @var \Drupal\node\NodeInterface $node */
101     $node = $this->nodes[$nid];
102     if (empty($node)) {
103       return;
104     }
105
106     $node->link = $node->url('canonical', ['absolute' => TRUE]);
107     $node->rss_namespaces = [];
108     $node->rss_elements = [
109       [
110         'key' => 'pubDate',
111         'value' => gmdate('r', $node->getCreatedTime()),
112       ],
113       [
114         'key' => 'dc:creator',
115         'value' => $node->getOwner()->getDisplayName(),
116       ],
117       [
118         'key' => 'guid',
119         'value' => $node->id() . ' at ' . $base_url,
120         'attributes' => ['isPermaLink' => 'false'],
121       ],
122     ];
123
124     // The node gets built and modules add to or modify $node->rss_elements
125     // and $node->rss_namespaces.
126
127     $build_mode = $display_mode;
128
129     $build = node_view($node, $build_mode);
130     unset($build['#theme']);
131
132     if (!empty($node->rss_namespaces)) {
133       $this->view->style_plugin->namespaces = array_merge($this->view->style_plugin->namespaces, $node->rss_namespaces);
134     }
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;
141       }
142       $this->view->style_plugin->namespaces += $xml_rdf_namespaces;
143     }
144
145     $item = new \stdClass();
146     if ($display_mode != 'title') {
147       // We render node contents.
148       $item->description = $build;
149     }
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();
156     $build = [
157       '#theme' => $this->themeFunctions(),
158       '#view' => $this->view,
159       '#options' => $this->options,
160       '#row' => $item,
161     ];
162
163     return $build;
164   }
165
166 }