Make Block config entities fully validatable

Created on 7 August 2023, over 1 year ago
Updated 5 August 2024, 3 months ago

Problem/Motivation

Blocks are some of the most widely used config entities. They should be validatable. This would be very valuable for the Recipes Initiative , especially now that Recipes uses config validation (see #3405328: [meta] Make recipes safer to use in the real world by supporting config validation and rolling back a broken recipe for details).

Proposed resolution

This can easily become a huge scope, so limit the scope to be reviewable:

  1. Make block.block.* validatable.
  2. Introduce type: block.settings.block_content:* to fix the accidental leftovers from when content blocks ("custom blocks" in Drupal 7) were split out — see #5
  3. Make one concrete block plugin with settings fully validatable: the search block (type: block.settings.search_form_block)
  4. .

  5. Open blockers for essential problems that are out of scope:
    1. ✅ Reduction by 11 LoC in this MR: 📌 Add config validation for weights (blocks, filters, etc. all use weights) Fixed
    2. ✅ Reduction by 1 file/11 LoC in this MR: 🐛 ExistsConstraintValidator should ignore NULL values and treat `core` as a valid module Needs review
  6. Open follow-ups for non-essential problems encountered along the way — see under "remaining tasks"

Remaining tasks

  1. ✅ Implement proposed solution
  2. ✅ Get tests passing
  3. ✅ Search module must provide update path for search_form_block blocks' page_id setting
  4. Explicit test coverage for each top-level property of a Block config entity in BlockValidationTest.

Follow-ups:

  1. 📌 [PP-1] Deprecate unused `provider` exported property from Block config entities Postponed
  2. 📌 [PP-1] Deprecate `null` as valid `weight` for Block config entities Postponed
  3. 📌 [PP-1] Deprecate and remove `status` and `info` settings from `block_content` blocks Postponed

User interface changes

None.

API changes

None.

Data model changes

None.

Release notes snippet

TBD

📌 Task
Status

Fixed

Version

11.0 🔥

Component
Block 

Last updated about 16 hours ago

Created by

🇧🇪Belgium wim leers Ghent 🇧🇪🇪🇺

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @wim leers
  • Assigned to wim leers
  • Status changed to Active 8 months ago
  • 🇧🇪Belgium wim leers Ghent 🇧🇪🇪🇺

    Repurposing this to add validation constraints; the original issue summary no longer makes sense because the scope/approach of 📌 Configuration schema & required values: add test coverage for `nullable: true` validation support Fixed evolved.

  • Merge request !6938Resolve #3379725 "Validatable block" → (Open) created by wim leers
  • Status changed to Needs work 8 months ago
  • 🇧🇪Belgium wim leers Ghent 🇧🇪🇪🇺

    Well this is interesting… AFAICT block.block.*:provider is dead/completely unused, but block.block.*:settings.provider is used.

  • Pipeline finished with Failed
    8 months ago
    Total: 533s
    #112765
  • Pipeline finished with Failed
    8 months ago
    Total: 551s
    #112849
  • Pipeline finished with Failed
    8 months ago
    Total: 487s
    #112863
  • Pipeline finished with Failed
    8 months ago
    Total: 489s
    #112873
  • Pipeline finished with Failed
    8 months ago
    Total: 546s
    #112879
  • 🇧🇪Belgium wim leers Ghent 🇧🇪🇪🇺

    Ah, I finally found the explanation for why status, info and view_mode are part of the generic type: block_settings (and hence apply to ALL block plugins!).

    The answer: it's simply an oversight that occurred in #2274175: Block plugin schema should be defined separately from the entity . 😄👍 (\Drupal\block_content\Plugin\Block\BlockContentBlock landed >1 year earlier, but the tight coupling only was removed ~6 months earlier, in #1927608: Remove the tight coupling between Block Plugins and Block Entities . Arguably at least there this should've been rectified, but that was at the height of the "we must get D8 done yesterday!" era, so it totally makes sense.)

    So this issue is definitely cleaning up some very old technical debt, that's caused confusion for every person who has ever looked at the exported YAML for blocks! 🚀

  • 🇫🇷France andypost

    Great find! Would be great to have CR for that change somehow as I bet some contrib already using the same pattern

  • Pipeline finished with Failed
    8 months ago
    Total: 616s
    #112902
  • Pipeline finished with Failed
    8 months ago
    Total: 491s
    #112930
  • 🇧🇪Belgium wim leers Ghent 🇧🇪🇪🇺

    #6: this will absolutely need a change record — probably multiple!

    Wow, with #5 fixed, plus one small fix to the ExtensionExists constraint, we went from 788 failures to 115 failures! Suddenly this became doable 😄

  • Pipeline finished with Failed
    8 months ago
    Total: 603s
    #112942
  • 🇧🇪Belgium wim leers Ghent 🇧🇪🇪🇺

    Yay, and after refining the constraints for the sole block (the search block) whose settings I've added validation constraints to: 115 → 56 failures.

  • Pipeline finished with Canceled
    8 months ago
    Total: 190s
    #112949
  • Pipeline finished with Failed
    8 months ago
    Total: 530s
    #112953
  • Pipeline finished with Failed
    8 months ago
    #112972
  • 🇧🇪Belgium wim leers Ghent 🇧🇪🇪🇺

    Reduced scope by:

    1. Undeprecating the (unused and useless) provider exported property in Block config entities around dropped us to 52 failures
    2. Undeprecating the status and info settings for "content block" blocks dropped us to 51 failures
  • Pipeline finished with Failed
    8 months ago
    Total: 504s
    #113007
  • Pipeline finished with Failed
    8 months ago
    Total: 492s
    #113042
  • 🇧🇪Belgium wim leers Ghent 🇧🇪🇪🇺

    Rewrote issue summary 👍

  • Pipeline finished with Failed
    8 months ago
    Total: 482s
    #113066
  • Pipeline finished with Failed
    8 months ago
    Total: 202s
    #113103
  • Pipeline finished with Failed
    8 months ago
    Total: 604s
    #113112
  • Pipeline finished with Success
    8 months ago
    Total: 572s
    #113167
  • Issue was unassigned.
  • Status changed to Needs review 8 months ago
  • 🇧🇪Belgium wim leers Ghent 🇧🇪🇪🇺

    GREEN!

    Update path tomorrow.

    Very satisfied that I got this to green with just 18 files, +138, -49 in less than a day 🥳🚀 With this in, we could seriously accelerate config validation adoption, and Recipes indirectly!

  • Pipeline finished with Failed
    8 months ago
    Total: 559s
    #113542
  • 🇧🇪Belgium wim leers Ghent 🇧🇪🇪🇺

    2 of the 3 follow-ups created.

    Update path test pushed.

  • Pipeline finished with Failed
    8 months ago
    Total: 491s
    #113565
  • Pipeline finished with Success
    8 months ago
    Total: 567s
    #113610
  • 🇧🇪Belgium wim leers Ghent 🇧🇪🇪🇺

    Update path pushed.

    3rd follow-up created (with MR!).

    Two blockers created:

    … both with a ready-to-review MR that's green. 😊

    Ready for final review! But please help land the two (trivial!) blockers first 😄

  • Status changed to Needs work 8 months ago
  • The Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".

    This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

    Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

  • Status changed to Needs review 8 months ago
  • Pipeline finished with Failed
    8 months ago
    Total: 579s
    #114423
  • Assigned to wim leers
  • Status changed to Needs work 8 months ago
  • 🇧🇪Belgium wim leers Ghent 🇧🇪🇪🇺
  • 🇧🇪Belgium borisson_ Mechelen, 🇧🇪

    I found one tiny nitpick.

  • Pipeline finished with Success
    8 months ago
    Total: 491s
    #115005
  • Issue was unassigned.
  • 🇧🇪Belgium wim leers Ghent 🇧🇪🇪🇺

    I started added explicit validation logic for all of the properties on Block config entities. I just pushed the test coverage for theme.

    But then I noticed that block.block.*:region doesn't have a validation constraint just yet! So this definitely needs more work before it is ready for final review. Will continue this next week.

  • Pipeline finished with Success
    8 months ago
    Total: 499s
    #115023
  • First commit to issue fork.
  • Pipeline finished with Success
    7 months ago
    Total: 601s
    #140766
  • Pipeline finished with Canceled
    7 months ago
    Total: 302s
    #143046
  • Pipeline finished with Failed
    7 months ago
    Total: 641s
    #143056
  • Pipeline finished with Failed
    7 months ago
    Total: 613s
    #160686
  • Pipeline finished with Failed
    7 months ago
    Total: 4446s
    #162198
  • Pipeline finished with Failed
    7 months ago
    Total: 491s
    #162441
  • Pipeline finished with Success
    7 months ago
    Total: 504s
    #162505
  • Pipeline finished with Success
    7 months ago
    Total: 572s
    #162523
  • Status changed to Needs review 7 months ago
  • 🇮🇳India narendraR Jaipur, India

    Marked NR for initial review.

  • Pipeline finished with Success
    7 months ago
    Total: 518s
    #163087
  • Status changed to Needs work 6 months ago
  • 🇺🇸United States phenaproxima Massachusetts

    This is looking good but still needs some changes...

  • Pipeline finished with Success
    5 months ago
    Total: 617s
    #191718
  • Pipeline finished with Canceled
    5 months ago
    Total: 67s
    #192777
  • Pipeline finished with Failed
    5 months ago
    Total: 515s
    #192780
  • Pipeline finished with Success
    5 months ago
    Total: 551s
    #201921
  • Status changed to Needs review 5 months ago
  • 🇮🇳India narendraR Jaipur, India
  • Status changed to Needs work 5 months ago
  • 🇺🇸United States phenaproxima Massachusetts

    A few questions but overall I think this looks pretty good.

  • Pipeline finished with Failed
    5 months ago
    Total: 596s
    #202677
  • Pipeline finished with Success
    5 months ago
    Total: 566s
    #202764
  • Pipeline finished with Success
    5 months ago
    Total: 510s
    #208440
  • Status changed to Needs review 5 months ago
  • 🇮🇳India narendraR Jaipur, India
  • Status changed to Needs work 5 months ago
  • 🇺🇸United States phenaproxima Massachusetts

    Two very minor things (basically just rewordings), otherwise I think this is OK.

  • Pipeline finished with Failed
    5 months ago
    Total: 508s
    #213111
  • Pipeline finished with Success
    5 months ago
    Total: 609s
    #213553
  • Status changed to Needs review 5 months ago
  • 🇮🇳India narendraR Jaipur, India

    Feedback addressed

  • Status changed to RTBC 4 months ago
  • 🇺🇸United States phenaproxima Massachusetts

    No objections here.

  • Status changed to Needs work 4 months ago
  • 🇳🇿New Zealand quietone

    I read the issue summary, the comments and the MR (not a code review). The find in #5 is great!

    I resolved the low hanging comments, leaving 7 to look at. I did find one unanswered question in the MR, I have the same question, so let's get that answered. Setting to NW.

    I think it would help if the issue summary explained why BlockInvalidRegionTest, testRebuildInvalidBlocks() are deleted

    I also question the need to shout at the reader in comments. I am referring to the use of 'TRICKY', a word that also have negative connotations. If we need to draw people's attention to something there is always the use of "Note that ...". There may be other options. This is not meant to block progress on this issue. This conversation may be more suitable for a separate issue.

  • 🇳🇿New Zealand quietone

    Forgot to add, that all the followup are made, with complete issue summaries and the links in the MR are correct.

  • Pipeline finished with Failed
    4 months ago
    Total: 165s
    #224397
  • Pipeline finished with Failed
    4 months ago
    Total: 173s
    #224418
  • Pipeline finished with Success
    4 months ago
    Total: 3425s
    #224473
  • Pipeline finished with Success
    4 months ago
    Total: 478s
    #227908
  • Pipeline finished with Success
    4 months ago
    Total: 477s
    #227916
  • Status changed to RTBC 4 months ago
  • 🇺🇸United States phenaproxima Massachusetts

    I removed the unnecessary test coverage, but as far as I can tell, this is otherwise good to go. I'm going to go ahead and boldly restore RTBC here, and if there's anything further to fix, let's get it over the line. This issue is even more imperative since Create flexible config actions to place a block in the admin or default themes Needs review is close to ready.

  • Status changed to Needs work 4 months ago
  • 🇬🇧United Kingdom alexpott 🇪🇺🌍

    I don't think that config validation issues shoudl just in a type widening for used values. Weight is always an integer once a block has been saved because config casts the value.

  • Pipeline finished with Failed
    4 months ago
    Total: 160s
    #228163
  • Pipeline finished with Failed
    4 months ago
    Total: 173s
    #228165
  • Pipeline finished with Failed
    4 months ago
    #228175
  • Status changed to Needs review 4 months ago
  • 🇺🇸United States phenaproxima Massachusetts

    Alright, all done here AFAIK. I did the deprecation dance around non-integer block weights.

  • Pipeline finished with Failed
    4 months ago
    Total: 159s
    #228176
  • Status changed to Needs work 4 months ago
  • The Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

    This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

    Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

  • Pipeline finished with Failed
    4 months ago
    Total: 155s
    #228239
  • Pipeline finished with Failed
    4 months ago
    #228244
  • Pipeline finished with Failed
    4 months ago
    Total: 767s
    #228265
  • Pipeline finished with Canceled
    4 months ago
    Total: 132s
    #228277
  • Pipeline finished with Failed
    4 months ago
    Total: 495s
    #228279
  • Pipeline finished with Success
    4 months ago
    Total: 460s
    #228292
  • Status changed to Needs review 4 months ago
  • 🇺🇸United States phenaproxima Massachusetts
  • Pipeline finished with Success
    4 months ago
    Total: 463s
    #228302
  • Status changed to Needs work 4 months ago
  • 🇬🇧United Kingdom alexpott 🇪🇺🌍

    Added some more review comments. Nice work on fixing up the int-ness of weight @phenaproxima

  • Pipeline finished with Failed
    4 months ago
    Total: 521s
    #228798
  • Pipeline finished with Canceled
    4 months ago
    Total: 195s
    #228883
  • Pipeline finished with Success
    4 months ago
    Total: 494s
    #228888
  • Pipeline finished with Success
    4 months ago
    Total: 1101s
    #228907
  • Status changed to Needs review 4 months ago
  • 🇺🇸United States phenaproxima Massachusetts

    I think I've addressed all of @alexpott's feedback.

  • Pipeline finished with Success
    4 months ago
    Total: 2175s
    #228925
  • Status changed to Needs work 4 months ago
  • 🇺🇸United States tim.plunkett Philadelphia

    Reviewed

  • Status changed to Needs review 4 months ago
  • 🇺🇸United States phenaproxima Massachusetts

    Okay, I agree with you @tim.plunkett; did the deprecation dance around page_id and added a change record.

  • Pipeline finished with Canceled
    4 months ago
    Total: 447s
    #231312
  • Pipeline finished with Failed
    4 months ago
    Total: 476s
    #231319
  • Status changed to RTBC 4 months ago
  • 🇺🇸United States tim.plunkett Philadelphia

    Great work, all!

  • 🇧🇪Belgium borisson_ Mechelen, 🇧🇪

    I reviewed it again today as well, and the one remark I had was answered. RTBC++

  • Pipeline finished with Success
    4 months ago
    Total: 1125s
    #231325
  • 🇬🇧United Kingdom alexpott 🇪🇺🌍

    Committed 1c21864 and pushed to 11.x. Thanks!

    Given where we are at in the 11.0.0 release cycle we can't commit this to 11.0.0 and have to target 11.1.0 instead. I've updated the deprecation messages and tests on commit.

    diff --git a/core/modules/block/src/Entity/Block.php b/core/modules/block/src/Entity/Block.php
    index 2effc409b7..68582e48f0 100644
    --- a/core/modules/block/src/Entity/Block.php
    +++ b/core/modules/block/src/Entity/Block.php
    @@ -349,7 +349,7 @@ public function preSave(EntityStorageInterface $storage) {
         parent::preSave($storage);
     
         if (!is_int($this->weight)) {
    -      @trigger_error('Saving a block with a non-integer weight is deprecated in drupal:11.0.0 and removed in drupal:12.0.0. See https://www.drupal.org/node/3462474', E_USER_DEPRECATED);
    +      @trigger_error('Saving a block with a non-integer weight is deprecated in drupal:11.1.0 and removed in drupal:12.0.0. See https://www.drupal.org/node/3462474', E_USER_DEPRECATED);
           $this->setWeight((int) $this->weight);
         }
     
    diff --git a/core/modules/block/tests/src/Kernel/BlockValidationTest.php b/core/modules/block/tests/src/Kernel/BlockValidationTest.php
    index 9a1a2412d6..2456e7fb57 100644
    --- a/core/modules/block/tests/src/Kernel/BlockValidationTest.php
    +++ b/core/modules/block/tests/src/Kernel/BlockValidationTest.php
    @@ -176,7 +176,7 @@ public function testWeightValidation(): void {
       public function testWeightCannotBeNull(): void {
         $this->entity->set('weight', NULL);
         $this->assertNull($this->entity->getWeight());
    -    $this->expectDeprecation('Saving a block with a non-integer weight is deprecated in drupal:11.0.0 and removed in drupal:12.0.0. See https://www.drupal.org/node/3462474');
    +    $this->expectDeprecation('Saving a block with a non-integer weight is deprecated in drupal:11.1.0 and removed in drupal:12.0.0. See https://www.drupal.org/node/3462474');
         $this->entity->save();
       }
     
    diff --git a/core/modules/search/search.module b/core/modules/search/search.module
    index 8d5b1e330d..0944c53dcf 100644
    --- a/core/modules/search/search.module
    +++ b/core/modules/search/search.module
    @@ -428,7 +428,7 @@ function search_block_presave(BlockInterface $block) {
       if ($block->getPluginId() === 'search_form_block') {
         $settings = $block->get('settings');
         if ($settings['page_id'] === '') {
    -      @trigger_error('Saving a search block with an empty page ID is deprecated in drupal:11.0.0 and removed in drupal:12.0.0. To use the default search page, use NULL. See https://www.drupal.org/node/3463132', E_USER_DEPRECATED);
    +      @trigger_error('Saving a search block with an empty page ID is deprecated in drupal:11.1.0 and removed in drupal:12.0.0. To use the default search page, use NULL. See https://www.drupal.org/node/3463132', E_USER_DEPRECATED);
           $settings['page_id'] = NULL;
           $block->set('settings', $settings);
         }
    
  • Status changed to Fixed 4 months ago
    • alexpott committed 1c218641 on 11.x
      Issue #3379725 by Wim Leers, phenaproxima, narendraR, alexpott, quietone...
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024