View source
<?php
namespace Drupal\text_with_title\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Template\Attribute;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Form\FormStateInterface;
class TextWithTitleAccordionFormatter extends FormatterBase {
public function view(FieldItemListInterface $items, $langcode = NULL) {
if (empty($langcode)) {
$langcode = \Drupal::languageManager()
->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)
->getId();
}
$entity = $items
->getEntity();
$this->accordion_group_id = $entity
->getEntityTypeId() . '_' . $entity
->bundle() . '_' . $entity
->id();
$elements = $this
->viewElements($items, $langcode);
$build = [
'#theme' => 'text_with_title_accordion',
'#panels' => $elements,
'#id' => $this->accordion_group_id,
];
return $build;
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
foreach ($items as $delta => $item) {
$id = $this->accordion_group_id . '--accordion-' . $delta;
$description_attributes = [];
$body_attributes = [
'id' => $id,
];
$heading_attributes = [
'data-toggle' => 'collapse',
];
if ($this
->getSetting('only_one_open')) {
$entity = $items
->getEntity();
$parent_id = $entity
->getEntityTypeId() . '_' . $entity
->bundle() . '_' . $entity
->id();
$heading_attributes['data-parent'] = '#' . $parent_id;
}
$attributes = [];
$elements[$delta] = [
'#theme' => 'text_with_title_panel',
'#heading' => [
'#plain_text' => $item->title,
],
'#heading_attributes' => new Attribute($heading_attributes),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#attributes' => new Attribute($attributes),
'#id' => $id,
'#target' => '#' . $id,
'#description' => [
'attributes' => new Attribute($description_attributes),
'content' => '',
'position' => 'before',
],
'#body' => [
'#type' => 'processed_text',
'#text' => $item->text['value'],
'#format' => $item->text['format'],
'#langcode' => $langcode,
],
'#body_attributes' => new Attribute($body_attributes),
];
}
return $elements;
}
public static function defaultSettings() {
return [
'only_one_open' => TRUE,
] + parent::defaultSettings();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$element['only_one_open'] = [
'#title' => $this
->t('Allow only one panel to be open at any one time.'),
'#description' => $this
->t('Disable this option to allow multiple panels to be visible at one time.'),
'#type' => 'checkbox',
'#default_value' => $this
->getSetting('only_one_open'),
];
return $element;
}
}