Problem/Motivation

After enabling ui_patterns_block submodule and using components as block on layout builder, I've got the following error:

Undefined array key "bundle" in Drupal\ui_patterns_blocks\Plugin\Block\ComponentBlock->getComponentSourceContexts() (line 137 of modules/contrib/ui_patterns/modules/ui_patterns_blocks/src/Plugin/Block/ComponentBlock.php).

Proposed resolution

On modules/ui_patterns_blocks/src/Plugin/Block/ComponentBlock.php block plugin line 137
replace: if (!$this->context["bundle"] && isset($this->context["entity"]))
by: if (!isset($this->context["bundle"]) && isset($this->context["entity"]))

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

b.khouy created an issue. See original summary.

b.khouy’s picture

Status: Needs work » Needs review
b.khouy’s picture

Assigned: b.khouy » Unassigned
Issue summary: View changes
pdureau’s picture

Status: Needs review » Needs work

Hi Brahim,

-    if (!$this->context["bundle"] && isset($this->context["entity"])) {
+    if (!isset($this->context["bundle"]) && isset($this->context["entity"])) {
Determine if a variable is considered set, this means if a variable is declared and is different than null.

Source: https://www.php.net/manual/fr/function.isset.php

Are we losing something with this change? What is happening if $this->context["bundle"] return a value resolving to false, but not to null?

Those are genuine questions, not rhetorical, I am not a PHP expert.

pdureau’s picture

Title: [2.0.0-beta2] Undefined array key "bundle" in Drupal\ui_patterns_blocks\Plugin\Block\ComponentBlock->getComponentSourceContexts() » [2.0.0-beta2] Undefined array key "bundle" in ComponentBlock
pdureau’s picture

Status: Needs work » Closed (duplicate)

already done in #3467502: [2.0.0-beta2] ContextHelper cleaning

But we will not forget you in the credits

b.khouy’s picture

@pdureau

If $this->context["bundle"] is expected to return either a string (the bundle), a boolean (for reasons that are unclear), or null, and the condition needs to be valid when the bundle is not null or when the bundle is false, the condition should be written like this:
if (!isset($this->context["bundle"]) && $this->context["bundle"] && isset($this->context["entity"])) {

Ultimately, the correct approach depends on your requirements. You're right I see that it has already been resolved in: #3467502: [2.0.0-beta2] ContextHelper cleaning
:)