Skip to content

Instantly share code, notes, and snippets.

@ruuds
Created May 19, 2023 12:23
Show Gist options
  • Save ruuds/5163d887431862d83f9f6d4d3a592939 to your computer and use it in GitHub Desktop.
Save ruuds/5163d887431862d83f9f6d4d3a592939 to your computer and use it in GitHub Desktop.
<?php
/**
* @file
* Block button modal module file.
*/
use Drupal\Core\Form\FormStateInterface;
/**
* Implements hook_form_BASE_FORM_ID_alter().
*/
function block_button_modal_form_block_form_alter(&$form, FormStateInterface $form_state, $form_id) {
/**
* @var \Drupal\block\BlockInterface $block
*/
$block = $form_state->getBuildInfo()['callback_object']->getEntity();
$form['third_party_settings']['block_button_modal']['enabled'] = [
'#type' => 'checkbox',
'#title' => t('Show block as modal dialog'),
'#description' => t('Show a button instead of the actual block. When the button is clicked, the block will show in a modal dialog.'),
'#default_value' => $block->getThirdPartySetting('block_button_modal', 'enabled', FALSE),
];
}
/**
* Implements hook_ENTITY_TYPE_view_alter().
*/
function block_button_modal_block_view_alter(array &$build, $entity, $display) {
echo "block_button_modal checkpoint 1<br>\n";
if (!isset($build['#configuration']['third_party_settings']['block_button_modal']) || !$build['#configuration']['third_party_settings']['block_button_modal']['enabled']) {
return;
}
echo "block_button_modal checkpoint 2<br>\n";
$build['#theme'] = 'block_button_modal_block';
$build['#block_label'] = $build['#configuration']['label'];
}
/**
* Implements hook_theme().
*/
function block_button_modal_theme($existing, $type, $theme, $path) {
return [
'block_button_modal_block' => [
'render element' => 'elements',
],
];
}
/**
* Implements hook_preprocess_HOOK().
*/
function template_preprocess_block_button_modal_block(&$variables) {
echo "block_button_modal checkpoint 3<br>\n";
template_preprocess_block($variables);
$variables['block_button_modal_block_id'] = str_replace('_', '-', 'block_button_modal_block_' . $variables['elements']['#id']);
$variables['block_label'] = $variables['elements']['#block_label'];
$variables['#attached']['library'][] = 'block_button_modal/block_button_modal_block';
$variables['block_button_modal_button'] = [
'#type' => 'button',
'#button_type' => 'block_button_modal',
'#value' => $variables['elements']['#block_label'],
'#block_button_modal_block_id' => $variables['block_button_modal_block_id'],
'#attributes' => [
'type' => 'button',
'data-block-button-modal-title' => $variables['elements']['#block_label'],
],
];
}
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function block_button_modal_theme_suggestions_block_button_modal_block_alter(array &$suggestions, array $variables) {
$suggestions[] = 'block_button_modal_block__' . $variables['elements']['#id'];
}
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function block_button_modal_theme_suggestions_input_alter(array &$suggestions, array $variables) {
if (isset($variables['element']['#button_type']) && $variables['element']['#button_type'] === 'block_button_modal') {
$suggestions[] = 'input__block_button_modal';
$suggestions[] = 'input__' . $variables['element']['#block_button_modal_block_id'];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment