View source
<?php
namespace Drupal\field_group\Plugin\field_group\FieldGroupFormatter;
use Drupal\Component\Utility\Html;
use Drupal\Core\Form\FormState;
use Drupal\Core\Render\Element\VerticalTabs;
use Drupal\field_group\Element\HorizontalTabs;
use Drupal\field_group\FieldGroupFormatterBase;
class Tabs extends FieldGroupFormatterBase {
public function process(&$element, $processed_object) {
parent::preRender($element, $processed_object);
$element += [
'#prefix' => '<div class=" ' . implode(' ', $this
->getClasses()) . '">',
'#suffix' => '</div>',
'#tree' => TRUE,
'#parents' => [
$this->group->group_name,
],
'#default_tab' => '',
];
if ($this
->getSetting('id')) {
$element['#id'] = Html::getUniqueId($this
->getSetting('id'));
}
if ($this
->getLabel()) {
$element['#title'] = $this
->getLabel();
}
$element += [
'#type' => $this
->getSetting('direction') . '_tabs',
'#theme_wrappers' => [
$this
->getSetting('direction') . '_tabs',
],
];
if ($width_breakpoint = $this
->getSetting('width_breakpoint')) {
$element['#attached']['drupalSettings']['widthBreakpoint'] = $width_breakpoint;
}
}
public function preRender(&$element, $rendering_object) {
$this
->process($element, $rendering_object);
if ($this
->getSetting('direction') == 'vertical') {
$form_state = new FormState();
$complete_form = [];
$element = VerticalTabs::processVerticalTabs($element, $form_state, $complete_form);
}
else {
$form_state = new FormState();
$complete_form = [];
$element = HorizontalTabs::processHorizontalTabs($element, $form_state, $complete_form);
}
$element['group']['#groups'][$this->group->group_name] = [
0 => [],
];
$element['group']['#groups'][$this->group->group_name]['#group_exists'] = TRUE;
}
public function settingsForm() {
$form = parent::settingsForm();
$form['direction'] = [
'#title' => $this
->t('Direction'),
'#type' => 'select',
'#options' => [
'vertical' => $this
->t('Vertical'),
'horizontal' => $this
->t('Horizontal'),
],
'#default_value' => $this
->getSetting('direction'),
'#weight' => 1,
];
$form['width_breakpoint'] = [
'#title' => $this
->t('Width Breakpoint'),
'#description' => $this
->t('Auto-disable the Tabs widget if the window width is equal or smaller than this breakpoint.'),
'#type' => 'number',
'#default_value' => $this
->getSetting('width_breakpoint'),
'#weight' => 2,
'#min' => 0,
];
return $form;
}
public function settingsSummary() {
$summary = parent::settingsSummary();
$summary[] = $this
->t('Direction: @direction', [
'@direction' => $this
->getSetting('direction'),
]);
return $summary;
}
public static function defaultContextSettings($context) {
return [
'direction' => 'vertical',
'width_breakpoint' => 640,
] + parent::defaultContextSettings($context);
}
public function getClasses() {
$classes = parent::getClasses();
$classes[] = 'field-group-' . $this->group->format_type . '-wrapper';
return $classes;
}
}