function block_styles_form_block_form_alter in Block Styles 2.x
Same name and namespace in other branches
- 8 block_styles.module \block_styles_form_block_form_alter()
- 1.x block_styles.module \block_styles_form_block_form_alter()
Implements hook_form_alter() adding administration for block layouts.
File
- ./
block_styles.module, line 39
Code
function block_styles_form_block_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
$entity = $form_state
->getFormObject()
->getEntity();
if (is_null($entity
->id())) {
return;
}
$style = _block_styles_get_style($entity
->id());
$styles_manager = \Drupal\styles_api\Style::stylePluginManager();
$styles = [];
$plugin_definitions = $styles_manager
->getDefinitions();
foreach ($plugin_definitions as $plugin_id => $plugin_definition) {
if ($plugin_definition['type'] == 'block') {
$styles[$plugin_id] = $plugin_definition['label'];
}
}
$disabled = TRUE;
$type = 'hidden';
$attributes = [
'style' => [
'display' => 'none',
],
];
$results = $form_state
->getValues();
if (isset($results['block_styles']['theme'])) {
if ($plugin_definitions[$results['block_styles']['theme']]['extras']['label']) {
$disabled = FALSE;
$type = 'textfield';
}
}
else {
if (!empty($plugin_definitions[$style['theme']]['extras']['label'])) {
$disabled = FALSE;
$type = 'textfield';
}
}
// Block styles
$form['block_styles'] = [
'#type' => 'fieldset',
'#title' => t('Block Styles Template'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
];
$form['block_styles']['theme'] = [
'#type' => 'select',
'#title' => t('Select block style'),
'#default_value' => $style['theme'],
'#empty_option' => t('None'),
'#options' => $styles,
'#description' => t('The selected layout template will be applied to block wrapper, replacing block.html.twig'),
'#ajax' => [
// When 'event' occurs, Drupal will perform an ajax request in the
// background. Usually the default value is sufficient (eg. change for
// select elements), but valid values include any jQuery event,
// most notably 'mousedown', 'blur', and 'submit'.
// 'event' => 'change',
'callback' => '_block_styles_theme_callback',
'wrapper' => 'text-replace',
],
];
$form['block_styles']['text'] = [
'#type' => $type,
'#title' => t('Text for button label'),
'#default_value' => $style['text'],
'#description' => t('Text to use on the button that show the block content.'),
'#size' => 60,
'#maxlength' => 64,
'#prefix' => '<div id="text-replace">',
'#suffix' => '</div>',
'#disabled' => $disabled,
];
$form['block_styles']['classes'] = [
'#type' => 'textfield',
'#title' => t('Add classes to block wrapper'),
'#default_value' => $style['classes'],
'#description' => t('Add classes for block wrapper, separated by spaces.'),
'#size' => 60,
'#maxlength' => 128,
];
$form['actions']['submit']['#submit'][] = '_block_styles_form_submit';
}