Problem/Motivation

When placing block on a page, you can set the block machine name. But the configuration of this block depends on the theme you place your block in.

On the same websites, if there are multiple themes, you can't place the same block on another theme with the same machine name because it is already used.

Proposed resolution

A best practice would be to prefix block machine name by the machine name of the theme it is in.

For example: block.block.bartik_help.yml

Remaining tasks

Subsequent tasks

Comments

Grimreaper created an issue. See original summary.

grimreaper’s picture

Assigning the issue to myself, I will delegate it to beginners in my company to mentor them.

grimreaper’s picture

Issue summary: View changes
grimreaper’s picture

Issue tags: +DevDaysTransylvania

Tagging.

ithom’s picture

Assigned: grimreaper » Unassigned
Status: Active » Needs review
Issue tags: +Smile Drupal contribution tour 2019
StatusFileSize
new17.98 KB

Hello here is a patch for this issue.

Thanks for the review.

Status: Needs review » Needs work
ithom’s picture

Status: Needs work » Needs review
StatusFileSize
new19.16 KB

Hello,

Here is the fixed patch for the issue.

Thanks for the review.

joachim’s picture

Status: Needs review » Needs work

This patch looks like it's only tests so far. I'm not seeing anything that implements the change the issue summary talks about.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

grimreaper’s picture

Assigned: Unassigned » grimreaper
Issue tags: +DrupalCon Amsterdam 2019
grimreaper’s picture

Status: Needs work » Needs review
StatusFileSize
new18.32 KB
new4.28 KB

With this new patch, we also have "Alter the autogenerated machine name in the form to add a block to prefix automatically by the theme machine name (this issue)".

Status: Needs review » Needs work
rachel_norfolk’s picture

Issue tags: -DrupalCon Amsterdam 2019 +Amsterdam2019

retagging

grimreaper’s picture

Status: Needs work » Needs review
StatusFileSize
new28.33 KB
new9.7 KB

Fixing tests.

grimreaper’s picture

Assigned: grimreaper » Unassigned

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

nod_’s picture

In Umami profile the blocks are prefixed with the theme name: umami_branding for exemple

nod_’s picture

Status: Needs review » Needs work
+++ b/core/modules/block/src/BlockForm.php
@@ -146,7 +146,7 @@ public function form(array $form, FormStateInterface $form_state) {
-      '#default_value' => !$entity->isNew() ? $entity->id() : $this->getUniqueMachineName($entity),
+      '#default_value' => !$entity->isNew() ? $entity->id() : $this->getUniqueMachineName($entity, $theme),

@@ -405,12 +405,15 @@ protected function submitVisibility(array $form, FormStateInterface $form_state)
-  public function getUniqueMachineName(BlockInterface $block) {
+  public function getUniqueMachineName(BlockInterface $block, $theme) {
...
+    $suggestion = $theme . '_' . $suggestion;

This is the actual change made by the patch, the rest is updating tests. There is an issue when a theme is not preselected when adding the block.

If you go to /en/admin/structure/block/add/local_tasks_block/, the machine name has the default theme in the name but when you use the select to change the theme (and region) the machine name is not updated.

To be the least problematic we could do:

    $suggestion = $block->getPlugin()->getMachineNameSuggestion();
    if ($block->getTheme()) {
      $suggestion = $block->getTheme() . '_' . $suggestion;
    }

That would avoid changing the signature of a public method as an added bonus.

pavnish’s picture

Assigned: Unassigned » pavnish

@nod_ I am working on it.

pavnish’s picture

Assigned: pavnish » Unassigned
Status: Needs work » Needs review
StatusFileSize
new29.52 KB
new10.3 KB

@nod_ Hi i am very happy to working with you.I have changed the patch as suggest by you in #18
Could you please review .
Thanks
Pavnish

Status: Needs review » Needs work

The last submitted patch, 20: 3061266-20.patch, failed testing. View results

pavnish’s picture

Assigned: Unassigned » pavnish

Checking failed test cases

pavnish’s picture

Assigned: pavnish » Unassigned
Status: Needs work » Needs review
StatusFileSize
new29.07 KB
pavnish’s picture

Assigned: Unassigned » pavnish
nod_’s picture

Assigned: pavnish » Unassigned
Issue tags: +Needs reroll
hardik_patel_12’s picture

StatusFileSize
new23.2 KB
ramya balasubramanian’s picture

Status: Needs review » Needs work
ridhimaabrol24’s picture

Status: Needs work » Needs review
StatusFileSize
new23.2 KB
new751 bytes

Fixing PHP lint error.

Status: Needs review » Needs work

The last submitted patch, 28: 3061266-28.patch, failed testing. View results

nod_’s picture

Issue tags: -Needs reroll
  1. +++ b/core/modules/block/src/BlockForm.php
    @@ -388,12 +388,17 @@ protected function submitVisibility(array $form, FormStateInterface $form_state)
    +   * @param string $theme
    +   *   The theme machine name in which the block is placed.
    

    This needs to be removed, the parameter doesn't exists anymore

  2. +++ b/core/modules/block/tests/src/Unit/BlockFormTest.php
    @@ -130,7 +130,7 @@ public function testGetUniqueMachineName() {
    @@ -140,18 +140,18 @@ public function testGetUniqueMachineName() {
    
    @@ -140,18 +140,18 @@ public function testGetUniqueMachineName() {
    -    $this->assertEquals('test_2', $block_form_controller->getUniqueMachineName($blocks['test']));
    +    $this->assertEquals('theme_machine_name_test_2', $block_form_controller->getUniqueMachineName($blocks['test']));
    ...
    -    $this->assertEquals('other_test_3', $block_form_controller->getUniqueMachineName($blocks['other_test']));
    -    $this->assertEquals('other_test_3', $block_form_controller->getUniqueMachineName($blocks['other_test_1']));
    -    $this->assertEquals('other_test_3', $block_form_controller->getUniqueMachineName($blocks['other_test_2']));
    +    $this->assertEquals('theme_machine_name_other_test_3', $block_form_controller->getUniqueMachineName($blocks['other_test']));
    +    $this->assertEquals('theme_machine_name_other_test_3', $block_form_controller->getUniqueMachineName($blocks['other_test_1']));
    +    $this->assertEquals('theme_machine_name_other_test_3', $block_form_controller->getUniqueMachineName($blocks['other_test_2']));
    ...
    -    $this->assertEquals('last_test', $block_form_controller->getUniqueMachineName($last_block));
    +    $this->assertEquals('theme_machine_name_last_test', $block_form_controller->getUniqueMachineName($last_block));
    

    This is not necessary, the test works fine as-is.

ridhimaabrol24’s picture

Assigned: Unassigned » ridhimaabrol24
ridhimaabrol24’s picture

Assigned: ridhimaabrol24 » Unassigned
Status: Needs work » Needs review
StatusFileSize
new20.64 KB
new2.51 KB

Hi @nod_
Thanks for the feedback. implemented the feedback. Kindly review.
Thanks

nod_’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me

catch’s picture

Title: Prefix block machine name by the theme machine name » Prefix block machine name suggestions with the theme machine name
Status: Reviewed & tested by the community » Needs work

The issue title makes this look like more of a change than it actually is. Looks sensible to me and reduces the chance for collisions.

However, this fails cspell:

home/catch/www/drupal/core/modules/views/tests/fixtures/update/block.block.bartik_exposedformtest_exposed_blockpage_1.yml:10:12 - Unknown word (exposedformtest)
/home/catch/www/drupal/core/modules/views/tests/fixtures/update/block.block.bartik_exposedformtest_exposed_blockpage_1.yml:10:36 - Unknown word (blockpage)
nod_’s picture

Status: Needs work » Needs review
StatusFileSize
new16.3 KB

I didn't see how that config was used, I removed it and the few tests I ran still worked, let's see what testbot says.

Status: Needs review » Needs work

The last submitted patch, 35: core-3061266-35.patch, failed testing. View results

nod_’s picture

Status: Needs work » Needs review
StatusFileSize
new20.65 KB

can't replicate the failures locally... adding back the config and renamed it.

nod_’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new1.24 KB

so... load bearing config file huh.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
Related issues: +#2043527: Theme name is included in block machine name but should be stored as a key instead

I'm pretty sure it used to work this way and we undid it for reasons. So yeah this leads me to #2043527: Theme name is included in block machine name but should be stored as a key instead where we kinda of did the opposite of this. But that was a bit different because this is about the id and not the dot structure of the config name. I think this change is okay but I've pinged @tim.plunkett to gather other thoughts.

Another good thing is that block_theme_initialize() supports this way of naming blocks so that makes sense.

  1. +++ b/core/modules/block/tests/src/Functional/Views/DisplayBlockTest.php
    similarity index 95%
    rename from core/modules/block_content/tests/modules/block_content_test/config/install/block.block.foobargorilla.yml
    
    rename from core/modules/block_content/tests/modules/block_content_test/config/install/block.block.foobargorilla.yml
    rename to core/modules/block_content/tests/modules/block_content_test/config/install/block.block.classy_foobargorilla.yml
    
    +++ b/core/modules/block_content/tests/modules/block_content_test/config/install/block.block.classy_foobargorilla.yml
    @@ -5,7 +5,7 @@ dependencies:
    -id: foobargorilla
    +id: classy_foobargorilla
    

    Why have we changed this config - we're only changing a suggestion - we don;'t have to have a theme name here and many existing sites won't so let's not make it look like it is enforced.

  2. +++ b/core/modules/block_content/tests/src/Functional/BlockContentCreationTest.php
    similarity index 91%
    rename from core/modules/config/tests/config_override_integration_test/config/install/block.block.config_override_test.yml
    
    rename from core/modules/config/tests/config_override_integration_test/config/install/block.block.config_override_test.yml
    rename to core/modules/config/tests/config_override_integration_test/config/install/block.block.classy_config_override_test.yml
    
    rename to core/modules/config/tests/config_override_integration_test/config/install/block.block.classy_config_override_test.yml
    index be0616ff50..250010bf66 100644
    
    index be0616ff50..250010bf66 100644
    --- a/core/modules/config/tests/config_override_integration_test/config/install/block.block.config_override_test.yml
    
    --- a/core/modules/config/tests/config_override_integration_test/config/install/block.block.config_override_test.yml
    +++ b/core/modules/config/tests/config_override_integration_test/config/install/block.block.classy_config_override_test.yml
    
    +++ b/core/modules/config/tests/config_override_integration_test/config/install/block.block.classy_config_override_test.yml
    +++ b/core/modules/config/tests/config_override_integration_test/config/install/block.block.classy_config_override_test.yml
    @@ -1,4 +1,4 @@
    
    @@ -1,4 +1,4 @@
    -id: config_override_test
    +id: classy_config_override_test
    
    +++ b/core/modules/config/tests/config_override_integration_test/src/CacheabilityMetadataConfigOverride.php
    @@ -19,9 +19,9 @@ public function loadOverrides($names) {
         $state = \Drupal::state()->get('config_override_integration_test.enabled', FALSE);
    -    if (in_array('block.block.config_override_test', $names) && $state !== FALSE) {
    +    if (in_array('block.block.classy_config_override_test', $names) && $state !== FALSE) {
           $overrides = $overrides + [
    -        'block.block.config_override_test' => [
    +        'block.block.classy_config_override_test' => [
               'settings' => ['label' => 'Overridden block label'],
             ],
           ];
    @@ -49,7 +49,7 @@ public function createConfigObject($name, $collection = StorageInterface::DEFAUL
    
    @@ -49,7 +49,7 @@ public function createConfigObject($name, $collection = StorageInterface::DEFAUL
        */
       public function getCacheableMetadata($name) {
         $metadata = new CacheableMetadata();
    -    if ($name === 'block.block.config_override_test') {
    +    if ($name === 'block.block.classy_config_override_test') {
    
    similarity index 94%
    rename from core/modules/config/tests/config_override_test/config/install/block.block.call_to_action.yml
    
    rename from core/modules/config/tests/config_override_test/config/install/block.block.call_to_action.yml
    rename to core/modules/config/tests/config_override_test/config/install/block.block.classy_call_to_action.yml
    
    rename to core/modules/config/tests/config_override_test/config/install/block.block.classy_call_to_action.yml
    index 8951c0d22b..e6b80a6efd 100644
    
    index 8951c0d22b..e6b80a6efd 100644
    --- a/core/modules/config/tests/config_override_test/config/install/block.block.call_to_action.yml
    
    --- a/core/modules/config/tests/config_override_test/config/install/block.block.call_to_action.yml
    +++ b/core/modules/config/tests/config_override_test/config/install/block.block.classy_call_to_action.yml
    
    +++ b/core/modules/config/tests/config_override_test/config/install/block.block.classy_call_to_action.yml
    +++ b/core/modules/config/tests/config_override_test/config/install/block.block.classy_call_to_action.yml
    @@ -5,7 +5,7 @@ dependencies:
    
    @@ -5,7 +5,7 @@ dependencies:
         - block_content
       theme:
         - classy
    -id: call_to_action
    +id: classy_call_to_action
    
    +++ b/core/modules/config/tests/config_override_test/src/PirateDayCacheabilityMetadataConfigOverride.php
    @@ -23,9 +23,9 @@ public function loadOverrides($names) {
    -      if (in_array('block.block.call_to_action', $names)) {
    +      if (in_array('block.block.classy_call_to_action', $names)) {
             $overrides = $overrides + [
    -          'block.block.call_to_action' => [
    +          'block.block.classy_call_to_action' => [
    
    +++ b/core/modules/layout_builder/tests/src/FunctionalJavascript/FieldBlockTest.php
    similarity index 90%
    rename from core/modules/locale/tests/modules/locale_test/config/optional/block.block.test_default_config.yml
    
    rename from core/modules/locale/tests/modules/locale_test/config/optional/block.block.test_default_config.yml
    rename to core/modules/locale/tests/modules/locale_test/config/optional/block.block.classy_test_default_config.yml
    
    +++ b/core/modules/locale/tests/modules/locale_test/config/optional/block.block.classy_test_default_config.yml
    @@ -3,7 +3,7 @@ status: true
    -id: test_default_config
    +id: classy_test_default_config
    
    +++ b/core/modules/locale/tests/src/Kernel/LocaleConfigManagerTest.php
    @@ -87,7 +87,7 @@ public function testGetDefaultConfigLangcode() {
    -      'id' => 'test_default_config',
    +      'id' => 'classy_test_default_config',
    
    @@ -110,12 +110,12 @@ public function testGetDefaultConfigLangcode() {
    -    $this->assertNull(\Drupal::service('locale.config_manager')->getDefaultConfigLangcode('block.block.test_default_config'), 'The block.block.test_default_config is not shipped configuration.');
    +    $this->assertNull(\Drupal::service('locale.config_manager')->getDefaultConfigLangcode('block.block.classy_test_default_config'), 'The block.block.classy_test_default_config is not shipped configuration.');
    ...
    -    $this->assertEqual('en', \Drupal::service('locale.config_manager')->getDefaultConfigLangcode('block.block.test_default_config'), 'The block.block.test_default_config is shipped configuration.');
    +    $this->assertEqual('en', \Drupal::service('locale.config_manager')->getDefaultConfigLangcode('block.block.classy_test_default_config'), 'The block.block.classy_test_default_config is shipped configuration.');
    
    +++ b/core/tests/Drupal/KernelTests/Core/Config/CacheabilityMetadataConfigOverrideTest.php
    @@ -66,14 +66,14 @@ public function testConfigEntityOverride() {
    -    $block = $entity_type_manager->getStorage('block')->load('call_to_action');
    +    $block = $entity_type_manager->getStorage('block')->load('classy_call_to_action');
    ...
    -    $this->assertEqual(['config:block.block.call_to_action', 'pirate-day-tag'], $block->getCacheTags());
    +    $this->assertEqual(['config:block.block.classy_call_to_action', 'pirate-day-tag'], $block->getCacheTags());
    

    Same with all these. I don't think we should be changing them if we don't have to.

  3. +++ b/core/modules/locale/tests/src/Kernel/LocaleConfigManagerTest.php
    @@ -110,12 +110,12 @@ public function testGetDefaultConfigLangcode() {
    diff --git a/core/modules/views/tests/fixtures/update/block.block.bartik_exposed_form_test_exposed_block_page_1.yml b/core/modules/views/tests/fixtures/update/block.block.bartik_exposed_form_test_exposed_block_page_1.yml
    
    diff --git a/core/modules/views/tests/fixtures/update/block.block.bartik_exposed_form_test_exposed_block_page_1.yml b/core/modules/views/tests/fixtures/update/block.block.bartik_exposed_form_test_exposed_block_page_1.yml
    new file mode 100644
    
    new file mode 100644
    index 0000000000..3e4547104d
    
    index 0000000000..3e4547104d
    --- /dev/null
    
    --- /dev/null
    +++ b/core/modules/views/tests/fixtures/update/block.block.bartik_exposed_form_test_exposed_block_page_1.yml
    

    Why do we need this?

grimreaper’s picture

Hello,

First, thanks to everyone contributing to this issue. Since comment 15, I don't have time to push it forward. So huge thanks.

In reply of comment 39, points 1 and 2, this is because a rule for the Coder module had been prepared in the related issue #3061184: Add a rule on block machine name. So to have Core ready for this new rule.

I initially wanted a new rule in Coder, and discussed of that with @Klausi at DDD Transylvania 2019, so he told me to have Core ready. For existing websites, it won't break existing blocks and admin can remove the suggested theme name when placing a new block. Also regarding Coder, PHPCS can be customized with phpcs.xml config file to disable this new rule.

For handling existing websites, I don't know if it is possible to have a rule in Coder not enabled by default. So people can add it in their phpcs.xml config file (opt-in instead of opt-out).

Comment 39, point 3: It was for a Views test and only being renamed in my patch comment 15. But maybe on 9.1.x it is no more relevant.

Regards,

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

grimreaper’s picture

Status: Needs work » Reviewed & tested by the community
Issue tags: +GlobalContributionWeekend2021

Rereading comment 39 and comment 40.

And review points of comment 39 had been replied in comment 40.

So back to RTBC.

rachel_norfolk’s picture

Issue tags: -GlobalContributionWeekend2021 +ContributionWeekend2021

Just doing a little tag tidying. Nice work everyone!!

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

I think adding the theme name to the suggestion is fine and makes sense. But I also think that this is just a suggestion and should not in anyway be mandated by a coder rule. Therefore I think we should do the changes recommended in #39 as we shouldn't be making unnecessary test changes to comply with a Coder rule that core is unlikely to ever adopt.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

drupaldope’s picture

I just made a copy of a subtheme.

I was very surprised to see the site messed up because CSS and JQuery could not be applied anymore.

What I observed:
the blocks were created in the first subtheme, and the name of the original subtheme was not included in the original machine name, so the blocks' ID were similar to #block-blockname
when I copied the theme, all blocks had new names such as #block-themename-blockname

this is very confusing and creates a lot of unnecessary work when copying themes.

if including the name of the theme in the block ID becomes the standard, then I would suggest to also make it a standard when a block is created in the original theme.

and maybe admin users should be warned about it when creating a block, that they are not allowed to name the block as they want and that they can't give it an ID that remains stable from one theme to another.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Sounds like a possible duplicate of https://www.drupal.org/project/drupal/issues/2858897

Webbeh’s picture

Cleaning out old tags from 2019-2021. Per #49, linking #2858897: Block name collision on theme creation as a similar issue.

smustgrave’s picture

Issue tags: +Needs reroll

Could almost say this one is a blocker for https://www.drupal.org/project/drupal/issues/2858897

Sounds like it needs reroll but also address the issues in #39

ravi.shankar’s picture

Assigned: Unassigned » ravi.shankar

Working on this issue.

sandeepsingh199’s picture

Status: Needs work » Needs review
StatusFileSize
new26.18 KB

re-rolled the #32 patch for 9.5.x.

Webbeh’s picture

Assigned: ravi.shankar » Unassigned
Issue summary: View changes
Status: Needs review » Needs work

Un-assigning this task.

Updating the IS to note the work that needs to be done, as #53 inadvertently moved to Needs Review (NR).

This still needs work (NW), see #3061266-39: Prefix block machine name suggestions with the theme machine name:

I think adding the theme name to the suggestion is fine and makes sense. But I also think that this is just a suggestion and should not in anyway be mandated by a coder rule. Therefore I think we should do the changes recommended in #39 as we shouldn't be making unnecessary test changes to comply with a Coder rule that core is unlikely to ever adopt.

Ankit.Gupta’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new29.04 KB

Reroll the patch #53 with Drupal 9.5.x

Webbeh’s picture

Status: Needs review » Needs work

#55, please see the status change and justification from #54. As you did not mention any fix to the feedback in #54, moving back to Needs Work.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Following the suggestion in #54. So going back to patch #37 and making the changes mentioned in #39

Hiding patches #53 and #55

#37 was a while ago that the intediff didn't full generate.

smustgrave’s picture

StatusFileSize
new6.62 KB
new10.49 KB

Fixed some test failures

Status: Needs review » Needs work

The last submitted patch, 59: 3061266-59.patch, failed testing. View results

smustgrave’s picture

Status: Needs work » Needs review
StatusFileSize
new2.2 KB
new11.98 KB
Webbeh’s picture

Issue summary: View changes

Big thanks to @smustgrave for following the issue comments and moving us forward. Per #58 into #61, updating the IS to update where we're at.

The last submitted patch, 58: 3061266-58.patch, failed testing. View results

larowlan’s picture

Status: Needs review » Reviewed & tested by the community

This looks good to me.

Re #47 I don't think this changes things - theming using the block ID is probably not a good idea, you should be using a class added by the template.

  • longwave committed cfe5c877 on 10.1.x
    Issue #3061266 by smustgrave, Grimreaper, ridhimaabrol24, nod_, pavnish...
longwave’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed to 10.1.x, thanks!

nod_’s picture

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.