block_classes.module in Block Classes 1.0.x
Adding classes to blocks.
File
block_classes.moduleView source
<?php
/**
* @file
* Adding classes to blocks.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\block\Entity\Block;
use Drupal\Component\Utility\Html;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\block\BlockInterface;
use Drupal\Core\Url;
/**
* Implements hook_help().
*/
function block_classes_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the block_classes module.
case 'help.page.block_classes':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t("Block Classes allows users to add classes to title, content of the Blocks.") . '</p>';
$output .= '<h3>' . t('Installation note') . '</h3>';
$output .= '<dl>';
$output .= '<dt>' . t('Enable the module on <a href=":extend_link">extend menu</a>.', [
':extend_link' => Url::fromRoute('system.modules_list')
->toString(),
]) . '</dt>';
$output .= '</dl>';
$output .= '<h3>' . t('Usage') . '</h3>';
$output .= '<dl>';
$output .= '<dt>' . t("To add a class to a block, simply visit that block's configuration page at Administration > Structure > Block Layout and click on Configure of the desired block.") . '</dt>';
$output .= '</dl>';
return $output;
}
}
/**
* Implements hook_ENTITY_TYPE_presave().
*/
function block_classes_block_presave(BlockInterface $entity) {
if (empty($entity
->getThirdPartySetting('block_classes', 'title_class'))) {
$entity
->unsetThirdPartySetting('block_classes', 'title_class');
}
if (empty($entity
->getThirdPartySetting('block_classes', 'content_class'))) {
$entity
->unsetThirdPartySetting('block_classes', 'content_class');
}
if (empty($entity
->getThirdPartySetting('block_classes', 'block_class'))) {
$entity
->unsetThirdPartySetting('block_classes', 'block_class');
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function block_classes_form_block_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if (\Drupal::currentUser()
->hasPermission('administer block css classes')) {
/** @var \Drupal\block\BlockInterface $block */
$block = $form_state
->getFormObject()
->getEntity();
// This will automatically be saved in the third party settings.
$form['third_party_settings']['#tree'] = TRUE;
$form['third_party_settings']['block_classes']['title_class'] = [
'#type' => 'textfield',
'#title' => t('Title CSS class(es)'),
'#default_value' => $block
->getThirdPartySetting('block_classes', 'title_class'),
'#maxlength' => 255,
];
$form['third_party_settings']['block_classes']['content_class'] = [
'#type' => 'textfield',
'#title' => t('Content CSS class(es)'),
'#default_value' => $block
->getThirdPartySetting('block_classes', 'content_class'),
'#maxlength' => 255,
];
$form['third_party_settings']['block_classes']['block_class'] = [
'#type' => 'textfield',
'#title' => t('Block CSS class(es)'),
'#description' => t('Customize the styling of this block by adding CSS classes. Separate multiple classes by spaces.'),
'#default_value' => $block
->getThirdPartySetting('block_classes', 'block_class'),
'#maxlength' => 255,
];
}
}
/**
* Implements hook_preprocess_HOOK().
*/
function block_classes_preprocess_block(&$variables) {
// Blocks coming from page manager widget does not have id.
if (!empty($variables['elements']['#id'])) {
$block = Block::load($variables['elements']['#id']);
if ($block && ($classes = $block
->getThirdPartySetting('block_classes', 'title_class'))) {
$classes_array = explode(' ', $classes);
foreach ($classes_array as $class) {
$variables['title_attributes']['class'][] = Html::cleanCssIdentifier($class, []);
}
}
if ($block && ($classes = $block
->getThirdPartySetting('block_classes', 'content_class'))) {
$classes_array = explode(' ', $classes);
foreach ($classes_array as $class) {
$variables['content_attributes']['class'][] = Html::cleanCssIdentifier($class, []);
}
}
if ($block && ($classes = $block
->getThirdPartySetting('block_classes', 'block_class'))) {
$classes_array = explode(' ', $classes);
foreach ($classes_array as $class) {
$variables['attributes']['class'][] = Html::cleanCssIdentifier($class, []);
}
}
}
}
Functions
Name![]() |
Description |
---|---|
block_classes_block_presave | Implements hook_ENTITY_TYPE_presave(). |
block_classes_form_block_form_alter | Implements hook_form_FORM_ID_alter(). |
block_classes_help | Implements hook_help(). |
block_classes_preprocess_block | Implements hook_preprocess_HOOK(). |