X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;ds=sidebyside;f=web%2Fmodules%2Fcontrib%2Fblock_class%2Fblock_class.module;fp=web%2Fmodules%2Fcontrib%2Fblock_class%2Fblock_class.module;h=b3afb939f5fce485e0ac41e52443eee58ebc7e25;hb=059867c3f96750652c80f39e44c442a58c2549ee;hp=c43fde2fb1116469de101bbeacf274f64476d800;hpb=f8fc16ae6b862bef59baaad5d051dd37b7ff11b2;p=yaffs-website diff --git a/web/modules/contrib/block_class/block_class.module b/web/modules/contrib/block_class/block_class.module index c43fde2fb..b3afb939f 100644 --- a/web/modules/contrib/block_class/block_class.module +++ b/web/modules/contrib/block_class/block_class.module @@ -2,15 +2,28 @@ /** * @file - * Module for adding classes to blocks. + * 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; + +/** + * Implements hook_ENTITY_TYPE_presave(). + */ +function block_class_block_presave(BlockInterface $entity) { + if (empty($entity->getThirdPartySetting('block_class', 'classes'))) { + $entity->unsetThirdPartySetting('block_class', 'classes'); + } +} /** * Implements hook_form_FORM_ID_alter(). */ -function block_class_form_block_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) { +function block_class_form_block_form_alter(&$form, FormStateInterface $form_state, $form_id) { if (\Drupal::currentUser()->hasPermission('administer block classes')) { /** @var \Drupal\block\BlockInterface $block */ @@ -18,12 +31,12 @@ function block_class_form_block_form_alter(&$form, \Drupal\Core\Form\FormStateIn // This will automatically be saved in the third party settings. $form['third_party_settings']['#tree'] = TRUE; - $form['third_party_settings']['block_class']['classes'] = array( + $form['third_party_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' => $block->getThirdPartySetting('block_class', 'classes'), - ); + ]; } } @@ -35,8 +48,36 @@ function block_class_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 ($classes = $block->getThirdPartySetting('block_class', 'classes')) { - $variables['attributes']['class'][] = $classes; + if ($block && $classes = $block->getThirdPartySetting('block_class', 'classes')) { + $classes_array = explode(' ', $classes); + foreach ($classes_array as $class) { + $variables['attributes']['class'][] = Html::cleanCssIdentifier($class, []); + } } } } + +/** + * Implements hook_help(). + */ +function block_class_help($route_name, RouteMatchInterface $route_match) { + switch ($route_name) { + // Main module help for the forms_to_email module. + case 'help.page.block_class': + $output = ''; + $output .= '

' . t('About') . '

'; + $output .= '

' . t("Block Class allows users to add classes to any block through the block's configuration interface. Hooray for more powerful block theming!") . '

'; + + $output .= '

' . t('Installation note') . '

'; + $output .= '
'; + $output .= '
' . t('Enable the module on extend menu.', [':extend_link' => \Drupal::url('system.modules_list')]) . '
'; + $output .= '
'; + + $output .= '

' . t('Usage') . '

'; + $output .= '
'; + $output .= '
' . 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.") . '
'; + $output .= '
'; + + return $output; + } +}