View source
<?php
namespace Drupal\quicktabs\Plugin\views\style;
use Drupal\core\form\FormStateInterface;
use Drupal\views\Plugin\views\style\StylePluginBase;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Component\Utility\Xss;
class Quicktabs extends StylePluginBase {
protected $usesRowPlugin = TRUE;
protected $usesRowClass = TRUE;
protected $defaultFieldLabels = TRUE;
protected $setMapping;
protected $tabs = [];
protected function defineOptions() {
$options = parent::defineOptions();
$options['path'] = [
'default' => 'quicktabs',
];
return $options;
}
public function setTabs(array $tabs) {
$this->tabs = $tabs;
}
public function getTabs() {
return $this->tabs;
}
public function setSetMapping(array $setMapping) {
$this->setMapping = $setMapping;
}
public function getSetMapping() {
return $this->setMapping;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
foreach ($form['grouping'] as $index => &$field) {
if ($index == 0) {
$field['field']['#required'] = 1;
$field['rendered']['#default_value'] = TRUE;
$field['rendered']['#access'] = FALSE;
$field['rendered_strip']['#access'] = FALSE;
}
elseif ($index > 0) {
unset($form['grouping'][$index]);
}
$current_value = $field['field']['#description']
->getUntranslatedString();
$field['field']['#description'] = $this
->t('You must specify a field by which to group the records. This field will be used for the title of each tab.', [
'@current_value' => $current_value,
]);
}
}
public function renderGroupingSets($sets, $level = 0) {
$output = [];
$theme_functions = $this->view
->buildThemeFunctions($this->groupingTheme);
$tab_titles = [];
$link_classes = [
'loaded',
];
$quicktab_id = str_replace('_', '-', $this->view
->id());
$set_count = 0;
foreach ($sets as $index => $set) {
$wrapper_attributes = [];
if ($set_count === 0) {
$wrapper_attributes['class'] = [
'active',
];
}
$tab_titles[] = [
'0' => Link::fromTextAndUrl(new TranslatableMarkup(Xss::filter($index, [
'img',
'em',
'strong',
'h2',
'h3',
'h4',
'h5',
'h6',
'small',
'span',
'i',
'br',
])), Url::fromRoute('<current>', [], [
'attributes' => [
'class' => $link_classes,
],
]))
->toRenderable(),
'#wrapper_attributes' => $wrapper_attributes,
];
$level = isset($set['level']) ? $set['level'] : 0;
$row = reset($set['rows']);
if (is_array($row) && isset($row['group'])) {
$single_output = [
'#theme' => $theme_functions,
'#view' => $this->view,
'#grouping' => $this->options['grouping'][$level],
'#rows' => $set['rows'],
];
}
else {
if ($this
->usesRowPlugin()) {
foreach ($set['rows'] as $index => $row) {
$this->view->row_index = $index;
$set['rows'][$index] = $this->view->rowPlugin
->render($row);
}
}
$single_output = $this
->renderRowGroup($set['rows']);
}
$single_output['#grouping_level'] = $level;
$single_output['#title'] = $set['group'];
if (!empty($this->options['grouping'])) {
$set_mapping = [];
foreach ($sets as $set_index => $set) {
foreach ($set['rows'] as $row_index => $row) {
$set_mapping[$set_index][] = $row_index;
}
}
}
$output[] = $single_output;
$set_count++;
}
$this
->setSetMapping($set_mapping);
unset($this->view->row_index);
$tabs = [
'#theme' => 'item_list',
'#items' => $tab_titles,
'#attributes' => [
'class' => [
'quicktabs-tabs',
],
],
];
$this
->setTabs($tabs);
$output['#theme_wrappers'] = [
'container' => [
'#attributes' => [
'class' => [
'quicktabs-wrapper',
],
'id' => 'quicktabs-' . $quicktab_id,
],
],
];
return $output;
}
}