diff --git a/block_class.module b/block_class.module index 7edd4f0..9e90cac 100644 --- a/block_class.module +++ b/block_class.module @@ -21,6 +21,22 @@ function block_class_block_presave(BlockInterface $entity) { } } +function block_class_form_layout_builder_configure_block_alter(&$form, FormStateInterface $form_state, $form_id) { + if (\Drupal::currentUser()->hasPermission('administer block classes')) { + /** @var \Drupal\layout_builder\SectionComponent $section_component */ + $section_component = $form_state->getFormObject()->getCurrentComponent(); + // We need to add the submit before save to ensure TPS to be saved. + array_unshift($form['#submit'], 'block_class_form_layout_builder_configure_block_submit'); + + $form['settings']['block_class_classes'] = [ + '#type' => 'textfield', + '#title' => t('CSS class(es)'), + '#description' => t('Customize the styling of this block by adding CSS classes. Separate multiple classes by spaces.'), + '#default_value' => $section_component->getThirdPartySetting('block_class', 'classes'), + ]; + } +} + /** * Implements hook_form_FORM_ID_alter(). */ @@ -59,6 +75,33 @@ function block_class_preprocess_block(&$variables) { } } +/** + * Implements hook_preprocess_HOOK(). + */ +function block_class_preprocess_layout(&$variables) { + $layout = $variables['layout'] ?? NULL; + if ($regions = $layout->getRegionNames()) { + foreach ($regions as $region_name) { + if (isset($variables['content'][$region_name])) { + _block_class_layout_builder_add_classes($variables, $region_name); + } + } + } +} + +/** + * Helper function for adding classes via layout builder. + */ +function _block_class_layout_builder_add_classes(&$variables, $region_name) { + foreach ($variables['content'][$region_name] as &$section_component) { + if (is_array($section_component) && isset($section_component['#block_class']['classes'])) { + $classes = explode(' ', $section_component['#block_class']['classes']); + $existing_classes = $section_component['#attributes']['class'] ?? []; + $section_component['#attributes']['class'] = array_merge($existing_classes, $classes); + } + } +} + /** * Implements hook_help(). */ @@ -83,3 +126,17 @@ function block_class_help($route_name, RouteMatchInterface $route_match) { return $output; } } + +/** + * Layout builder configure block submit. + */ +function block_class_form_layout_builder_configure_block_submit($form, FormStateInterface $form_state) { + /** @var \Drupal\layout_builder\SectionComponent $section_component */ + $section_component = $form_state->getFormObject()->getCurrentComponent(); + if ($classes = $form_state->getValue(['settings', 'block_class_classes'])) { + $section_component->setThirdPartySetting('block_class', 'classes', $classes); + } + else { + $section_component->unsetThirdPartySetting('block_class', 'classes'); + } +} diff --git a/block_class.services.yml b/block_class.services.yml new file mode 100644 index 0000000..cbe28b9 --- /dev/null +++ b/block_class.services.yml @@ -0,0 +1,6 @@ +services: + block_class.render_block_component_subscriber: + class: Drupal\block_class\EventSubscriber\BlockComponentRenderArray + arguments: [] + tags: + - { name: event_subscriber } diff --git a/src/EventSubscriber/BlockComponentRenderArray.php b/src/EventSubscriber/BlockComponentRenderArray.php new file mode 100644 index 0000000..3dcf64f --- /dev/null +++ b/src/EventSubscriber/BlockComponentRenderArray.php @@ -0,0 +1,36 @@ +getBuild(); + if (!empty($build)) { + $build['#block_class'] = $event->getComponent()->getThirdPartySettings('block_class'); + $event->setBuild($build); + } + } + +}