View source
<?php
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\Component\Utility\Unicode;
function block_attributes_form_block_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$block = $form_state
->getFormObject()
->getEntity()
->getPlugin();
$config = $block
->getConfiguration();
$attributes = \Drupal::config('block_attributes.config')
->get('attributes') ?: [];
$form['settings']['attributes'] = [
'#type' => 'details',
'#title' => t('Attributes'),
'#tree' => TRUE,
];
$config_path = Url::fromRoute('block_attributes.config')
->toString();
$referrer_path = parse_url(\Drupal::request()->headers
->get('referer'))['path'];
$coming_from_config = $config_path == $referrer_path;
if ($coming_from_config) {
$form['settings']['attributes']['#open'] = TRUE;
}
$destination = \Drupal::destination()
->getAsArray();
$config_path = Url::fromRoute('block_attributes.config', [], [
'query' => $destination,
])
->toString();
if (count($attributes)) {
$form['settings']['attributes']['#description'] = '<small>' . t('Manage available attributes <a href="@config">here</a>.', [
'@config' => $config_path,
]) . '</small>';
}
else {
$form['settings']['attributes']['help'] = [
'#markup' => t('Manage available attributes <a href="@config">here</a>.', [
'@config' => $config_path,
]),
];
}
$autofocus = FALSE;
foreach ($attributes as $attribute => $info) {
if (!$info['label']) {
$info['label'] = str_replace('-', ' ', Unicode::ucfirst($attribute));
}
if (!$info['description']) {
$info['description'] = t('Enter value for <code>@attribute</code> attribute.', [
'@attribute' => $attribute,
]);
}
$type = !empty($info['options']) ? 'select' : 'textfield';
$form['settings']['attributes'][$attribute] = [
'#type' => $type,
'#title' => $info['label'],
'#description' => $info['description'],
'#default_value' => isset($config['attributes'][$attribute]) ? $config['attributes'][$attribute] : '',
];
if ($type == 'select') {
$form['settings']['attributes'][$attribute]['#empty_option'] = '- ' . t('Select value') . ' -';
$form['settings']['attributes'][$attribute]['#options'] = $info['options'];
}
if ($coming_from_config && !$autofocus) {
$form['settings']['attributes'][$attribute]['#attributes'] = [
'autofocus' => 'autofocus',
];
$autofocus = TRUE;
}
}
$form['actions']['submit']['#submit'][] = 'block_attributes_form_block_form_submit';
}
function block_attributes_form_block_form_submit($form, FormStateInterface $form_state) {
$block = $form_state
->getFormObject()
->getEntity();
$plugin = $block
->getPlugin();
$attributes = $form_state
->getValue('settings')['attributes'];
$plugin
->setConfigurationValue('attributes', $attributes);
$block
->save();
}
function block_attributes_preprocess_block(&$variables, $hook) {
if (isset($variables['configuration']['attributes'])) {
$attributes = $variables['configuration']['attributes'];
foreach ($attributes as $name => $value) {
if (isset($variables['attributes'][$name]) && is_array($variables['attributes'][$name])) {
$values = explode(' ', $value);
$values = array_map('trim', $values);
$values = array_filter($values, 'strlen');
$variables['attributes'][$name] = array_merge($variables['attributes'][$name], $values);
}
else {
$variables['attributes'][$name] = [
$value,
];
}
}
}
}