View source
<?php
namespace Drupal\text_with_title\Plugin\Field\FieldFormatter;
use Drupal\Component\Utility\Html;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageInterface;
class TextWithTitleTabsVerticalFormatter extends FormatterBase {
public static function defaultSettings() {
return [
'tabs_width' => '3',
] + parent::defaultSettings();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$element['tabs_width'] = [
'#title' => $this
->t('Tabs Width'),
'#type' => 'select',
'#options' => [
'1' => '1 Column',
'2' => '2 Column',
'3' => '3 Column',
'4' => '4 Column',
'5' => '5 Column',
'6' => '6 Column',
'7' => '7 Column',
'8' => '8 Column',
'9' => '9 Column',
'10' => '10 Column',
'11' => '11 Column',
'12' => '12 Column',
],
'#default_value' => $this
->getSetting('tabs_width'),
];
return $element;
}
public function view(FieldItemListInterface $items, $langcode = NULL) {
if (empty($langcode)) {
$langcode = \Drupal::languageManager()
->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)
->getId();
}
$elements = $this
->viewElements($items, $langcode);
$build = [
'#theme' => 'text_with_title_tabs_vertical',
'#tabs' => $elements,
'#tabs_width' => $this
->getSetting('tabs_width'),
];
return $build;
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
$id = Html::getUniqueId('bootstrap_tabs');
foreach ($items as $delta => $item) {
$elements[$delta] = [
'title' => [
'#plain_text' => $item->title,
],
'active' => $delta == 0 ? TRUE : FALSE,
'id' => $id . '--' . $delta,
'text' => [
'#type' => 'processed_text',
'#text' => $item->text['value'],
'#format' => $item->text['format'],
'#langcode' => $langcode,
],
];
}
return $elements;
}
}