Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / views / src / Plugin / views / row / OpmlFields.php
1 <?php
2
3 namespace Drupal\views\Plugin\views\row;
4
5 use Drupal\Core\Form\FormStateInterface;
6
7 /**
8  * Renders an OPML item based on fields.
9  *
10  * @ViewsRow(
11  *   id = "opml_fields",
12  *   title = @Translation("OPML fields"),
13  *   help = @Translation("Display fields as OPML items."),
14  *   theme = "views_view_row_opml",
15  *   display_types = {"feed"}
16  * )
17  */
18 class OpmlFields extends RowPluginBase {
19
20   /**
21    * Does the row plugin support to add fields to it's output.
22    *
23    * @var bool
24    */
25   protected $usesFields = TRUE;
26
27   /**
28    * {@inheritdoc}
29    */
30   protected function defineOptions() {
31     $options = parent::defineOptions();
32     $options['text_field'] = ['default' => ''];
33     $options['created_field'] = ['default' => ''];
34     $options['type_field'] = ['default' => ''];
35     $options['description_field'] = ['default' => ''];
36     $options['html_url_field'] = ['default' => ''];
37     $options['language_field'] = ['default' => ''];
38     $options['xml_url_field'] = ['default' => ''];
39     $options['url_field'] = ['default' => ''];
40     return $options;
41   }
42
43   /**
44    * {@inheritdoc}
45    */
46   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
47     parent::buildOptionsForm($form, $form_state);
48
49     $initial_labels = ['' => $this->t('- None -')];
50     $view_fields_labels = $this->displayHandler->getFieldLabels();
51     $view_fields_labels = array_merge($initial_labels, $view_fields_labels);
52
53     $types = [
54       'rss' => $this->t('RSS'),
55       'link' => $this->t('Link'),
56       'include' => $this->t('Include'),
57     ];
58     $types = array_merge($initial_labels, $types);
59     $form['type_field'] = [
60       '#type' => 'select',
61       '#title' => $this->t('Type attribute'),
62       '#description' => $this->t('The type of this row.'),
63       '#options' => $types,
64       '#default_value' => $this->options['type_field'],
65     ];
66     $form['text_field'] = [
67       '#type' => 'select',
68       '#title' => $this->t('Text attribute'),
69       '#description' => $this->t('The field that is going to be used as the OPML text attribute for each row.'),
70       '#options' => $view_fields_labels,
71       '#default_value' => $this->options['text_field'],
72       '#required' => TRUE,
73     ];
74     $form['created_field'] = [
75       '#type' => 'select',
76       '#title' => $this->t('Created attribute'),
77       '#description' => $this->t('The field that is going to be used as the OPML created attribute for each row.'),
78       '#options' => $view_fields_labels,
79       '#default_value' => $this->options['created_field'],
80     ];
81     $form['description_field'] = [
82       '#type' => 'select',
83       '#title' => $this->t('Description attribute'),
84       '#description' => $this->t('The field that is going to be used as the OPML description attribute for each row.'),
85       '#options' => $view_fields_labels,
86       '#default_value' => $this->options['description_field'],
87       '#states' => [
88         'visible' => [
89           ':input[name="row_options[type_field]"]' => ['value' => 'rss'],
90         ],
91       ],
92     ];
93     $form['html_url_field'] = [
94       '#type' => 'select',
95       '#title' => $this->t('HTML URL attribute'),
96       '#description' => $this->t('The field that is going to be used as the OPML htmlUrl attribute for each row.'),
97       '#options' => $view_fields_labels,
98       '#default_value' => $this->options['html_url_field'],
99       '#states' => [
100         'visible' => [
101           ':input[name="row_options[type_field]"]' => ['value' => 'rss'],
102         ],
103       ],
104     ];
105     $form['language_field'] = [
106       '#type' => 'select',
107       '#title' => $this->t('Language attribute'),
108       '#description' => $this->t('The field that is going to be used as the OPML language attribute for each row.'),
109       '#options' => $view_fields_labels,
110       '#default_value' => $this->options['language_field'],
111       '#states' => [
112         'visible' => [
113           ':input[name="row_options[type_field]"]' => ['value' => 'rss'],
114         ],
115       ],
116     ];
117     $form['xml_url_field'] = [
118       '#type' => 'select',
119       '#title' => $this->t('XML URL attribute'),
120       '#description' => $this->t('The field that is going to be used as the OPML text attribute for each row.'),
121       '#options' => $view_fields_labels,
122       '#default_value' => $this->options['xml_url_field'],
123       '#states' => [
124         'visible' => [
125           ':input[name="row_options[type_field]"]' => ['value' => 'rss'],
126         ],
127       ],
128     ];
129     $form['url_field'] = [
130       '#type' => 'select',
131       '#title' => $this->t('URL attribute'),
132       '#description' => $this->t('The field that is going to be used as the OPML URL attribute for each row.'),
133       '#options' => $view_fields_labels,
134       '#default_value' => $this->options['url_field'],
135       '#states' => [
136         'visible' => [
137           ':input[name="row_options[type_field]"]' => [
138             ['value' => 'link'],
139             ['value' => 'include'],
140           ],
141         ],
142       ],
143     ];
144   }
145
146   /**
147    * {@inheritdoc}
148    */
149   public function validate() {
150     $errors = parent::validate();
151     if (empty($this->options['text_field'])) {
152       $errors[] = $this->t('Row style plugin requires specifying which views field to use for OPML text attribute.');
153     }
154     if (!empty($this->options['type_field'])) {
155       if ($this->options['type_field'] == 'rss') {
156         if (empty($this->options['xml_url_field'])) {
157           $errors[] = $this->t('Row style plugin requires specifying which views field to use for XML URL attribute.');
158         }
159       }
160       elseif (in_array($this->options['type_field'], ['link', 'include'])) {
161         if (empty($this->options['url_field'])) {
162           $errors[] = $this->t('Row style plugin requires specifying which views field to use for URL attribute.');
163         }
164       }
165     }
166     return $errors;
167   }
168
169   /**
170    * {@inheritdoc}
171    */
172   public function render($row) {
173     // Create the OPML item array.
174     $item = [];
175     $row_index = $this->view->row_index;
176     $item['text'] = $this->getField($row_index, $this->options['text_field']);
177     $item['created'] = $this->getField($row_index, $this->options['created_field']);
178     if ($this->options['type_field']) {
179       $item['type'] = $this->options['type_field'];
180       if ($item['type'] == 'rss') {
181         $item['description'] = $this->getField($row_index, $this->options['description_field']);
182         $item['language'] = $this->getField($row_index, $this->options['language_field']);
183         $item['xmlUrl'] = $this->getField($row_index, $this->options['xml_url_field']);
184         $item['htmlUrl'] = $this->getField($row_index, $this->options['html_url_field']);
185       }
186       else {
187         $item['url'] = $this->getField($row_index, $this->options['url_field']);
188       }
189     }
190     // Remove empty attributes.
191     $item = array_filter($item);
192
193     $build = [
194       '#theme' => $this->themeFunctions(),
195       '#view' => $this->view,
196       '#options' => $this->options,
197       '#row' => $item,
198       '#field_alias' => isset($this->field_alias) ? $this->field_alias : '',
199     ];
200     return $build;
201   }
202
203   /**
204    * Retrieves a views field value from the style plugin.
205    *
206    * @param $index
207    *   The index count of the row as expected by views_plugin_style::getField().
208    * @param $field_id
209    *   The ID assigned to the required field in the display.
210    *
211    * @return string
212    *   The rendered field value.
213    */
214   public function getField($index, $field_id) {
215     if (empty($this->view->style_plugin) || !is_object($this->view->style_plugin) || empty($field_id)) {
216       return '';
217     }
218     return (string) $this->view->style_plugin->getField($index, $field_id);
219   }
220
221 }