Problem/Motivation

When creating a new block via block layout region is not stored.

Steps to reproduce

1) Go to /admin/structure/blocks
2) Use the 'Place Block' function next to a region, for example the 'Sidebar Second'.
3) Choose 'add custom block'
4) On the 'Configure Block'-page the region you've selected on step #2 is not selected.

Proposed resolution

Use a URL parameter to remember/set the region like we do for theme

Remaining tasks

User interface changes

When creating a new block the region will be remembered.

API changes

NA

Data model changes

NA

Release notes snippet

NA

CommentFileSizeAuthor
#75 2833116-nr-bot.txt90 bytesneeds-review-queue-bot
#73 2833116-nr-bot.txt2.67 KBneeds-review-queue-bot
#66 2833116-nr-bot.txt90 bytesneeds-review-queue-bot
#64 Screenshot 2023-12-01 135315.png46.49 KBNitin shrivastava
#56 interdiff-50-56.txt2.32 KBnitin_lama
#56 2833116-56.patch10.16 KBnitin_lama
#50 interdiff-2833116-48-50.txt791 bytesmohit_aghera
#50 2833116-50.patch10.18 KBmohit_aghera
#48 re_roll_diff_47_48.txt2.54 KBmohit_aghera
#48 2833116-48.patch10.18 KBmohit_aghera
#47 interdiff-2833116-46-47.txt932 bytesmohit_aghera
#47 2833116-47.patch10.18 KBmohit_aghera
#46 interdiff-2833116-44-46.txt705 bytesmohit_aghera
#46 2833116-46.patch10.3 KBmohit_aghera
#44 2833116-44.patch10.23 KBsmustgrave
#44 interdiff-42-44.txt4.32 KBsmustgrave
#42 2833116-42.patch5.39 KBsmustgrave
#42 interdiff-31-42.txt4.72 KBsmustgrave
#39 2833116-39.patch4.5 KB_pratik_
#38 interdiff_31-38.txt2.17 KB_pratik_
#38 2833116-38.patch4.5 KB_pratik_
#35 after_patch.jpg16.62 KBgaurav-mathur
#35 before_patch.jpg16.61 KBgaurav-mathur
#34 After-Patch-2833116-31.png353.29 KBManibharathi E R
#34 Before-Patch-2833116-31.png351.48 KBManibharathi E R
#31 2833116-31.patch3.72 KBsmustgrave
#31 interdiff-19-31.txt4.7 KBsmustgrave
#26 interdiff_25-26.txt1.51 KBRatan Priya
#26 2833116-26.patch1.66 KBRatan Priya
#25 interdiff_23-25.txt2.19 KBRatan Priya
#25 2833116-25.patch1.67 KBRatan Priya
#23 Screen Shot 2022-07-28 at 1.46.02 PM.png793.41 KBsmustgrave
#21 interdiff-2833116-12-19.txt1017 bytesmohit_aghera
#21 2833116-19.patch3.28 KBmohit_aghera
#21 2833116-19-test-only.patch1.06 KBmohit_aghera
#14 Before Patch.png61.08 KBRinku Jacob 13
#14 After Patch.png61.73 KBRinku Jacob 13
#12 2833116-12.patch2.28 KB_shY
#9 2833116-9.patch1.56 KB_shY

Issue fork drupal-2833116

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:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Peter van den Heuvel created an issue. See original summary.

Peter van den Heuvel’s picture

Issue summary: View changes
cilefen’s picture

Title: When Placing a Block on 'Configure Block' page the selected region is empty » When Placing a Block on 'Configure Block' page the originally selected region is lost
Priority: Minor » Normal

Version: 8.2.3 » 8.2.x-dev

Core issues are now filed against the dev versions where changes will be made. Document the specific release you are using in your issue comment. More information about choosing a version.

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

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Branches prior to 8.8.x are not supported, and Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should 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.

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

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev
quietone’s picture

Issue tags: +Bug Smash Initiative

I tested this on Drupal 10.0.x and the problem still exists.

_shY’s picture

I made a small investigation about that.
Seems like the region set in the core/modules/block/src/BlockForm.php and it's taken from the request.

    $region = $entity->isNew() ? $this->getRequest()->query->get('region', $entity_region) : $entity_region;
    $form['region'] = [
      '#type' => 'select',
      '#title' => $this->t('Region'),
      '#description' => $this->t('Select the region where this block should be displayed.'),
      '#default_value' => $region,
      '#required' => TRUE,
      '#options' => system_region_list($form_state->getValue('theme', $theme), REGIONS_VISIBLE),
      '#prefix' => '<div id="edit-block-region-wrapper">',
      '#suffix' => '</div>',
    ];

But, when we redirect to this form, regions don't pass through request.
So, first I add the route parameter for the action link ("Add custom block"). And later, when we create our custom block, add this parameter to the form state redirect method in core/modules/block_content/src/BlockContentForm.php

        $form_state->setRedirect(
          'block.admin_add',
          [
            'plugin_id' => 'block_content:' . $block->uuid(),
            'theme' => $theme,
            'region' => $this->getRequest()->query->get('region'),
          ]
        );
_shY’s picture

Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch, 9: 2833116-9.patch, failed testing. View results

_shY’s picture

Reworked patch. Now all tests must pass.

_shY’s picture

Status: Needs work » Needs review
Rinku Jacob 13’s picture

FileSize
61.73 KB
61.08 KB

I have applied the Patch #12 Successfully for drupal 9.3.x-dev. Before applying the patch the region we have selected in the place block function is not displayed in the Region select box. But after the patch the region will displayed as default.

_shY’s picture

Rinku Jacob 13, thank you for testing.

But after the patch the region will displayed as default.

Do you mean, that after applying the patch, a region that was selected before will be displayed in the settings form?

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

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should 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.

Rinku Jacob 13’s picture

@ Kostia Bohach I have meant like that . But mistakely i wrote that. After applying the patch, a region that was selected before will be displayed in the settings form. In screenshots we can see the difference.Sorry for the mistake.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

This seems like a perfect use case for a test.

Munavijayalakshmi’s picture

Assigned: Unassigned » Munavijayalakshmi
Munavijayalakshmi’s picture

Assigned: Munavijayalakshmi » Unassigned
mohit_aghera’s picture

The last submitted patch, 21: 2833116-19-test-only.patch, failed testing. View results

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs tests
FileSize
793.41 KB

Wow quick turn around on those tests! Good job!

Tested patch #21

Going to block layout
Place block
Create new custom block
Saving that block
Verified the region I originally clicked to place the block was remembered.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

This seems like a nice improvement to usability.

  1. +++ b/core/modules/block/src/Controller/BlockLibraryController.php
    @@ -104,6 +104,16 @@ public function listBlocks(Request $request, $theme) {
    +    $add_action_url = isset($build['local_actions']['block_content_add_action']['#link']['url'])
    +      ? $build['local_actions']['block_content_add_action']['#link']['url']
    +      : NULL;
    

    This can be written as:
    $add_action_url = $build['local_actions']['block_content_add_action']['#link']['url'] ?? NULL;

  2. +++ b/core/modules/block/src/Controller/BlockLibraryController.php
    @@ -104,6 +104,16 @@ public function listBlocks(Request $request, $theme) {
    +    if ($add_action_url instanceof Url && $region) {
    

    I think we shouldn't do the && region here. I think adding the param if it is NULL should be okay - it'll just be ignored and then we don't have to think about regions called '0'.

  3. +++ b/core/modules/block_content/src/BlockContentForm.php
    @@ -68,13 +68,14 @@ public function save(array $form, FormStateInterface $form_state) {
    -        $form_state->setRedirect(
    -          'block.admin_add',
    -          [
    -            'plugin_id' => 'block_content:' . $block->uuid(),
    -            'theme' => $theme,
    -          ]
    -        );
    +        $route_parameters = [
    +          'plugin_id' => 'block_content:' . $block->uuid(),
    +          'theme' => $theme,
    +        ];
    +        if ($region = $this->getRequest()->query->get('region')) {
    +          $route_parameters['region'] = $region;
    +        }
    +        $form_state->setRedirect('block.admin_add', $route_parameters);
    

    I think this could be:

            $form_state->setRedirect(
              'block.admin_add',
              [
                'plugin_id' => 'block_content:' . $block->uuid(),
                'theme' => $theme,
                'region' => $this->getRequest()->query->get('region')
              ]
            );
    

    For the same reason as above.

  4. There's no test coverage of the change to \Drupal\block\Controller\BlockLibraryController::listBlocks() - if I remove the new code the test still passes.
Ratan Priya’s picture

Status: Needs work » Needs review
FileSize
1.67 KB
2.19 KB

@alexpott,
Made changes as per comment #24 for points 1,2,3.
Needs review.

Ratan Priya’s picture

Fixed custom commands failing issue.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Tested #26

Went to block layout
Clicked Add block into header region
Create custom block
Next page the header region was auto selected

Tested again with a region in the middle
Same correct behavior.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

We've lost the test coverage that was present in #21 and the missing test coverage noted by #24.4 still does not exist.

smustgrave’s picture

Good catch. @Ratan Priya was there a reason for removing the tests?

Going back to #19 and starting from there to be safe.

smustgrave’s picture

Fyi this needs more work for the solution. If you go to /admin/structure/block/library/stark?region=content the region parameter isn't set.

smustgrave’s picture

Updated solution to cover additional scenario

Updated tests also.

Status: Needs review » Needs work

The last submitted patch, 31: 2833116-31.patch, failed testing. View results

mohit_aghera’s picture

Status: Needs work » Needs review

Looks like test case failures are random. Test results seems to be green.

Manibharathi E R’s picture

Patch #31 Applied successfully in Drupal 9.4.x and Drupal 9.5.x.

Steps:
1. Go to Structure->Block Layout
2. Click Place block any regions
3. Than Click Add custom block
4. Check the regions are not auto selected.
5. Apply the Patch
6. Regions are auto selected in the block configuration.

gaurav-mathur’s picture

FileSize
16.61 KB
16.62 KB

Applied patch #31 successfully in Drupal 9.5.x. and working fine.Adding screenshots to refer after and before patch.

ameymudras’s picture

Status: Needs review » Needs work

Tested on Drupal 9.5

- Issue summary is clear and steps to reproduce have been provided
- The patch applies cleanly
- Was able to reproduce the issue following the steps in #34 and the patch fixes the issue
- A couple of suggestions with respect to the code

The comment wordings could have been better

+    // Adds to the "Add custom block" action url route parameters, to provide
+    // correct region setting after creation.

Can we make use of dependency injection here

+    if ($region = \Drupal::request()->query->get('region')) {
+      $options['query']['region'] = $region;
+    }
_pratik_’s picture

Assigned: Unassigned » _pratik_
_pratik_’s picture

Assigned: _pratik_ » Unassigned
Status: Needs work » Needs review
FileSize
4.5 KB
2.17 KB
_pratik_’s picture

Fixed above CCF.

Status: Needs review » Needs work

The last submitted patch, 39: 2833116-39.patch, failed testing. View results

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

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should 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

Started back at #31

Updated the comment slightly
Added the current route to the LocalActionDefault

smustgrave’s picture

Version: 9.5.x-dev » 10.1.x-dev
smustgrave’s picture

More convinced we will need a trigger_error :(

Status: Needs review » Needs work

The last submitted patch, 44: 2833116-44.patch, failed testing. View results

mohit_aghera’s picture

Status: Needs work » Needs review
FileSize
10.3 KB
705 bytes

I think a few permissions were missing in the test case.
Failing test case is passing on local now.

mohit_aghera’s picture

Oops, probably something like this also be a good change.
Updated the patch.
Cancelled the previous test case run and hiding the patch.

mohit_aghera’s picture

Looks like my local 10.1.x head was not updated.
Re-rolled the patch.

Status: Needs review » Needs work

The last submitted patch, 48: 2833116-48.patch, failed testing. View results

mohit_aghera’s picture

Status: Needs work » Needs review
FileSize
10.18 KB
791 bytes

Fixing the label change happened in latest head.
Hiding failed patches.

Status: Needs review » Needs work

The last submitted patch, 50: 2833116-50.patch, failed testing. View results

mohit_aghera’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Needs Review Queue Initiative

Tested patch #50 following steps in issue summary and it resolves the issue.

Still think we will need a trigger_error but maybe the committer will surprise me.

quietone’s picture

Status: Reviewed & tested by the community » Needs work

Doing RTBC triage.

The first thing I notice is that the issue summary is incomplete and the steps to reproduce are out of date. And since this is making a change to the user interface there should be before and after screenshots. However, maybe manual testing here is fine.

I read the comments. I agree with #24 that this is a nice usability improvement. The changes asked for in #24 appear to have been done. In #42 there should be an explanation for why the current route is added to LocalActionDefault and that should be in the proposed resolution as well. And a reminder that anyone who works on a patch should ask for reviews and someone else to set the issue to RTBC.

I then looked at the patch.

  1. +++ b/core/modules/block/src/Controller/BlockLibraryController.php
    @@ -104,6 +104,14 @@ public function listBlocks(Request $request, $theme) {
    +    // Add url route parameters to provide correct region setting
    +    // after creation.
    

    Needs to be wrapped to 80 columns.

  2. +++ b/core/modules/block_content/src/Plugin/Menu/LocalAction/BlockContentAddLocalAction.php
    @@ -20,6 +20,12 @@ public function getOptions(RouteMatchInterface $route_match) {
    +    // If the current request has a region query parameter.
    

    This isn't a complete sentence.

  3. +++ b/core/modules/block_content/tests/src/Functional/BlockContentListTest.php
    @@ -69,6 +69,22 @@ protected function setUp(): void {
    +   * Tests that region value is retained when we create new block.
    

    This can be simpler. "Tests the region value when a new block is saved."

  4. +++ b/core/lib/Drupal/Core/Menu/LocalActionDefault.php
    @@ -37,11 +44,14 @@ class LocalActionDefault extends PluginBase implements LocalActionInterface, Con
    +  public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteProviderInterface $route_provider, Request $request) {
    

    See policy for adding constructor arguments. Although my later testing questions whether these changes are needed.

  5. +++ b/core/modules/block_content/tests/src/Functional/BlockContentListTest.php
    @@ -69,6 +69,22 @@ protected function setUp(): void {
    +      'info[0][value]' => $this->randomString(),
    

    Since this is testing placement and not the value can we skip generating a random value and just use, say, 'foo'.

I then tested on a fresh install of Drupal 10.1x, standard install. First, I confirmed the problem using the steps in the IS. The I applied the patch and confirmed the fix. I then removed the changes to the menu system (core/modules/menu_ui/src/Plugin/Menu/LocalAction/MenuLinkAdd.php and core/modules/menu_ui/src/Plugin/Menu/LocalAction/MenuLinkAdd.php), cleared cached and retested. The fix was still working but the new test was failing. That needs to be looked into. I didn't look further myself.

So, back to NW for comment changes and then a question about the changes to the menu system.

nitin_lama’s picture

Assigned: Unassigned » nitin_lama
nitin_lama’s picture

Assigned: nitin_lama » Unassigned
Status: Needs work » Needs review
FileSize
10.16 KB
2.32 KB

Addressed #54 changes but not sure of the cause of the failed tests. For pointer 4, we can remove the unused class object but are unsure of its original purpose. Updated patch. Please review.

smustgrave’s picture

Status: Needs review » Needs work

Issue summary needs to be updated

and #54.4 hasn't been done.

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

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Issue summary: View changes

Hiding patches and converted to MR.

smustgrave’s picture

Status: Needs work » Needs review

@quietone the request is used to get the query parameter which can't get from route_match. To get around that we would probably have to update the url to include region also. That seemed like the least desirable approach

quietone’s picture

Status: Needs review » Needs work

@smustgrave, thanks. I don't know enough about this so I will leave this for others to RTBC.

The conversion to MR lost a change, I have commented in the MR.

smustgrave’s picture

Status: Needs work » Needs review
Nitin shrivastava’s picture

Status: Needs review » Reviewed & tested by the community
FileSize
46.49 KB

@smustgrave
The issue has been resolved following the patch in MR #5562. When creating a custom block within the region, it displays correctly in the drop-down menu, automatically selecting the respective region
Thanks !

Before Implementation

Only local images are allowed.

After Implementation

Example

cilefen’s picture

Nitpick: Is it normal for `url` to be lower-case in comments? It’s an acronym.

Neither my comment nor the screenshot review above should be credited.

needs-review-queue-bot’s picture

Status: Reviewed & tested by the community » Needs work
FileSize
90 bytes

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 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.

larowlan’s picture

Left a review

mohit_aghera’s picture

Status: Needs work » Needs review

@larowlan Addressed all 3 issues.
Tests are green now.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Resolved all the threads fixed by @mohit_aghera.

Rebased the MR as there was a merge conflict.

Retested by going through block layout > place block into content region > custom block > verified region is saved.

larowlan’s picture

Updating issue credits

larowlan’s picture

Status: Reviewed & tested by the community » Needs work

Left some comments on the MR to simplify this

smustgrave’s picture

Status: Needs work » Needs review

Addressed feedback.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
FileSize
2.67 KB

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.

smustgrave’s picture

Status: Needs work » Needs review

Fixed

needs-review-queue-bot’s picture

Status: Needs review » Needs work
FileSize
90 bytes

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.

smustgrave’s picture

Status: Needs work » Needs review

Rebased

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

acbramley’s picture

Component: block.module » block_content.module
Status: Needs review » Reviewed & tested by the community

Test only failed as expected. Looks good to go!

smustgrave’s picture

Thanks @acbramley!

alexpott’s picture

Version: 11.x-dev » 10.3.x-dev
Status: Reviewed & tested by the community » Fixed

Committed and pushed 1f94e17f9f to 11.x and a0138ae5ef to 10.3.x. Thanks!

  • alexpott committed a0138ae5 on 10.3.x
    Issue #2833116 by smustgrave, mohit_aghera, Ratan Priya, _pratik_, _shY...

  • alexpott committed 1f94e17f on 11.x
    Issue #2833116 by smustgrave, mohit_aghera, Ratan Priya, _pratik_, _shY...

Status: Fixed » Closed (fixed)

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