menu_block_title-8.x-1.x-dev/menu_block_title.module
menu_block_title.module
<?php
/**
* @file
* Alters menu block titles to show the active menu item's parent.
*/
use Drupal\block\Entity\Block;
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\menu_block_title\MenuBlockTitle;
/**
* Implements hook_help().
*/
function menu_block_title_help($route_name, RouteMatchInterface $route_match) {
// Main module help for the menu_block_title module.
if ($route_name == 'help.page.menu_block_title') {
$output = '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t("This module simply adds an option to system menu blocks which when active will show the active menu item's parent as a link in the block's title. This makes for useful sidebar menus where the block title is a link back to the current item's menu parent") . '</p>';
return $output;
}
}
/**
* Implements hook_block_view_alter().
*
* Alters the titles of specified menu blocks to set the title to the parent
* menu link title.
*/
function menu_block_title_block_view_alter(array &$build, BlockPluginInterface $block): void {
if (_menu_block_title_needs_modifying($build, $block)) {
$build['#pre_render'][] = [MenuBlockTitle::class, 'preRender'];
$menu_name = $block->getDerivativeId();
$build['#cache']['contexts'][] = 'route.menu_active_trails:' . $menu_name;
}
}
/**
* Helper function to determine whether a block needs modification.
*
* @param \Drupal\Core\Block\BlockPluginInterface $block
* The block entity.
*
* @return bool
* The boolean indicating whether this block needs modifying.
*/
function _menu_block_title_needs_modifying($build, BlockPluginInterface $block): bool {
$configuration = $block->getConfiguration();
if (isset($configuration['level']) && $block_config = Block::load($build['#id'])) {
$modify_title = $block_config->getThirdPartySetting('menu_block_title', 'modify_title');
return empty($modify_title) ? FALSE : $modify_title;
}
return FALSE;
}
/**
* Form alteration for the block form to add options.
*/
function menu_block_title_form_block_form_alter(&$form, FormStateInterface $form_state, $form_id): void {
/** @var \Drupal\block\BlockInterface $block */
$block = $form_state->getFormObject()->getEntity();
if (isset($form['settings']['menu_levels'])) {
// This will automatically be saved in the third party settings.
$form['third_party_settings']['#tree'] = TRUE;
$form['third_party_settings']['menu_block_title']['modify_title'] = [
'#type' => 'checkbox',
'#title' => t('Block title as menu link parent'),
'#description' => t('If checked the title of this block will display the parent of the active menu item.'),
'#default_value' => $block->getThirdPartySetting('menu_block_title', 'modify_title'),
];
}
}
