TextWithTitleTabsFormatter.php in Text with Title Field 8
File
src/Plugin/Field/FieldFormatter/TextWithTitleTabsFormatter.php
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\Language\LanguageInterface;
class TextWithTitleTabsFormatter extends FormatterBase {
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',
'#tabs' => $elements,
];
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;
}
}