3 namespace Drupal\views\Plugin\views\row;
5 use Drupal\Core\Form\FormStateInterface;
8 * Renders an OPML item based on fields.
12 * title = @Translation("OPML fields"),
13 * help = @Translation("Display fields as OPML items."),
14 * theme = "views_view_row_opml",
15 * display_types = {"feed"}
18 class OpmlFields extends RowPluginBase {
21 * Does the row plugin support to add fields to it's output.
25 protected $usesFields = TRUE;
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' => ''];
46 public function buildOptionsForm(&$form, FormStateInterface $form_state) {
47 parent::buildOptionsForm($form, $form_state);
49 $initial_labels = ['' => $this->t('- None -')];
50 $view_fields_labels = $this->displayHandler->getFieldLabels();
51 $view_fields_labels = array_merge($initial_labels, $view_fields_labels);
54 'rss' => $this->t('RSS'),
55 'link' => $this->t('Link'),
56 'include' => $this->t('Include'),
58 $types = array_merge($initial_labels, $types);
59 $form['type_field'] = [
61 '#title' => $this->t('Type attribute'),
62 '#description' => $this->t('The type of this row.'),
64 '#default_value' => $this->options['type_field'],
66 $form['text_field'] = [
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'],
74 $form['created_field'] = [
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'],
81 $form['description_field'] = [
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'],
89 ':input[name="row_options[type_field]"]' => ['value' => 'rss'],
93 $form['html_url_field'] = [
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'],
101 ':input[name="row_options[type_field]"]' => ['value' => 'rss'],
105 $form['language_field'] = [
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'],
113 ':input[name="row_options[type_field]"]' => ['value' => 'rss'],
117 $form['xml_url_field'] = [
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'],
125 ':input[name="row_options[type_field]"]' => ['value' => 'rss'],
129 $form['url_field'] = [
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'],
137 ':input[name="row_options[type_field]"]' => [
139 ['value' => 'include'],
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.');
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.');
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.');
172 public function render($row) {
173 // Create the OPML item array.
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']);
187 $item['url'] = $this->getField($row_index, $this->options['url_field']);
190 // Remove empty attributes.
191 $item = array_filter($item);
194 '#theme' => $this->themeFunctions(),
195 '#view' => $this->view,
196 '#options' => $this->options,
198 '#field_alias' => isset($this->field_alias) ? $this->field_alias : '',
204 * Retrieves a views field value from the style plugin.
207 * The index count of the row as expected by views_plugin_style::getField().
209 * The ID assigned to the required field in the display.
212 * The rendered field value.
214 public function getField($index, $field_id) {
215 if (empty($this->view->style_plugin) || !is_object($this->view->style_plugin) || empty($field_id)) {
218 return (string) $this->view->style_plugin->getField($index, $field_id);