Skip to content
Snippets Groups Projects
Commit 33e02bec authored by HamzaDwaya's avatar HamzaDwaya
Browse files

Issue #3535705 by hamzadwaya: Convert issue 2999491 to a module that can be...

Issue #3535705 by hamzadwaya: Convert issue 2999491 to a module that can be used in any project and minimize support effrots
parent 252a02c6
No related branches found
No related tags found
No related merge requests found
......@@ -25,68 +25,93 @@ use Drupal\layout_builder\Plugin\Block\InlineBlock;
function layout_builder_reusable_blocks_form_layout_builder_add_block_alter(&$form, FormStateInterface $form_state) {
$component = $form_state->get('layout_builder__component');
// Only process if we're adding an InlineBlock
if ($component && $component->getPlugin() instanceof InlineBlock) {
// Load module configuration
// Early return if not dealing with an InlineBlock
if (!$component || !$component->getPlugin() instanceof InlineBlock) {
return;
}
$config = \Drupal::config('layout_builder_reusable_blocks.settings');
$make_all_blocks_reusable = $config->get('make_all_blocks_reusable') ?? FALSE;
$step_two = $form_state->get('layout_builder_reusable_step_two');
$make_reusable = $form_state->get('layout_builder_reusable_make_reusable');
// Handle different form states
if (!$step_two) {
if ($make_all_blocks_reusable) {
_layout_builder_reusable_blocks_auto_set_reusable($form, $form_state);
return;
}
_layout_builder_reusable_blocks_show_choice_buttons($form, $form_state);
}
elseif ($step_two && $make_reusable) {
_layout_builder_reusable_blocks_handle_reusable_block_form($form, $form_state);
}
}
// If configured to make all blocks reusable, skip the choice step
if ($make_all_blocks_reusable && !$form_state->get('layout_builder_reusable_step_two')) {
// Set flags to indicate we're creating a reusable block
/**
* Automatically sets the block to be reusable when configured to do so.
*/
function _layout_builder_reusable_blocks_auto_set_reusable(&$form, FormStateInterface $form_state) {
$form_state->set('layout_builder_reusable_make_reusable', TRUE);
$form_state->set('layout_builder_reusable_step_two', TRUE);
$form_state->setRebuild();
return;
// Add admin title field for reusable blocks
_layout_builder_reusable_blocks_handle_reusable_block_form($form, $form_state);
}
// Show choice buttons only if not configured to make all blocks reusable
// and we haven't already processed the choice
if (!$make_all_blocks_reusable && !$form_state->get('layout_builder_reusable_step_two')) {
// Remove the original submit button
/**
* Shows the choice buttons for inline vs reusable blocks.
*/
function _layout_builder_reusable_blocks_show_choice_buttons(&$form, FormStateInterface $form_state) {
// Remove the original submit button and change title
unset($form['actions']['submit']);
$form['#title'] = t('What type of block do you want to create?');
// Add inline block option button
$form['actions']['inline_block'] = [
'#type' => 'submit',
'#value' => t('Create inline block'),
'#submit' => ['layout_builder_reusable_blocks_add_block_submit_handler'],
'#limit_validation_errors' => [],
'#ajax' => [
// Define common AJAX settings
$ajax_settings = [
'callback' => 'layout_builder_reusable_blocks_ajax_callback',
'wrapper' => $form['#id'],
],
'#attributes' => ['data-layout-builder-reusable-action' => 'inline'],
];
// Add reusable block option button
$form['actions']['reusable_block'] = [
// Define common submit settings
$submit_settings = [
'#type' => 'submit',
'#value' => t('Create reusable block'),
'#submit' => ['layout_builder_reusable_blocks_add_block_submit_handler'],
'#limit_validation_errors' => [],
'#ajax' => [
'callback' => 'layout_builder_reusable_blocks_ajax_callback',
'wrapper' => $form['#id'],
],
'#ajax' => $ajax_settings,
];
// Add inline block option
$form['actions']['inline_block'] = $submit_settings + [
'#value' => t('Create inline block'),
'#attributes' => ['data-layout-builder-reusable-action' => 'inline'],
];
// Add reusable block option
$form['actions']['reusable_block'] = $submit_settings + [
'#value' => t('Create reusable block'),
'#attributes' => ['data-layout-builder-reusable-action' => 'reusable'],
];
// Add explanatory text for users
// Add explanatory text
$form['description'] = [
'#type' => 'html_tag',
'#tag' => 'p',
'#value' => t('Inline blocks are single-use and cannot be reused. Reusable blocks can be used in multiple places.'),
'#weight' => -10,
];
// Hide the main plugin form until a choice is made
$form['settings']['#access'] = FALSE;
}
}
// Add reusable block fields when step two is active and reusable is selected
if ($form_state->get('layout_builder_reusable_step_two') && $form_state->get('layout_builder_reusable_make_reusable')) {
/**
* Handles the form modifications for reusable blocks.
*/
function _layout_builder_reusable_blocks_handle_reusable_block_form(&$form, FormStateInterface $form_state) {
// Add admin title field for reusable blocks
$form['info'] = [
'#type' => 'textfield',
......@@ -101,7 +126,6 @@ function layout_builder_reusable_blocks_form_layout_builder_add_block_alter(&$fo
$form['actions']['submit']['#submit'][] = 'layout_builder_reusable_blocks_configure_block_submit_handler';
}
}
}
/**
* Custom submit handler for the first step of adding a block.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment