public function ReusableBlocks::alterForm in Panopoly Magic 8.2
Alters the layout builder add and update block forms.
Parameters
array $form: The form array.
\Drupal\Core\Form\FormStateInterface $form_state: The form state.
string $form_id: The form array.
Throws
\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
\Drupal\Component\Plugin\Exception\PluginNotFoundException
File
- src/
Alterations/ ReusableBlocks.php, line 86
Class
- ReusableBlocks
- A service for altering some Layout Builder forms to allow reusable blocks.
Namespace
Drupal\panopoly_magic\AlterationsCode
public function alterForm(array &$form, FormStateInterface $form_state, $form_id) {
$form_args = $form_state
->getBuildInfo()['args'];
/** @var \Drupal\layout_builder\SectionStorageInterface $section_storage */
$section_storage = $form_args[0];
$delta = $form_args[1];
if ($form_id === 'layout_builder_add_block') {
/** @var \Drupal\layout_builder\SectionComponent $component */
$component = $form_state
->get('layout_builder__component');
$uuid = $component
->getUuid();
}
else {
$uuid = $form_args[3];
$component = $section_storage
->getSection($delta)
->getComponent($uuid);
}
// Store some properties for us to access later.
$form_state
->set('panopoly_magic__component', $component);
$form_state
->set('panopoly_magic__section_storage', $section_storage);
$form_state
->set('panopoly_magic__delta', $delta);
$form_state
->set('panopoly_magic__uuid', $uuid);
/** @var \Drupal\Core\Block\BlockPluginInterface $block */
$block = $component
->getPlugin();
if ($block
->getBaseId() === 'block_content') {
// Show the block content form here.
/** @var \Drupal\block_content\Plugin\Derivative\BlockContent[] $block_contents */
$block_contents = $this->entityTypeManager
->getStorage('block_content')
->loadByProperties([
'uuid' => $block
->getDerivativeId(),
]);
if (count($block_contents) === 1) {
$form['messages'] = [
'#theme' => 'status_messages',
'#message_list' => [
'warning' => [
$this
->t("This block is reusable! Any changes made will be applied globally."),
],
],
];
$form['block_form'] = [
'#type' => 'container',
'#process' => [
[
static::class,
'processBlockContentForm',
],
],
'#block' => reset($block_contents),
'#access' => $this->currentUser
->hasPermission('create and edit custom blocks'),
];
$form['#validate'][] = [
static::class,
'blockContentValidate',
];
$form['#submit'][] = [
static::class,
'blockContentSubmit',
];
}
}
elseif ($block
->getBaseId() === 'inline_block') {
/** @var \Drupal\block_content\BlockContentInterface $block_content */
$block_content = $form['settings']['block_form']['#block'];
$form['reusable'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Reusable'),
'#description' => $this
->t('Would you like to be able to reuse this block? This option can not be changed after saving.'),
'#default_value' => $block_content
->isReusable(),
];
$form['info'] = [
'#type' => 'textfield',
'#title' => $this
->t('Admin title'),
'#description' => $this
->t('The title used to find and reuse this block later.'),
'#states' => [
'visible' => [
':input[name="reusable"]' => [
'checked' => TRUE,
],
],
],
];
$form['#submit'][] = [
static::class,
'staticInlineBlockSubmit',
];
}
$form['actions']['#weight'] = 100;
}