Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / views / src / Plugin / views / style / Grid.php
1 <?php
2
3 namespace Drupal\views\Plugin\views\style;
4
5 use Drupal\Component\Utility\Html;
6 use Drupal\Core\Form\FormStateInterface;
7
8 /**
9  * Style plugin to render each item in a grid cell.
10  *
11  * @ingroup views_style_plugins
12  *
13  * @ViewsStyle(
14  *   id = "grid",
15  *   title = @Translation("Grid"),
16  *   help = @Translation("Displays rows in a grid."),
17  *   theme = "views_view_grid",
18  *   display_types = {"normal"}
19  * )
20  */
21 class Grid extends StylePluginBase {
22
23   /**
24    * {@inheritdoc}
25    */
26   protected $usesRowPlugin = TRUE;
27
28   /**
29    * {@inheritdoc}
30    */
31   protected function defineOptions() {
32     $options = parent::defineOptions();
33     $options['columns'] = ['default' => '4'];
34     $options['automatic_width'] = ['default' => TRUE];
35     $options['alignment'] = ['default' => 'horizontal'];
36     $options['col_class_custom'] = ['default' => ''];
37     $options['col_class_default'] = ['default' => TRUE];
38     $options['row_class_custom'] = ['default' => ''];
39     $options['row_class_default'] = ['default' => TRUE];
40     return $options;
41   }
42
43   /**
44    * {@inheritdoc}
45    */
46   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
47     parent::buildOptionsForm($form, $form_state);
48     $form['columns'] = [
49       '#type' => 'number',
50       '#title' => $this->t('Number of columns'),
51       '#default_value' => $this->options['columns'],
52       '#required' => TRUE,
53       '#min' => 1,
54     ];
55     $form['automatic_width'] = [
56       '#type' => 'checkbox',
57       '#title' => $this->t('Automatic width'),
58       '#description' => $this->t('The width of each column will be calculated automatically based on the number of columns provided. If additional classes are entered or a theme injects classes based on a grid system, disabling this option may prove beneficial.'),
59       '#default_value' => $this->options['automatic_width'],
60     ];
61     $form['alignment'] = [
62       '#type' => 'radios',
63       '#title' => $this->t('Alignment'),
64       '#options' => ['horizontal' => $this->t('Horizontal'), 'vertical' => $this->t('Vertical')],
65       '#default_value' => $this->options['alignment'],
66       '#description' => $this->t('Horizontal alignment will place items starting in the upper left and moving right. Vertical alignment will place items starting in the upper left and moving down.'),
67     ];
68     $form['col_class_default'] = [
69       '#title' => $this->t('Default column classes'),
70       '#description' => $this->t('Add the default views column classes like views-col, col-1 and clearfix to the output. You can use this to quickly reduce the amount of markup the view provides by default, at the cost of making it more difficult to apply CSS.'),
71       '#type' => 'checkbox',
72       '#default_value' => $this->options['col_class_default'],
73     ];
74     $form['col_class_custom'] = [
75       '#title' => $this->t('Custom column class'),
76       '#description' => $this->t('Additional classes to provide on each column. Separated by a space.'),
77       '#type' => 'textfield',
78       '#default_value' => $this->options['col_class_custom'],
79     ];
80     if ($this->usesFields()) {
81       $form['col_class_custom']['#description'] .= ' ' . $this->t('You may use field tokens from as per the "Replacement patterns" used in "Rewrite the output of this field" for all fields.');
82     }
83     $form['row_class_default'] = [
84       '#title' => $this->t('Default row classes'),
85       '#description' => $this->t('Adds the default views row classes like views-row, row-1 and clearfix to the output. You can use this to quickly reduce the amount of markup the view provides by default, at the cost of making it more difficult to apply CSS.'),
86       '#type' => 'checkbox',
87       '#default_value' => $this->options['row_class_default'],
88     ];
89     $form['row_class_custom'] = [
90       '#title' => $this->t('Custom row class'),
91       '#description' => $this->t('Additional classes to provide on each row. Separated by a space.'),
92       '#type' => 'textfield',
93       '#default_value' => $this->options['row_class_custom'],
94     ];
95     if ($this->usesFields()) {
96       $form['row_class_custom']['#description'] .= ' ' . $this->t('You may use field tokens from as per the "Replacement patterns" used in "Rewrite the output of this field" for all fields.');
97     }
98   }
99
100   /**
101    * Return the token-replaced row or column classes for the specified result.
102    *
103    * @param int $result_index
104    *   The delta of the result item to get custom classes for.
105    * @param string $type
106    *   The type of custom grid class to return, either "row" or "col".
107    *
108    * @return string
109    *   A space-delimited string of classes.
110    */
111   public function getCustomClass($result_index, $type) {
112     $class = $this->options[$type . '_class_custom'];
113     if ($this->usesFields() && $this->view->field) {
114       $class = strip_tags($this->tokenizeValue($class, $result_index));
115     }
116
117     $classes = explode(' ', $class);
118     foreach ($classes as &$class) {
119       $class = Html::cleanCssIdentifier($class);
120     }
121     return implode(' ', $classes);
122   }
123
124 }