1

I have a views block with id=block-views-block-services-block-1 which I used in the menu link.

The menu link is now shown as https://example.com/gb/#block-views-block-services-block-1

As you can see it's really not looking nice with long hyphend words. Is there any easy way to change it to something like this https://example.com/gb/#services

I read an answer for Drupal 7 where we need to edit template.php to change the ID. But in Drupal 8 it seems that template.php is replaced with twig.html. I'm not very sure as I'm new to Drupal.

Could anyone please explain few steps on how to do it?

2

you should place this function in your themes/YOURTHEME/YOURTHEME.theme file:

function YOURTHEME_preprocess_block(&$variables) {
  dump($variables);
}

and you will see the name of your block. You have to look for block_html_id. Based on your description the value of the block_html_id will be block-views-block-services-block-1.

Then you can simply owerdefine it this way:

function YOURTHEME_preprocess_block(&$variables) {
  if ($variables['block_html_id'] == "block-views-block-services-block-1") {
    $variables['block_html_id'] = "services";
  }
}

Or you can create a custom module and you can put this function there too. In that case you have to replace the YOURTHEME to the name of the custom module.

  • I cannot get this to work in Bootstrap theme. I have cleared cache several times, but the ID never changes. function bootstrap_mytheme_preprocess_block(&$variables) { if ($variables['block_html_id'] == "block-views-block-homeblocks-block-1") { $variables['block_html_id'] = "about"; } } – hockey2112 May 20 at 20:59
  • Are you sure that this bootstrap_mytheme_preprocess_block function is called? What do you see if you dump the $variables before the condition? – Zoltán Süle May 21 at 8:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.