/core/modules/node/src/Plugin/views/row/Rss.php

https://gitlab.com/geeta7/drupal · PHP · 171 lines · 94 code · 25 blank · 52 comment · 8 complexity · cfc3b2493f2199c914a3120c31cde137 MD5 · raw file

  1. <?php
  2. /**
  3. * @file
  4. * Contains \Drupal\node\Plugin\views\row\Rss.
  5. */
  6. namespace Drupal\node\Plugin\views\row;
  7. use Drupal\Core\Entity\EntityManagerInterface;
  8. use Drupal\views\Plugin\views\row\RssPluginBase;
  9. /**
  10. * Plugin which performs a node_view on the resulting object
  11. * and formats it as an RSS item.
  12. *
  13. * @ViewsRow(
  14. * id = "node_rss",
  15. * title = @Translation("Content"),
  16. * help = @Translation("Display the content with standard node view."),
  17. * theme = "views_view_row_rss",
  18. * register_theme = FALSE,
  19. * base = {"node_field_data"},
  20. * display_types = {"feed"}
  21. * )
  22. */
  23. class Rss extends RssPluginBase {
  24. // Basic properties that let the row style follow relationships.
  25. var $base_table = 'node_field_data';
  26. var $base_field = 'nid';
  27. // Stores the nodes loaded with preRender.
  28. var $nodes = array();
  29. /**
  30. * {@inheritdoc}
  31. */
  32. protected $entityTypeId = 'node';
  33. /**
  34. * The node storage
  35. *
  36. * @var \Drupal\node\NodeStorageInterface
  37. */
  38. protected $nodeStorage;
  39. /**
  40. * Constructs the Rss object.
  41. *
  42. * @param array $configuration
  43. * A configuration array containing information about the plugin instance.
  44. * @param string $plugin_id
  45. * The plugin_id for the plugin instance.
  46. * @param mixed $plugin_definition
  47. * The plugin implementation definition.
  48. * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
  49. * The entity manager.
  50. */
  51. public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $entity_manager) {
  52. parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_manager);
  53. $this->nodeStorage = $entity_manager->getStorage('node');
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function buildOptionsForm_summary_options() {
  59. $options = parent::buildOptionsForm_summary_options();
  60. $options['title'] = $this->t('Title only');
  61. $options['default'] = $this->t('Use site default RSS settings');
  62. return $options;
  63. }
  64. public function summaryTitle() {
  65. $options = $this->buildOptionsForm_summary_options();
  66. return $options[$this->options['view_mode']];
  67. }
  68. public function preRender($values) {
  69. $nids = array();
  70. foreach ($values as $row) {
  71. $nids[] = $row->{$this->field_alias};
  72. }
  73. if (!empty($nids)) {
  74. $this->nodes = $this->nodeStorage->loadMultiple($nids);
  75. }
  76. }
  77. public function render($row) {
  78. global $base_url;
  79. $nid = $row->{$this->field_alias};
  80. if (!is_numeric($nid)) {
  81. return;
  82. }
  83. $display_mode = $this->options['view_mode'];
  84. if ($display_mode == 'default') {
  85. $display_mode = \Drupal::config('system.rss')->get('items.view_mode');
  86. }
  87. // Load the specified node:
  88. /** @var \Drupal\node\NodeInterface $node */
  89. $node = $this->nodes[$nid];
  90. if (empty($node)) {
  91. return;
  92. }
  93. $node->link = $node->url('canonical', array('absolute' => TRUE));
  94. $node->rss_namespaces = array();
  95. $node->rss_elements = array(
  96. array(
  97. 'key' => 'pubDate',
  98. 'value' => gmdate('r', $node->getCreatedTime()),
  99. ),
  100. array(
  101. 'key' => 'dc:creator',
  102. 'value' => $node->getOwner()->getDisplayName(),
  103. ),
  104. array(
  105. 'key' => 'guid',
  106. 'value' => $node->id() . ' at ' . $base_url,
  107. 'attributes' => array('isPermaLink' => 'false'),
  108. ),
  109. );
  110. // The node gets built and modules add to or modify $node->rss_elements
  111. // and $node->rss_namespaces.
  112. $build_mode = $display_mode;
  113. $build = node_view($node, $build_mode);
  114. unset($build['#theme']);
  115. if (!empty($node->rss_namespaces)) {
  116. $this->view->style_plugin->namespaces = array_merge($this->view->style_plugin->namespaces, $node->rss_namespaces);
  117. }
  118. elseif (function_exists('rdf_get_namespaces')) {
  119. // Merge RDF namespaces in the XML namespaces in case they are used
  120. // further in the RSS content.
  121. $xml_rdf_namespaces = array();
  122. foreach (rdf_get_namespaces() as $prefix => $uri) {
  123. $xml_rdf_namespaces['xmlns:' . $prefix] = $uri;
  124. }
  125. $this->view->style_plugin->namespaces += $xml_rdf_namespaces;
  126. }
  127. $item = new \stdClass();
  128. if ($display_mode != 'title') {
  129. // We render node contents.
  130. $item->description = $build;
  131. }
  132. $item->title = $node->label();
  133. $item->link = $node->link;
  134. // Provide a reference so that the render call in
  135. // template_preprocess_views_view_row_rss() can still access it.
  136. $item->elements = &$node->rss_elements;
  137. $item->nid = $node->id();
  138. $build = array(
  139. '#theme' => $this->themeFunctions(),
  140. '#view' => $this->view,
  141. '#options' => $this->options,
  142. '#row' => $item,
  143. );
  144. return $build;
  145. }
  146. }