Problem/Motivation

To reproduce:

Using Drupal 8.8.x, Standard

  • Enable Layout Builder and override on Basic page
  • Add a node
  • Override the node layout and add a Basic block inline block with the title Testing
  • Save the layout.
  • Add a new block on admin/structure/block/block-content and use Testing as block description.

Current behaviour:

You will now get the error: A custom block with block description Testing already exists.

However, since the block is non-reusable and a composite of that node it will not be listed in the Custom block library.

Expected behaviour:

The block should have been created.

Layout Builder / non-reusable blocks seems to ignore the UniqueField constraint on the label (which is probably correct) but it should not conflict with reusable content blocks because they are distinguished.

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Issue fork drupal-3054197

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

johndevman created an issue. See original summary.

johnwebdev’s picture

Issue summary: View changes
johnwebdev’s picture

Status: Active » Needs review
StatusFileSize
new1.09 KB

Failing test

Status: Needs review » Needs work

The last submitted patch, 3: 3054197-3--fail.patch, failed testing. View results

tedbow’s picture

Status: Needs work » Needs review
StatusFileSize
new5.6 KB
new6.7 KB
  1. +++ b/core/modules/block_content/tests/src/Functional/BlockContentValidationTest.php
    @@ -37,4 +37,27 @@ public function testValidation() {
    +  /**
    +   * Tests block label validation.
    +   */
    +  public function testLabelValidation() {
    

    The testValidation() method in this class already check for the validation message so I think we could just add this to the existing test and do another case where it should not exist.

  2. Here is patch creates a new UniqueBlockContentLabelValueValidator. I have changed UniqueFieldValueValidator to add a getEntityQuery() method. This allows use to extend this and just add
    $query->condition('reusable', 1);

    We could also just copy logic from UniqueFieldValueValidator and not extend it. But it seems useful to have a "unique with conditions" ability in other validators.

bkosborne’s picture

Status: Needs review » Reviewed & tested by the community

This works as expected and solves the problem for us! Thank you!

tedbow’s picture

Status: Reviewed & tested by the community » Needs work

patch doesn't apply anymore

tedbow’s picture

Status: Needs work » Needs review
StatusFileSize
new6.69 KB

reroll

nghai’s picture

Hey @tedbow

  1. the patch application fails with following error:
    Hunk #3 FAILED at 55.
    1 out of 3 hunks FAILED -- saving rejects to file core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/UniqueFieldValueValidator.php.rej
  2. after manual application of the patch,
    • it is possible to create a new custom block at admin/structure/block/block-content with same description as used for a non-reusable block but,
    • now it throws error when same title is used to add the translation of the same non-reusable block (using layout builder).

    without the patch the translations works fine.

Tested with Drupal 8.7.5, with layouts builders + multi-lingual

UPDATE: the patch 3054197-8 not failing with core 8.8.x

nghai’s picture

StatusFileSize
new7.08 KB
new962 bytes

As mentioned here and from above comment

The BlockContentInlineBlockTranslateForm also validates using an _entity_form with BlockContent.php as entity type, which is not the case for creating and updating InlineBlock originally.

It would be ideal to validate the block_content only if there are reusable. Attaching the patch and interdiff to review.

And the above patch 3054197-8 is not failing with 8.8.x.

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.

capysara’s picture

I'm not sure how to test the translation part, but the patch in #10 applies and works for 8.9.

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.

janmejaig’s picture

I have verified the above ticket and found that it is working fine on Drupal 9.1.x as expected. This seems to be working as expected without applying patch.

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.

abhijith s’s picture

StatusFileSize
new17.02 KB

Applied patch #10 on 9.2.x and it doesn't fix this issue.I'm still getting that error after applying this patch.

after

abhijith s’s picture

Status: Needs review » Needs work
vakulrai’s picture

Status: Needs work » Needs review

Abhijith S, I check the scenario like below and seems to be working as expected:

  1. I enabled Layout builder, and added blocks to a "Content type", with name "Testing"
  2. Now I created a custom block here, "admin/structure/block/block-content", with name "Testing" and it gets created without any error

I think you must be testing it by directly adding it form custom blocks, that's why its giving validation error, please check this: Allow Custom blocks to be set as non-reusable adding access restriction based on where it was used.

Thanks

vakulrai’s picture

StatusFileSize
new7.24 KB

Rerolled for 9.2.x, #10 was failing to apply cleanly.

This statement was failing the patch.

@@ -39,4 +39,27 @@ public function testValidation() {		@@ -42,4 +42,27 @@ public function testValidation() {
     ]));		     $this->assertEqual(new FormattableMarkup('A custom block with block description %value already exists.', ['%value' => $block->label()]), $violations[0]->getMessage());
   }

Thanks.

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.

bkosborne’s picture

I was thinking - why do we even force block titles to be unique at all? I did some searching and finally found the source of this: #1998658: Creating Custom Block with same name as existing block throws SQL error

I have to dig more - but I don't think that SQL error is still relevant? There's nothing in the database table that indicates it must be unique.

Maybe we should instead just open a separate issue to allow blocks to have duplicate titles? Why should content blocks be any different than Nodes in this regard (which allow duplicate titles).

ranjith_kumar_k_u’s picture

StatusFileSize
new7.22 KB

Re-rolled #19 for 9.4

devashish jangid’s picture

StatusFileSize
new19.68 KB
new32.81 KB

Drupal 9.4.x-dev
Verified and tested patch #23.
Patch applied successfully and looks good for me.
Sharing screenshot for the reference.
Moving this ticket to RTBC.

lobsterr’s picture

After investigation I found the UniqueField constraint is useless and it is an heritage from the old custom block module. More info could be found here:

#3272969: Remove unique constraint on block content info field

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

Status: Needs review » Postponed (maintainer needs more info)
Issue tags: +Bug Smash Initiative

Wondering if this is still an issue if the unique constraint is dropped in https://www.drupal.org/project/drupal/issues/3272969

smustgrave’s picture

Status: Postponed (maintainer needs more info) » Closed (duplicate)

After speaking with @larowlan on slack.

its a hark-back to d7 era DB where the 'info' key formed the block plugin ID
we don't need that anymore because we use the block content uuid (edited)

And in https://www.drupal.org/project/drupal/issues/3272969 we are removing the constraint. So closing out as duplicate.

PunamShelke made their first commit to this issue’s fork.