public function AllowedBlocksForm::ajaxSubmit in Layout Builder Restrictions 8.2
Callback function for AJAX form submission.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
File
- modules/
layout_builder_restrictions_by_region/ src/ Form/ AllowedBlocksForm.php, line 327
Class
- AllowedBlocksForm
- Provides form for designating allowed blocks.
Namespace
Drupal\layout_builder_restrictions_by_region\FormCode
public function ajaxSubmit(array &$form, FormStateInterface $form_state) {
$values = $form_state
->getValues();
$static_id = $values['static_id'];
$layout_plugin = $values['layout_plugin'];
$region_id = $values['region_id'];
$categories = $values['allowed_blocks'];
$block_restrictions = [];
if (!empty($categories)) {
foreach ($categories as $category => $category_setting) {
$restriction_type = $category_setting['restriction'];
$block_restrictions[$category]['restriction_type'] = $restriction_type;
if (in_array($restriction_type, [
'whitelisted',
'blacklisted',
])) {
foreach ($category_setting['allowed_blocks'] as $block_id => $block_setting) {
if ($block_setting == '1') {
// Include only checked blocks.
$block_restrictions[$category]['restrictions'][$block_id] = $block_setting;
}
}
}
}
}
// Write settings to tempStore.
$tempstore = $this->privateTempStoreFactory;
$store = $tempstore
->get('layout_builder_restrictions_by_region');
$store
->set($static_id . ':' . $layout_plugin . ':' . $region_id, $block_restrictions);
$response = new AjaxResponse();
if ($form_state
->getErrors()) {
// Could there ever be form errors?
// It's all checkboxes and radio buttons.
}
else {
$command = new CloseModalDialogCommand();
$response
->addCommand($command);
$this->messenger
->addWarning($this
->t('There is unsaved Layout Builder Restrictions configuration.'));
$status_messages = [
'#type' => 'status_messages',
];
$messages = $this->renderer
->renderRoot($status_messages);
$messages = '<div id="layout-builder-restrictions-messages">' . $messages . '</div>';
if (!empty($messages)) {
$response
->addCommand(new ReplaceCommand('#layout-builder-restrictions-messages', $messages));
}
$region_status = $this
->RegionRestrictionStatusString($layout_plugin, $region_id, $static_id, NULL);
$response
->addCommand(new ReplaceCommand('#restriction-status--' . $layout_plugin . '--' . $region_id . ' .data', '<span class="data">' . $region_status . '</span>'));
}
return $response;
}