diff --git a/src/Plugin/views/style/ViewsBootstrapTab.php b/src/Plugin/views/style/ViewsBootstrapTab.php index 1fd2710..6013cfa 100644 --- a/src/Plugin/views/style/ViewsBootstrapTab.php +++ b/src/Plugin/views/style/ViewsBootstrapTab.php @@ -38,6 +38,13 @@ class ViewsBootstrapTab extends StylePluginBase { */ protected $usesRowPlugin = TRUE; + /** + * Does the style plugin support grouping of rows. + * + * @var bool + */ + protected $usesGrouping = FALSE; + /** * Definition. */ @@ -56,36 +63,42 @@ class ViewsBootstrapTab extends StylePluginBase { parent::buildOptionsForm($form, $form_state); if (isset($form['grouping'])) { unset($form['grouping']); + } - $form['tab_field'] = [ - '#type' => 'select', - '#title' => $this->t('Tab field'), - '#options' => $this->displayHandler->getFieldLabels(TRUE), - '#required' => TRUE, - '#default_value' => $this->options['tab_field'], - '#description' => $this->t('Select the field that will be used as the tab.'), - ]; + $form['tab_field'] = [ + '#type' => 'select', + '#title' => $this->t('Tab field'), + '#options' => $this->displayHandler->getFieldLabels(TRUE), + '#required' => TRUE, + '#default_value' => $this->options['tab_field'], + '#description' => $this->t('Select the field that will be used as the tab.'), + ]; - $form['tab_type'] = [ - '#type' => 'select', - '#title' => $this->t('Tab Type'), - '#options' => [ - 'tabs' => $this->t('Tabs'), - 'pills' => $this->t('Pills'), - 'list' => $this->t('List'), - ], - '#required' => TRUE, - '#default_value' => $this->options['tab_type'], - ]; + $form['tab_group'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Group by tab field'), + '#default_value' => $this->options['tab_group'], + '#description' => $this->t('If you want to group tabs by same values.'), + ]; - $form['justified'] = [ - '#type' => 'checkbox', - '#title' => $this->t('Justified'), - '#default_value' => $this->options['justified'], - '#description' => $this->t('Make tabs equal widths of their parent'), - ]; - } + $form['tab_type'] = [ + '#type' => 'select', + '#title' => $this->t('Tab Type'), + '#options' => [ + 'tabs' => $this->t('Tabs'), + 'pills' => $this->t('Pills'), + 'list' => $this->t('List'), + ], + '#required' => TRUE, + '#default_value' => $this->options['tab_type'], + ]; + $form['justified'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Justified'), + '#default_value' => $this->options['justified'], + '#description' => $this->t('Make tabs equal widths of their parent'), + ]; } } diff --git a/templates/views-bootstrap-tab.html.twig b/templates/views-bootstrap-tab.html.twig --- a/templates/views-bootstrap-tab.html.twig +++ b/templates/views-bootstrap-tab.html.twig @@ -1,21 +1,31 @@ {% if title is not empty %} -

{{ title }}

+

{{ title }}

{% endif %}
- -
- {% for key, row in rows %} - {% set row_classes = ['tab-pane', 'fade', loop.first ? 'show active'] %} -
- {{ row.content }} -
- {% endfor %} -
+ +
+ {% for key, row in rows %} + {% set row_classes = ['tab-pane', 'fade', loop.first ? 'show active'] %} +
+ {% if row.content is not empty %} + {{ row.content }} + {% endif %} + {% if row.content_tabs is not empty %} + {% for tc_key, tab_content in row.content_tabs %} + {{ tab_content }} + {% endfor %} + {% endif %} +
+ {% endfor %} +
diff --git a/views_bootstrap.theme.inc b/views_bootstrap.theme.inc index 8ff73bd..824600e 100644 --- a/views_bootstrap.theme.inc +++ b/views_bootstrap.theme.inc @@ -212,18 +212,53 @@ function template_preprocess_views_bootstrap_tab(array &$vars) { $tab_field = $view->style_plugin->options['tab_field']; $vars['tab_type'] = $view->style_plugin->options['tab_type']; $vars['justified'] = $view->style_plugin->options['justified']; + $vars['tab_group'] = $view->style_plugin->options['tab_group']; // Get tabs. if ($tab_field) { + // Keeps which id is used for group by field value + $tab_ids = []; if (isset($view->field[$tab_field])) { foreach (array_keys($vars['rows']) as $key) { - $vars['tabs'][$key] = $view->style_plugin->getFieldValue($key, $tab_field); + $tab_field_value = $view->style_plugin->getFieldValue($key, $tab_field); + // The rendered field but strip tags so no views markup ruins + // the tab specific markup. + $tab_field_rendered = strip_tags($view->style_plugin->getField($key, $tab_field)); + + // If tabs are grouped get previous key for the value + if ($vars['tab_group'] && isset($tab_ids[$tab_field_value])) { + $key = $tab_ids[$tab_field_value]; + } + else { + $tab_ids[$tab_field_value] = $key; + } + $vars['tabs'][$key] = $tab_field_rendered; } } foreach ($vars['rows'] as $id => $row) { $vars['rows'][$id] = []; - $vars['rows'][$id]['content'] = $row; - $vars['rows'][$id]['attributes'] = new Attribute(); + // $vars['rows'][$id]['content'] = $row; + // $vars['rows'][$id]['attributes'] = new Attribute(); + + $tab_field_value = $view->style_plugin->getFieldValue($id, $tab_field); + + // If groups the tabs create new array with all the values for the tab content + if ($vars['tab_group'] && isset($tab_ids[$tab_field_value])) { + if (!isset($vars['rows'][$id]['content_tabs'])) { + $vars['rows'][$id]['content_tabs'] = array(); + } + + // group by the key of first met value of the field grouped by + $id = $tab_ids[$tab_field_value]; + $vars['rows'][$id]['content_tabs'][] = $row; + } + else { + $vars['rows'][$id]['content'] = $row; + } + + if (!isset($vars['rows'][$id]['attributes'])) { + $vars['rows'][$id]['attributes'] = new Attribute(); + } if ($row_class = $view->style_plugin->getRowClass($id)) { $vars['rows'][$id]['attributes']->addClass($row_class); }