diff --git a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ParagraphBehavior.php b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ParagraphBehavior.php index 8c41b8f1d..87e45669f 100644 --- a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ParagraphBehavior.php +++ b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ParagraphBehavior.php @@ -26,8 +26,15 @@ * label = @Translation("Paragraphs behavior plugin key") * ), * "behavior_plugin_default" = @ContextDefinition("any", - * label = @Translation("Paragraphs behavior plugin default for this key") + * label = @Translation("Paragraphs behavior plugin default for this key"), + * required = FALSE, + * default_value = NULL, * ), + * "throw_on_missing_plugin" = @ContextDefinition("boolean", + * label = @Translation("Whether to throw if behaviour plugin is not enabled"), + * required = FALSE, + * default_value = TRUE, + * ) * } * ) */ @@ -44,17 +51,24 @@ class ParagraphBehavior extends DataProducerPluginBase { * Key for requested value of this paragraph behavior plugin. * @param mixed $behavior_plugin_default * Provided default value for requested key or NULL. + * @param bool $throw_on_missing_plugin + * Whether to throw if behaviour plugin is not enabled. * * @return mixed * Value of this paragraph behavior plugin key. * * @throws \Exception */ - public function resolve(ParagraphInterface $paragraph, string $behavior_plugin_id, string $behavior_plugin_key, $behavior_plugin_default = NULL) { + public function resolve(ParagraphInterface $paragraph, string $behavior_plugin_id, string $behavior_plugin_key, mixed $behavior_plugin_default = NULL, bool $throw_on_missing_plugin = TRUE): mixed { if ($paragraph->getParagraphType()->hasEnabledBehaviorPlugin($behavior_plugin_id)) { return $paragraph->getBehaviorSetting($behavior_plugin_id, $behavior_plugin_key, $behavior_plugin_default); } - throw new \Exception('Not enabled or invalid paragraphs behavior plugin.'); + + if ($throw_on_missing_plugin) { + throw new \Exception('Not enabled or invalid paragraphs behavior plugin.'); + } + + return $behavior_plugin_default; } } diff --git a/modules/thunder_gqls/tests/src/Kernel/DataProducer/ParagraphsBehaviorTest.php b/modules/thunder_gqls/tests/src/Kernel/DataProducer/ParagraphsBehaviorTest.php index 9f2347a8e..51a650c20 100644 --- a/modules/thunder_gqls/tests/src/Kernel/DataProducer/ParagraphsBehaviorTest.php +++ b/modules/thunder_gqls/tests/src/Kernel/DataProducer/ParagraphsBehaviorTest.php @@ -61,9 +61,9 @@ public function testBehaviorSettings(): void { 'paragraph' => $paragraph, 'behavior_plugin_id' => 'test_text_color', 'behavior_plugin_key' => 'text_color', - 'behavior_plugin_default' => 'blue', + 'behavior_plugin_default' => NULL, ]); - $this->assertEquals('blue', $result); + $this->assertNull($result); // Create a paragraph and set its feature settings. $paragraph = Paragraph::create([ @@ -81,7 +81,7 @@ public function testBehaviorSettings(): void { 'paragraph' => $paragraph, 'behavior_plugin_id' => 'test_text_color', 'behavior_plugin_key' => 'text_color', - 'behavior_plugin_default' => 'blue', + 'behavior_plugin_default' => NULL, ]); // Now we should not get the default value.