3 namespace Drupal\views\Plugin\views\row;
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\views\Plugin\views\PluginBase;
7 use Drupal\views\Views;
10 * @defgroup views_row_plugins Views row plugins
12 * Plugins that control how Views outputs an individual record.
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.
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
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.
27 * @ingroup views_plugins
32 * Base class for Views row plugins.
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
38 abstract class RowPluginBase extends PluginBase {
43 protected $usesOptions = TRUE;
46 * Does the row plugin support to add fields to its output.
50 protected $usesFields = FALSE;
53 * Returns the usesFields property.
57 public function usesFields() {
58 return $this->usesFields;
64 protected function defineOptions() {
65 $options = parent::defineOptions();
66 if (isset($this->base_table)) {
67 $options['relationship'] = ['default' => 'none'];
74 * Provide a form for setting options.
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();
81 // A whole bunch of code to figure out what relationships are valid for
83 $relationships = $executable->display_handler->getOption('relationships');
84 $relationship_options = [];
86 foreach ($relationships as $relationship) {
87 $relationship_handler = Views::handlerManager('relationship')->getHandler($relationship);
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();
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);
106 $form['relationship'] = [
108 '#title' => $this->t('Relationship'),
109 '#options' => $relationship_options,
110 '#default_value' => $rel,
114 $form['relationship'] = [
123 * Validate the options form.
125 public function validateOptionsForm(&$form, FormStateInterface $form_state) {}
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.
131 public function submitOptionsForm(&$form, FormStateInterface $form_state) {}
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);
143 $this->field_alias = $this->view->query->addField($this->base_table, $this->base_field);
149 * Allow the style to do stuff before each row is rendered.
152 * The full array of results from the query.
154 public function preRender($result) {}
157 * Render a row object. This usually passes through to a theme template
158 * of some form, but not always.
161 * A single row of the query result, so an element of $view->result.
164 * The rendered output of a single row, used by the style plugin.
166 public function render($row) {
168 '#theme' => $this->themeFunctions(),
169 '#view' => $this->view,
170 '#options' => $this->options,
172 '#field_alias' => isset($this->field_alias) ? $this->field_alias : '',