View source
<?php
namespace Drupal\bootstrap\Plugin\Preprocess;
use Drupal\bootstrap\Annotation\BootstrapPreprocess;
use Drupal\bootstrap\Utility\Element;
use Drupal\bootstrap\Utility\Variables;
use Drupal\Component\Render\MarkupInterface;
use Drupal\Component\Utility\Html;
class BootstrapPanel extends PreprocessBase implements PreprocessInterface {
protected function preprocessElement(Element $element, Variables $variables) {
$element
->map([
'id',
]);
$element
->addClass([
'form-item',
'js-form-item',
'form-wrapper',
'js-form-wrapper',
]);
$body = [];
$properties = [
'field_prefix',
'body',
'children',
];
if ($element
->isType([
'details',
'fieldset',
])) {
$properties[] = 'value';
}
$properties[] = 'field_suffix';
foreach ($properties as $property) {
$body[$property] = Element::create($element
->getProperty($property, []))
->getArray();
}
$variables['body'] = array_filter($body);
$map = [
'attributes' => 'attributes',
'body_attributes' => 'body_attributes',
'content_attributes' => 'body_attributes',
'description' => 'description',
'description_attributes' => 'description_attributes',
'description_display' => 'description_display',
'errors' => 'errors',
'footer' => 'footer',
'required' => 'required',
'panel_type' => 'panel_type',
'title' => 'heading',
'title_attributes' => 'heading_attributes',
];
if ($element
->isType('details')) {
$variables['collapsible'] = TRUE;
$variables['collapsed'] = !$element
->getProperty('open', TRUE);
$element
->removeAttribute('open');
}
elseif ($element
->isType('fieldset')) {
unset($variables['collapsible'], $variables['collapsed']);
$map['collapsed'] = 'collapsed';
$map['collapsible'] = 'collapsible';
}
$variables
->map($map);
}
protected function preprocessVariables(Variables $variables) {
$id = $variables
->getAttribute('id', Html::getUniqueId($variables
->offsetGet('id', 'bootstrap-panel')));
unset($variables['id']);
if ($variables['heading'] && $variables['collapsible']) {
if ($body_id = $variables
->getAttribute('id', "{$id}--content", 'body_attributes')) {
if ($variables['target'] = $variables
->offsetGet('target', "#{$body_id}")) {
$variables
->setAttributes([
'aria-controls' => preg_replace('/^#/', '', $variables['target']),
'aria-expanded' => !$variables['collapsed'] ? 'true' : 'false',
'aria-pressed' => !$variables['collapsed'] ? 'true' : 'false',
'data-toggle' => 'collapse',
'role' => 'button',
], 'heading_attributes');
}
}
}
$heading = $variables
->offsetGet('heading');
if ($heading && (is_string($heading) || $heading instanceof MarkupInterface)) {
$variables
->offsetSet('heading', [
'#markup' => $heading,
]);
}
if (!$variables
->offsetGet('panel_type')) {
$variables
->offsetSet('panel_type', 'default');
}
$this
->preprocessDescription();
$this
->preprocessAttributes();
}
}