33351185041c2ca26241553918d5d9f55cc32f88
[yaffs-website] / web / core / modules / views / src / Plugin / views / row / RowPluginBase.php
1 <?php
2
3 namespace Drupal\views\Plugin\views\row;
4
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\views\Plugin\views\PluginBase;
7 use Drupal\views\Views;
8
9 /**
10  * @defgroup views_row_plugins Views row plugins
11  * @{
12  * Plugins that control how Views outputs an individual record.
13  *
14  * Row plugins handle rendering each individual record from the view results.
15  * For instance, a row plugin could render fields, render an entire entity
16  * in a particular view mode, or render the raw data from the results.
17  *
18  * Row plugins are used by some (but not all) style plugins. They are not
19  * activated unless the style plugin sets them up. See the
20  * @link views_style_plugins Views style plugins topic @endlink for
21  * more information.
22  *
23  * Row plugins extend \Drupal\views\Plugin\views\row\RowPluginBase. They must
24  * be annotated with \Drupal\views\Annotation\ViewsRow annotation, and
25  * they must be in namespace directory Plugin\views\row.
26  *
27  * @ingroup views_plugins
28  * @see plugin_api
29  */
30
31 /**
32  * Base class for Views row plugins.
33  *
34  * This is really just a wrapper around a theme hook. It renders a row
35  * of the result table by putting it into a render array with the set theme
36  * hook.
37  */
38 abstract class RowPluginBase extends PluginBase {
39
40   /**
41    * {@inheritdoc}
42    */
43   protected $usesOptions = TRUE;
44
45   /**
46    * Does the row plugin support to add fields to its output.
47    *
48    * @var bool
49    */
50   protected $usesFields = FALSE;
51
52   /**
53    * Returns the usesFields property.
54    *
55    * @return bool
56    */
57   public function usesFields() {
58     return $this->usesFields;
59   }
60
61   /**
62    * {@inheritdoc}
63    */
64   protected function defineOptions() {
65     $options = parent::defineOptions();
66     if (isset($this->base_table)) {
67       $options['relationship'] = ['default' => 'none'];
68     }
69
70     return $options;
71   }
72
73   /**
74    * Provide a form for setting options.
75    */
76   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
77     parent::buildOptionsForm($form, $form_state);
78     if (isset($this->base_table)) {
79       $executable = $form_state->get('view')->getExecutable();
80
81       // A whole bunch of code to figure out what relationships are valid for
82       // this item.
83       $relationships = $executable->display_handler->getOption('relationships');
84       $relationship_options = [];
85
86       foreach ($relationships as $relationship) {
87         $relationship_handler = Views::handlerManager('relationship')->getHandler($relationship);
88
89         // If this relationship is valid for this type, add it to the list.
90         $data = Views::viewsData()->get($relationship['table']);
91         $base = $data[$relationship['field']]['relationship']['base'];
92         if ($base == $this->base_table) {
93           $relationship_handler->init($executable, $relationship);
94           $relationship_options[$relationship['id']] = $relationship_handler->adminLabel();
95         }
96       }
97
98       if (!empty($relationship_options)) {
99         $relationship_options = array_merge(['none' => $this->t('Do not use a relationship')], $relationship_options);
100         $rel = empty($this->options['relationship']) ? 'none' : $this->options['relationship'];
101         if (empty($relationship_options[$rel])) {
102           // Pick the first relationship.
103           $rel = key($relationship_options);
104         }
105
106         $form['relationship'] = [
107           '#type' => 'select',
108           '#title' => $this->t('Relationship'),
109           '#options' => $relationship_options,
110           '#default_value' => $rel,
111         ];
112       }
113       else {
114         $form['relationship'] = [
115           '#type' => 'value',
116           '#value' => 'none',
117         ];
118       }
119     }
120   }
121
122   /**
123    * Validate the options form.
124    */
125   public function validateOptionsForm(&$form, FormStateInterface $form_state) {}
126
127   /**
128    * Perform any necessary changes to the form values prior to storage.
129    * There is no need for this function to actually store the data.
130    */
131   public function submitOptionsForm(&$form, FormStateInterface $form_state) {}
132
133   /**
134    * {@inheritdoc}
135    */
136   public function query() {
137     if (isset($this->base_table)) {
138       if (isset($this->options['relationship']) && isset($this->view->relationship[$this->options['relationship']])) {
139         $relationship = $this->view->relationship[$this->options['relationship']];
140         $this->field_alias = $this->view->query->addField($relationship->alias, $this->base_field);
141       }
142       else {
143         $this->field_alias = $this->view->query->addField($this->base_table, $this->base_field);
144       }
145     }
146   }
147
148   /**
149    * Allow the style to do stuff before each row is rendered.
150    *
151    * @param $result
152    *   The full array of results from the query.
153    */
154   public function preRender($result) {}
155
156   /**
157    * Render a row object. This usually passes through to a theme template
158    * of some form, but not always.
159    *
160    * @param object $row
161    *   A single row of the query result, so an element of $view->result.
162    *
163    * @return string
164    *   The rendered output of a single row, used by the style plugin.
165    */
166   public function render($row) {
167     return [
168       '#theme' => $this->themeFunctions(),
169       '#view' => $this->view,
170       '#options' => $this->options,
171       '#row' => $row,
172       '#field_alias' => isset($this->field_alias) ? $this->field_alias : '',
173     ];
174   }
175
176 }
177
178 /**
179  * @}
180  */