Details.php in Field Group 8
File
src/Plugin/field_group/FieldGroupFormatter/Details.php
View source
<?php
namespace Drupal\field_group\Plugin\field_group\FieldGroupFormatter;
use Drupal\Component\Utility\Html;
use Drupal\field_group\FieldGroupFormatterBase;
class Details extends FieldGroupFormatterBase {
public function preRender(&$element, $rendering_object) {
parent::preRender($element, $rendering_object);
$element += array(
'#type' => 'details',
'#title' => Html::escape($this
->t($this
->getLabel())),
'#open' => $this
->getSetting('open'),
);
if ($this
->getSetting('id')) {
$element['#id'] = Html::getId($this
->getSetting('id'));
}
$classes = $this
->getClasses();
if (!empty($classes)) {
$element += array(
'#attributes' => array(
'class' => $classes,
),
);
}
if ($this
->getSetting('description')) {
$element += array(
'#description' => $this
->getSetting('description'),
);
}
if ($this
->getSetting('required_fields')) {
$element['#attached']['library'][] = 'field_group/formatter.details';
$element['#attached']['library'][] = 'field_group/core';
}
}
public function settingsForm() {
$form = parent::settingsForm();
$form['open'] = array(
'#type' => 'checkbox',
'#title' => $this
->t('Display element open by default.'),
'#default_value' => $this
->getSetting('open'),
);
if ($this->context == 'form') {
$form['required_fields'] = array(
'#type' => 'checkbox',
'#title' => $this
->t('Mark group as required if it contains required fields.'),
'#default_value' => $this
->getSetting('required_fields'),
'#weight' => 2,
);
}
return $form;
}
public function settingsSummary() {
$summary = array();
if ($this
->getSetting('open')) {
$summary[] = $this
->t('Default state open');
}
else {
$summary[] = $this
->t('Default state closed');
}
if ($this
->getSetting('required_fields')) {
$summary[] = $this
->t('Mark as required');
}
return $summary;
}
public static function defaultContextSettings($context) {
$defaults = array(
'open' => FALSE,
'required_fields' => $context == 'form',
) + parent::defaultSettings($context);
if ($context == 'form') {
$defaults['required_fields'] = 1;
}
return $defaults;
}
}