Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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,
* )
* }
* )
*/
Expand All @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand All @@ -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.
Expand Down