Currently, if there are active block reaction, blocks which are displayed are only the ones user selects and place inside block reaction. If you would like to use context block reaction just for one case, for example, place additional sidebar navigation on article pages, you would need to place all the blocks from block layout to block reaction. I will provide a patch, which will add you a checkbox inside block reaction to include blocks from default block layout. That way you will only have to place specific blocks into your context and all other blocks from default block layout will be automatically added.

Comments

icurk created an issue. See original summary.

icurk’s picture

Assigned: icurk » Unassigned
Status: Active » Needs review
StatusFileSize
new3.45 KB
boshtian’s picture

Status: Needs review » Reviewed & tested by the community

I confirm that the patch is working.

  • boshtian committed 2d50f09 on 8.x-1.x authored by icurk
    Issue #2873537 by icurk, boshtian: Include blocks from default block...
boshtian’s picture

Status: Reviewed & tested by the community » Fixed
grimreaper’s picture

Status: Fixed » Active

Hello,

I was testing that and there is still a problem.

Even if the checkbox is checked, if you place a block in a region with context, it will remove the blocks placed in this region in particular with the native "block layout".

Steps to reproduce:
- On a standard Drupal installation
- Enable context, context ui
- Create a context
- Add a block reaction
- Check "Include blocks from Block layout"
- Place a block inside the "sidebar first" region of Bartik
- Save
- The default search block from the left sidebar does not appear
- Change the block you placed with context to the "sidebar second"
- Save
- The default search block from the left sidebar appears

grimreaper’s picture

Status: Active » Needs review
StatusFileSize
new1.18 KB

Here is a patch that should fix the issue.

Off topic: I don't understand why there is a "return $build" in this foreach.

foreach ($this->contextManager->getActiveReactions('blocks') as $reaction) {
      if ($reaction->includeDefaultBlocks()) {
        $build = array_merge_recursive($this->getBuildFromBlockLayout(), $build);
        return $build;
      }
    }
grimreaper’s picture

Also with or without patch from comment #7, if I have the blocks from the default block layout included, I have messages displayed twice.

One in the standard zone and one on the "content" region that is forced by context in its code from src/Plugin/DisplayVariant/ContextBlockPageVariant.php.

icurk’s picture

Thanks @Grimreaper, nice finding. I tested with your patch and I don't get a message displayed twice. You are talking about drupal messages, right? Do you have steps I can take to reproduce this error? Messages are only displayed twice if I put system message block in block layout and also into context.

For the question about "return $build". I put it there because we don't need to loop over all contexts if we already found a context which has "include blocks from def..." checked. I agree that it would be cleaner if we replace "return $build" with "break".

grimreaper’s picture

Thanks @icurk for the explanation.

For the message block, I opened a dedicated issue #2876747: Messages block displayed twice so it does not block the merge of the patch from comment #7.

grimreaper’s picture

When making the patch for #2876747: Messages block displayed twice, I figured out that the block page title was not displayed even though I checked "Include blocks from Block layout"

And I don't understand why the following code is necessary for the main page content:

    // Place main content and messages blocks, these will be removed by the
    // reactions if a message or content block has been manually placed.
    $build['content']['system_main'] = $this->mainContent;

Yes If I remove this line the main page content block does not appear but it should appear as "Include blocks from Block layout" is checked...

icurk’s picture

Status: Needs review » Reviewed & tested by the community

I reviewed patch from #7 and it works. Since Issue with duplicated message has its own issue I think #7 patch can be committed.

  • Grimreaper authored 345b702 on 8.x-1.x
    Issue #2873537 by icurk, Grimreaper: Include blocks from default block...
boshtian’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

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

tnfno’s picture

I am on 8.x-1.x-dev and when I select to Include blocks from Block layout I get the a 500 and the following PHP error:

PHP Fatal error: Unsupported operand types in /drupal/core/lib/Drupal/Core/Render/RenderCache.php on line 284

d.sibaud’s picture

8.x-1.x-dev, strange behaviour

I have a context with path condition:

/oncologia
/oncologia/*
/oncology
/oncology/*

and reactions:

- body class
- block: one block placed and "Include blocks from Block layout" checked

on /oncologia and /oncology all works, but on /oncologia/* and /oncology/* paths all the blocks are shown except the title block.

1) if I deactivate the context, the block title reappears
2) if I leave the context and plus I add the block title inside the block reaction, it shows correctly, but not honoring its weight:

3) if I change a block weight in the context block's reaction tab (showing the tabledrag-hide td) by ex to -10 and, in the /admin/structure/block another block has an higher weight (es: 0), the block set in context will be shown after the ones configured in the same region in /admin/structure/block
(I'll open another issue for this last one)

luke.leber’s picture

Patch #7 has an issue. The array_merge_recursive call has the potential to create an invalid data type for the 'max-age' cache key.

This will result in the fatal error that tnfno mentioned.

Admittedly, I don't have the context of this module down (no pun) enough that I can confidently contribute a fix for it, but as food for the other developers' thoughts, would array_replace_recursive work instead? Or perhaps coercing the 'max-age' => array back into an int?

johnhuang0808’s picture

Hi @Luke.Leber
Thank you for finding the bug.
I tried to fix the invalid max-age value issue in #2902184.

Would you take a look and help me test if the patch works for you? Thanks.

icurk’s picture

#17 I created a new issue for the problem with sorting the blocks. Why page title is not shown is still a mystery for me, but a workaround is to place it in context.

maaty388’s picture

I fixed title not displaying if using context Here
#17 check it out