Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / comment / src / Plugin / views / row / Rss.php
1 <?php
2
3 namespace Drupal\comment\Plugin\views\row;
4
5 use Drupal\views\Plugin\views\row\RssPluginBase;
6
7 /**
8  * Plugin which formats the comments as RSS items.
9  *
10  * @ViewsRow(
11  *   id = "comment_rss",
12  *   title = @Translation("Comment"),
13  *   help = @Translation("Display the comment as RSS."),
14  *   theme = "views_view_row_rss",
15  *   register_theme = FALSE,
16  *   base = {"comment_field_data"},
17  *   display_types = {"feed"}
18  * )
19  */
20 class Rss extends RssPluginBase {
21
22   /**
23    * {@inheritdoc}
24    */
25   protected $base_table = 'comment_field_data';
26
27   /**
28    * {@inheritdoc}
29    */
30   protected $base_field = 'cid';
31
32   /**
33    * @var \Drupal\comment\CommentInterface[]
34    */
35   protected $comments;
36
37   /**
38    * {@inheritdoc}
39    */
40   protected $entityTypeId = 'comment';
41
42   public function preRender($result) {
43     $cids = [];
44
45     foreach ($result as $row) {
46       $cids[] = $row->cid;
47     }
48
49     $this->comments = $this->entityManager->getStorage('comment')->loadMultiple($cids);
50   }
51
52   /**
53    * {@inheritdoc}
54    */
55   public function buildOptionsForm_summary_options() {
56     $options = parent::buildOptionsForm_summary_options();
57     $options['title'] = $this->t('Title only');
58     $options['default'] = $this->t('Use site default RSS settings');
59     return $options;
60   }
61
62   public function render($row) {
63     global $base_url;
64
65     $cid = $row->{$this->field_alias};
66     if (!is_numeric($cid)) {
67       return;
68     }
69
70     $view_mode = $this->options['view_mode'];
71     if ($view_mode == 'default') {
72       $view_mode = \Drupal::config('system.rss')->get('items.view_mode');
73     }
74
75     // Load the specified comment and its associated node:
76     /** @var $comment \Drupal\comment\CommentInterface */
77     $comment = $this->comments[$cid];
78     if (empty($comment)) {
79       return;
80     }
81
82     $comment->link = $comment->url('canonical', ['absolute' => TRUE]);
83     $comment->rss_namespaces = [];
84     $comment->rss_elements = [
85       [
86         'key' => 'pubDate',
87         'value' => gmdate('r', $comment->getCreatedTime()),
88       ],
89       [
90         'key' => 'dc:creator',
91         'value' => $comment->getAuthorName(),
92       ],
93       [
94         'key' => 'guid',
95         'value' => 'comment ' . $comment->id() . ' at ' . $base_url,
96         'attributes' => ['isPermaLink' => 'false'],
97       ],
98     ];
99
100     // The comment gets built and modules add to or modify
101     // $comment->rss_elements and $comment->rss_namespaces.
102     $build = $this->entityManager->getViewBuilder('comment')->view($comment, 'rss');
103     unset($build['#theme']);
104
105     if (!empty($comment->rss_namespaces)) {
106       $this->view->style_plugin->namespaces = array_merge($this->view->style_plugin->namespaces, $comment->rss_namespaces);
107     }
108
109     $item = new \stdClass();
110     if ($view_mode != 'title') {
111       // We render comment contents.
112       $item->description = $build;
113     }
114     $item->title = $comment->label();
115     $item->link = $comment->link;
116     // Provide a reference so that the render call in
117     // template_preprocess_views_view_row_rss() can still access it.
118     $item->elements = &$comment->rss_elements;
119     $item->cid = $comment->id();
120
121     $build = [
122       '#theme' => $this->themeFunctions(),
123       '#view' => $this->view,
124       '#options' => $this->options,
125       '#row' => $item,
126     ];
127     return $build;
128   }
129
130 }