diff --git a/config/schema/menu_block.schema.yml b/config/schema/menu_block.schema.yml index 0112779..b652b8a 100644 --- a/config/schema/menu_block.schema.yml +++ b/config/schema/menu_block.schema.yml @@ -8,6 +8,9 @@ block.settings.menu_block:*: follow_parent: type: string label: 'Set initial visibility level to the active menu item, or its children' + label_link: + type: boolean + label: 'If the block label should be a link' label_type: type: string label: 'Label type' diff --git a/menu_block.install b/menu_block.install index bc273f9..bdb6b4c 100644 --- a/menu_block.install +++ b/menu_block.install @@ -5,6 +5,8 @@ * Install, update and uninstall functions for the Menu Block module. */ +use Drupal\menu_block\Plugin\Block\MenuBlock; + /** * Issue #2932048: Config schema mismatch for expand(ed). */ @@ -12,3 +14,22 @@ function menu_block_update_8101() { $config_factory = \Drupal::configFactory(); $config_factory->rename('expanded', 'expand'); } + +/** + * Add default config. + */ +function menu_block_update_8102() { + $config_factory = \Drupal::configFactory(); + foreach ($config_factory->listAll('block.block.') as $block_config_name) { + $block = $config_factory->getEditable($block_config_name); + $settings = $block->get('settings'); + // Only update system_menu_block config. + if (strpos($settings['id'], 'system_menu_block:') === 0) { + // Set default config for existing menu block config. + $settings['label_link'] ?? FALSE; + $settings['label_type'] ?? MenuBlock::LABEL_BLOCK; + $block->set('settings', $settings) + ->save(TRUE); + } + } +} diff --git a/src/Plugin/Block/MenuBlock.php b/src/Plugin/Block/MenuBlock.php index 82d96f5..4f37a17 100644 --- a/src/Plugin/Block/MenuBlock.php +++ b/src/Plugin/Block/MenuBlock.php @@ -4,8 +4,10 @@ namespace Drupal\menu_block\Plugin\Block; use Drupal\Core\Access\AccessResult; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Link; use Drupal\Core\Menu\MenuParentFormSelectorInterface; use Drupal\Core\Menu\MenuTreeParameters; +use Drupal\Core\Render\Markup; use Drupal\Core\Session\AccountInterface; use Drupal\system\Entity\Menu; use Drupal\system\Plugin\Block\SystemMenuBlock; @@ -114,6 +116,23 @@ class MenuBlock extends SystemMenuBlock { ], ]; + $form['advanced']['label_link'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Link the title?'), + '#default_value' => $config['label_link'], + '#states' => [ + 'visible' => [ + ':input[name="settings[label_display]"]' => ['checked' => TRUE], + ':input[name="settings[label_type]"]' => [ + ['value' => self::LABEL_ACTIVE_ITEM], + ['value' => self::LABEL_PARENT], + ['value' => self::LABEL_ROOT], + ['value' => self::LABEL_FIXED], + ], + ], + ], + ]; + $form['style'] = [ '#type' => 'details', '#title' => $this->t('HTML and style options'), @@ -190,6 +209,7 @@ class MenuBlock extends SystemMenuBlock { $this->configuration['parent'] = $form_state->getValue('parent'); $this->configuration['suggestion'] = $form_state->getValue('suggestion'); $this->configuration['label_type'] = $form_state->getValue('label_type'); + $this->configuration['label_link'] = $form_state->getValue('label_link'); } /** @@ -346,6 +366,7 @@ class MenuBlock extends SystemMenuBlock { 'parent' => $this->getDerivativeId() . ':', 'suggestion' => strtr($this->getDerivativeId(), '-', '_'), 'label_type' => self::LABEL_BLOCK, + 'label_link' => FALSE, ]; } @@ -492,6 +513,10 @@ class MenuBlock extends SystemMenuBlock { $menu = $this->menuTree->load($this->getDerivativeId(), $parameters); $link = $this->findLinkInTree($menu, $link_id); if ($link) { + if ($this->configuration['label_link']) { + $block_link = Link::fromTextAndUrl($link->link->getTitle(), $link->link->getUrlObject())->toString(); + return Markup::create($block_link); + } return $link->link->getTitle(); } } diff --git a/tests/src/Functional/MenuBlockTest.php b/tests/src/Functional/MenuBlockTest.php index 0f68dda..ae38e95 100644 --- a/tests/src/Functional/MenuBlockTest.php +++ b/tests/src/Functional/MenuBlockTest.php @@ -469,6 +469,7 @@ class MenuBlockTest extends BrowserTestBase { 'id' => $block_id, 'settings[label]' => 'Block title', 'settings[label_display]' => TRUE, + 'settings[label_link]' => FALSE, 'settings[parent]' => 'main:' . $this->links['child-1'], 'region' => 'primary_menu', ], 'Save block'); @@ -486,6 +487,11 @@ class MenuBlockTest extends BrowserTestBase { 'option' => MenuBlock::LABEL_FIXED, 'title' => 'child-1 menu item', ], + 'fixed menu item as link' => [ + 'option' => MenuBlock::LABEL_FIXED, + 'title' => 'child-1 menu item', + 'label_link' => TRUE, + ], 'fixed menu item parent' => [ 'option' => MenuBlock::LABEL_FIXED, 'title' => 'child-1 menu item', @@ -495,10 +501,20 @@ class MenuBlockTest extends BrowserTestBase { 'option' => MenuBlock::LABEL_ACTIVE_ITEM, 'title' => 'child-1-1 menu item', ], + 'active item as link' => [ + 'option' => MenuBlock::LABEL_ACTIVE_ITEM, + 'title' => 'child-1-1 menu item', + 'label_link' => TRUE, + ], 'parent item' => [ 'option' => MenuBlock::LABEL_PARENT, 'title' => 'child-1 menu item', ], + 'parent item as link' => [ + 'option' => MenuBlock::LABEL_PARENT, + 'title' => 'child-1 menu item', + 'label_link' => TRUE, + ], 'parent item top level' => [ 'option' => MenuBlock::LABEL_PARENT, 'title' => 'parent menu item', @@ -518,6 +534,11 @@ class MenuBlockTest extends BrowserTestBase { 'option' => MenuBlock::LABEL_ROOT, 'title' => 'parent menu item', ], + 'menu root as link' => [ + 'option' => MenuBlock::LABEL_ROOT, + 'title' => 'parent menu item', + 'label_link' => TRUE, + ], 'menu root 2' => [ 'option' => MenuBlock::LABEL_ROOT, 'title' => 'parent menu item', @@ -538,9 +559,12 @@ class MenuBlockTest extends BrowserTestBase { foreach ($options as $case_id => $option) { // The 'label_display' setting should be TRUE if not defined explicitly. $label_display = $option['label_display'] ?? TRUE; + // The 'label_link' setting should default to FALSE. + $label_link = $option['label_link'] ?? FALSE; $this->drupalPostForm('admin/structure/block/manage/main', [ 'settings[label_type]' => $option['option'], 'settings[label_display]' => $label_display, + 'settings[label_link]' => $label_link, ], 'Save block'); $test_link = empty($option['test_link']) ? 'menu-block-test/hierarchy/parent/child-1/child-1-1' : $option['test_link']; $this->drupalGet($test_link); @@ -553,6 +577,13 @@ class MenuBlockTest extends BrowserTestBase { // and invert it to determine if the block title is visible or not. $visible = !$block_label->hasClass('visually-hidden'); $this->assertEquals($label_display, $visible, "Test case '$case_id' should have the right visibility."); + + if ($label_link) { + $this->assertStringContainsString('<a href="', $block_label->getHtml(), "Test case '$case_id' should have a link in the block title."); + } + else { + $this->assertStringNotContainsString('<a href="', $block_label->getHtml(), "Test case '$case_id' should not have a link in the block title."); + } } }