public function BootstrapLayoutsBase::buildConfigurationForm in Bootstrap Layouts 8.4
Same name and namespace in other branches
- 8.5 src/Plugin/Layout/BootstrapLayoutsBase.php \Drupal\bootstrap_layouts\Plugin\Layout\BootstrapLayoutsBase::buildConfigurationForm()
Form constructor.
Plugin forms are embedded in other forms. In order to know where the plugin form is located in the parent form, #parents and #array_parents must be known, but these are not available during the initial build phase. In order to have these properties available when building the plugin form's elements, let this method return a form element that has a #process callback and build the rest of the form in the callback. By the time the callback is executed, the element's #parents and #array_parents properties will have been set by the form API. For more documentation on #parents and #array_parents, see \Drupal\Core\Render\Element\FormElement.
Parameters
array $form: An associative array containing the initial structure of the plugin form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().
Return value
array The form structure.
Overrides LayoutBase::buildConfigurationForm
File
- src/
Plugin/ Layout/ BootstrapLayoutsBase.php, line 95
Class
- BootstrapLayoutsBase
- Layout class for all bootstrap layouts.
Namespace
Drupal\bootstrap_layouts\Plugin\LayoutCode
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$configuration = $this
->getConfiguration();
/** @var \Drupal\bootstrap_layouts\BootstrapLayoutsManager $bootstrap_layouts_manager */
$manager = \Drupal::getContainer()
->get('plugin.manager.bootstrap_layouts');
$classes = $manager
->getClassOptions();
$tokens = FALSE;
if (\Drupal::moduleHandler()
->moduleExists('token')) {
$tokens = [
'#title' => $this
->t('Tokens'),
'#type' => 'container',
];
$tokens['help'] = [
'#theme' => 'token_tree_link',
'#token_types' => 'all',
'#global_types' => FALSE,
'#dialog' => TRUE,
];
}
// Add wrappers.
$wrapper_options = [
'div' => 'Div',
'span' => 'Span',
'section' => 'Section',
'article' => 'Article',
'header' => 'Header',
'footer' => 'Footer',
'aside' => 'Aside',
'figure' => 'Figure',
];
$form['layout'] = [
'#type' => 'container',
'#tree' => TRUE,
];
$form['layout']['wrapper'] = [
'#type' => 'select',
'#title' => $this
->t('Wrapper'),
'#options' => $wrapper_options,
'#default_value' => $form_state
->getValue([
'layout',
'wrapper',
], $configuration['layout']['wrapper']),
];
$form['layout']['classes'] = [
'#type' => 'select',
'#title' => $this
->t('Classes'),
'#options' => $classes,
'#default_value' => $form_state
->getValue([
'layout',
'classes',
], $configuration['layout']['classes']) ?: [],
'#multiple' => TRUE,
];
$form['layout']['add_layout_class'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Add layout specific class: <code>@class</code>', [
'@class' => Html::cleanCssIdentifier($this
->getPluginId()),
]),
'#default_value' => (int) $form_state
->getValue([
'layout',
'add_layout_class',
], $configuration['layout']['add_layout_class']),
];
$form['layout']['attributes'] = [
'#type' => 'textfield',
'#title' => $this
->t('Additional attributes'),
'#description' => 'E.g. id|custom-id,role|navigation,data-something|some value',
'#default_value' => $form_state
->getValue([
'layout',
'attributes',
], $configuration['layout']['attributes']),
];
if ($tokens) {
$form['layout']['tokens'] = $tokens;
}
// Add each region's settings.
foreach ($this
->getRegionNames() as $region => $region_label) {
$default_values = NestedArray::mergeDeep($this
->getRegionDefaults(), isset($configuration['regions'][$region]) ? $configuration['regions'][$region] : [], $form_state
->getValue([
'regions',
$region,
], []));
$form[$region] = [
'#group' => 'additional_settings',
'#type' => 'details',
'#open' => TRUE,
'#title' => $this
->t('Region: @region', [
'@region' => $region_label,
]),
'#weight' => 20,
];
$form[$region]['wrapper'] = [
'#type' => 'select',
'#title' => $this
->t('Wrapper'),
'#options' => $wrapper_options,
'#default_value' => $default_values['wrapper'],
];
$form[$region]['classes'] = [
'#type' => 'select',
'#title' => $this
->t('Classes'),
'#options' => $classes,
'#default_value' => $default_values['classes'],
'#multiple' => TRUE,
];
$form[$region]['add_region_classes'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Add region specific classes: <code>bs-region</code> and <code>bs-region--@region</code>', [
'@region' => $region,
]),
'#default_value' => (int) $default_values['add_region_classes'],
];
$form[$region]['attributes'] = [
'#type' => 'textfield',
'#title' => $this
->t('Additional attributes'),
'#description' => 'E.g. id|custom-id,role|navigation,data-something|some value',
'#default_value' => $default_values['attributes'],
];
if ($tokens) {
$form[$region]['tokens'] = $tokens;
}
}
return $form;
}