More tidying.
[yaffs-website] / web / modules / contrib / views_bootstrap / src / Plugin / views / style / ViewsBootstrapGrid.php
1 <?php
2
3 namespace Drupal\views_bootstrap\Plugin\views\style;
4
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\views\Plugin\views\style\StylePluginBase;
7 use Drupal\Component\Utility\Html;
8
9 /**
10  * Style plugin to render each item in an ordered or unordered list.
11  *
12  * @ingroup views_style_plugins
13  *
14  * @ViewsStyle(
15  *   id = "views_bootstrap_grid",
16  *   title = @Translation("Bootstrap Grid"),
17  *   help = @Translation("Displays rows in a Bootstrap Grid layout"),
18  *   theme = "views_bootstrap_grid",
19  *   theme_file = "../views_bootstrap.theme.inc",
20  *   display_types = {"normal"}
21  * )
22  */
23 class ViewsBootstrapGrid extends StylePluginBase {
24   /**
25    * Overrides \Drupal\views\Plugin\views\style\StylePluginBase::usesRowPlugin.
26    *
27    * @var bool
28    */
29   protected $usesRowPlugin = TRUE;
30
31   /**
32    * Overrides \Drupal\views\Plugin\views\style\StylePluginBase::usesRowClass.
33    *
34    * @var bool
35    */
36   protected $usesRowClass = TRUE;
37
38   /**
39    * Return the token-replaced row or column classes for the specified result.
40    *
41    * @param int $result_index
42    *   The delta of the result item to get custom classes for.
43    * @param string $type
44    *   The type of custom grid class to return, either "row" or "col".
45    *
46    * @return string
47    *   A space-delimited string of classes.
48    */
49   public function getCustomClass($result_index, $type) {
50     if (isset($this->options[$type . '_class_custom'])) {
51       $class = $this->options[$type . '_class_custom'];
52       if ($this->usesFields() && $this->view->field) {
53         $class = strip_tags($this->tokenizeValue($class, $result_index));
54       }
55
56       $classes = explode(' ', $class);
57       foreach ($classes as &$class) {
58         $class = Html::cleanCssIdentifier($class);
59       }
60       return implode(' ', $classes);
61     }
62   }
63
64   /**
65    * Normalize a list of columns.
66    *
67    * Normalize columns based upon the fields that are available. This compares
68    * the fields stored in the style handler
69    * to the list of fields actually in the view, removing fields that
70    * have been removed and adding new fields in their own column.
71    * - Each field must be in a column.
72    * - Each column must be based upon a field, and that field is somewhere in
73    * the column.
74    * - Any fields not currently represented must be added.
75    * - Columns must be re-ordered to match the fields.
76    *
77    * @param array $columns
78    *   An array of all fields; the key is the id of the field and the
79    *   value is the id of the column the field should be in.
80    * @param array|null $fields
81    *   The fields to use for the columns. If not provided, they will
82    *   be requested from the current display. The running render should
83    *   send the fields through, as they may be different than what the
84    *   display has listed due to access control or other changes.
85    *
86    * @return array
87    *   An array of all the sanitized columns.
88    */
89   public function sanitizeColumns(array $columns, $fields = NULL) {
90     $sanitized = [];
91     if ($fields === NULL) {
92       $fields = $this->displayHandler->getOption('fields');
93     }
94     // Pre-configure the sanitized array so that the order is retained.
95     foreach ($fields as $field => $info) {
96       // Set to itself so that if it isn't touched, it gets column
97       // status automatically.
98       $sanitized[$field] = $field;
99     }
100
101     if (!empty($columns)) {
102       return $sanitized;
103     }
104
105     foreach ($columns as $field => $column) {
106       // first, make sure the field still exists.
107       if (!isset($sanitized[$field])) {
108         continue;
109       }
110
111       // If the field is the column, mark it so, or the column
112       // it's set to is a column, that's ok.
113       if ($field == $column || $columns[$column] == $column && !empty($sanitized[$column])) {
114         $sanitized[$field] = $column;
115       }
116       // Since we set the field to itself initially, ignoring
117       // the condition is ok; the field will get its column
118       // status back.
119     }
120
121     return $sanitized;
122   }
123
124   /**
125    * Definition.
126    */
127   protected function defineOptions() {
128     $options = parent::defineOptions();
129
130     $options['alignment'] = ['default' => 'horizontal'];
131     $options['columns'] = ['default' => '4'];
132     $options['col_xs'] = ['default' => 'col-xs-12'];
133     $options['col_sm'] = ['default' => 'col-sm-12'];
134     $options['col_md'] = ['default' => 'col-md-12'];
135     $options['col_lg'] = ['default' => 'col-lg-12'];
136     $options['automatic_width'] = ['default' => TRUE];
137     $options['col_class_custom'] = ['default' => ''];
138     $options['col_class_default'] = ['default' => TRUE];
139     $options['row_class_custom'] = ['default' => ''];
140     $options['row_class_default'] = ['default' => TRUE];
141     $options['default'] = ['default' => ''];
142     $options['info'] = ['default' => []];
143     $options['override'] = ['default' => TRUE];
144     $options['sticky'] = ['default' => FALSE];
145     $options['order'] = ['default' => 'asc'];
146     $options['caption'] = ['default' => ''];
147     $options['summary'] = ['default' => ''];
148     $options['description'] = ['default' => ''];
149     return $options;
150   }
151
152   /**
153    * {@inheritdoc}
154    */
155   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
156     parent::buildOptionsForm($form, $form_state);
157
158     $form['alignment'] = [
159       '#type' => 'radios',
160       '#title' => $this->t('Alignment'),
161       '#options' => [
162         'horizontal' => $this->t('Horizontal'),
163         'vertical' => $this->t('Vertical'),
164       ],
165       '#description' => $this->t('Horizontal alignment will place items starting in the upper left and moving right.
166       Vertical alignment will place items starting in the upper left and moving down.'),
167       '#default_value' => $this->options['alignment'],
168     ];
169
170     $form['columns'] = [
171       '#type' => 'select',
172       '#title' => $this->t('Number of columns per row'),
173       '#required' => TRUE,
174       '#default_value' => isset($this->options['columns']) ? $this->options['columns'] : NULL,
175       '#options' => [
176         1 => 1,
177         2 => 2,
178         3 => 3,
179         4 => 4,
180         6 => 6,
181         12 => 12,
182         999 => $this->t('All'),
183       ],
184     ];
185
186     foreach (['xs', 'sm', 'md', 'lg'] as $size) {
187       $form["col_${size}"] = [
188         '#type' => 'select',
189         '#title' => $this->t("Number of columns (col-${size})"),
190         '#required' => TRUE,
191         '#default_value' => isset($this->options["col_${size}"]) ? $this->options["col_${size}"] : NULL,
192         '#options' => [
193           "col-${size}-12" => 1,
194           "col-${size}-6" => 2,
195           "col-${size}-4" => 3,
196           "col-${size}-3" => 4,
197           "col-${size}-2" => 6,
198           "col-${size}-1" => 12,
199         ],
200       ];
201     }
202   }
203
204 }