Problem/Motivation

If the SystemMainBlock is used by a module like Page Manager, it will cause a fatal error.
See #2819219: Fatal error when "setMainContent()" method is not called (block module not installed)

Proposed resolution

Restrict the block to block.module.

Is this an abuse of the context system?

Remaining tasks

User interface changes

API changes

Data model changes

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

tim.plunkett created an issue. See original summary.

tim.plunkett’s picture

Status: Active » Needs review
FileSize
2.77 KB

Status: Needs review » Needs work

The last submitted patch, 2: 2825497-block-2.patch, failed testing.

tim.plunkett’s picture

Status: Needs work » Needs review
FileSize
4.02 KB
2.31 KB

Just testing things out.

Status: Needs review » Needs work

The last submitted patch, 4: 2825497-block-4.patch, failed testing.

tim.plunkett’s picture

Status: Needs work » Needs review
FileSize
1.07 KB
5.09 KB

Okay maybe this isn't a good idea.

Status: Needs review » Needs work

The last submitted patch, 6: 2825497-block-6.patch, failed testing.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

tim.plunkett’s picture

Status: Needs work » Needs review
FileSize
5.08 KB

This will still fail, but rerolling just the same

Status: Needs review » Needs work

The last submitted patch, 9: 2825497-block-9.patch, failed testing.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

jiv_e’s picture

I'm sorry to interrupt, but why is this a better solution than returning an empty array by default? Is there some other problem you are solving?

This seems overly complex to me. The fatal error is avoided by returning an empty array, right?

tim.plunkett’s picture

If the block is in the situation where it would return an empty array, it should never have been available to use in the first place.

jiv_e’s picture

I think the idea of restricting the main block to block module is better than just throwing a fatal error for other modules. But this is still looking overly complex to me. I have argumented for the benefits of the simpler solution here: https://www.drupal.org/node/2819219#comment-12135122

I have not yet seen any practical reason for "it should never". Can you help me out to understand those? Also could there be some case where other modules could need an access to the main block? Thanks!

jiv_e’s picture

Hi!

I'm wondering why my questions go unanswered. Are they inappropriate? These are honest questions so if there's something wrong how I present them I would be happy to be guided!

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

EclipseGc’s picture

So, full disclosure: I support the spirit, if not the specific implementation, of what Tim's suggesting here.

@jiv_e

I think your points, on this issue and the other you've referenced, are fine and appropriate. That being said, I'd start my counter argument by pointing out that contextual plugins (of which blocks are a member) work a little differently that perhaps what you're expecting.

Contextual plugins operate on the idea that there are certain method which cannot be invoked until context is set on the plugin. Other methods (like forms for configuration) CAN be invoked w/o context because it's not necessary for them. The block build() method is one of these methods that requires contexts to already be set. The conversion of the MainContent Block is one of those things I started and with help from others, we ultimately got working with the new plugin system. That being said, "context" came along a little later, and the main content block was never revisited subsequent to the introduction of that api addition. Which is to say that the setter that plugin uses is weird, non-standard, and there's already a standard for plugin which need that sort of handling... it's called context(s).

To address Tim's solution more directly, I'd actually invent a new type of context object that only the Main content block requires. That would give us an object on which we could place the expected render array. If the context isn't provided via a typical context provider, then no one will ever accidentally get it, and the block system itself could just hardcode the service id of that context. This might actually make the render pipe-line smoother and clearer as there'd be a service to which objects could read/write main content output. That's just my 2 cents on the approach. I'd love to iterate on this more and see where it goes.

Eclipse

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

jastraat’s picture

I have a conflicting experience with the system_main_block.

I'm using the following modules:

  • Drupal core 8.6.5
  • ctools 8.x-3.0
  • page_manager 8.x-4.0-beta3
  • panels 8.x-4.3
  • panels_everywhere 8.x-4.0-beta1

While I initially could not use Main page content in my panels layouts, when I added a condition of content type = page - it started working.

I only add this comment in case someone else is as frustrated as I was in trying to include the node content in a variant of node/{node}

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

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

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.

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.

sjnorton’s picture

Edit: While I was not able to avoid the error on the landing page I was working on, I was able to create a new landing page, and edit the layout on other nodes, without triggering the error. Since I reconstructed the new landing page with precisely the same elements as the original, I don't know what difference might have triggered the error unless it is some sort of data corruption in how that particular page/node was stored. In any case, while you can get around it, the error is very frustrating and nearly impossible to diagnose because you lose the ability to add, delete, or change blocks included in the areas managed by layout builder. (Unless there is another way to access that? I assume they are in the database, but I didn't want to go mucking about in there.) I'm sure future users would appreciate a fix, or at least some tools to address it.

A clue? While trying to figure out the error, I used other systems which allow you to manage the display, which kept reporting that I had unsaved changes. I would click the 'save' button, and would receive a message that the display had been saved, but upon re-visiting that module it would again say there were unsaved changes. Odd.
-----------

Is there any chance of a functional patch for this? I see other issues with suggested patches were closed as duplicates, but there doesn't seem to be a resolution.

I ask because I *just* started having this error in an existing installation of Drupal 8.9.1, possibly because of running a 'composer update' command. Since the layout I'm trying to manage is the front page, this has brought my site development to a halt (trying, painfully to move from D7 to D8). I can see the front page, but I can't get at any of the pieces controlled by the layout builder to add or change blocks. I'm using a contrib theme that depends heavily on layout builder for basic page visual structure.

Wish I could help, but I don't have the skills. Any advice welcome!

My error:
Argument 1 passed to Drupal\Core\Cache\CacheableMetadata::createFromRenderArray() must be of the type array, null given, called in [site root]/core/modules/layout_builder/src/EventSubscriber/BlockComponentRenderArray.php on line 110 in Drupal\Core\Cache\CacheableMetadata::createFromRenderArray() (line 149 of [site root]/core/lib/Drupal/Core/Cache/CacheableMetadata.php)

Sseto’s picture

@sjnorton

I'm also doing a migration from D7 to D8 too and I'm also having the same issue. Have you find a solution for this problem?

Thanks!

sjnorton’s picture

@sseto

No - my only solution was to rebuild the page from scratch (for the most part, just inserting the same blocks). I don't know why my original home page caused those errors.

joseph.olstad’s picture

Status: Needs work » Closed (duplicate)
Parent issue: » #2885370: SystemMainBlock::build does not always return array

this patch does not apply to 9.1.x
last test also failed

however #2885370: SystemMainBlock::build does not always return array still does apply cleanly to 9.1.x

marking this as a duplicate, and re-openning the other