X-Git-Url: http://aleph1.co.uk/gitweb/?a=blobdiff_plain;f=web%2Fmodules%2Fcontrib%2Fentity_browser%2Fsrc%2FWidgetBase.php;h=da094e4cf556b78840f0bf17130c98076d9ca7de;hb=2257eb96fa3afedcfba62207e838f49ee9c757e2;hp=e2479dfc31b4e994256ec5e35ddd3c07ac01506d;hpb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;p=yaffs-website diff --git a/web/modules/contrib/entity_browser/src/WidgetBase.php b/web/modules/contrib/entity_browser/src/WidgetBase.php index e2479dfc3..da094e4cf 100644 --- a/web/modules/contrib/entity_browser/src/WidgetBase.php +++ b/web/modules/contrib/entity_browser/src/WidgetBase.php @@ -112,15 +112,8 @@ abstract class WidgetBase extends PluginBase implements WidgetInterface, Contain public function getForm(array &$original_form, FormStateInterface $form_state, array $additional_widget_parameters) { $form = []; - // Allow configuration overrides at runtime based on form state to enable - // use cases where the instance of a widget may have contextual - // configuration like field settings. "widget_context" doesn't have to be - // used in this way, if a widget doesn't want its default configuration - // overwritten it can not call this method and implement its own logic. - foreach ($this->defaultConfiguration() as $key => $value) { - if ($form_state->has(['entity_browser', 'widget_context', $key]) && isset($this->configuration[$key])) { - $this->configuration[$key] = $form_state->get(['entity_browser', 'widget_context', $key]); - } + if ($form_state->has(['entity_browser', 'widget_context'])) { + $this->handleWidgetContext($form_state->get(['entity_browser', 'widget_context'])); } // Check if widget supports auto select functionality and expose config to @@ -361,4 +354,22 @@ abstract class WidgetBase extends PluginBase implements WidgetInterface, Contain return $this->getPluginDefinition()['auto_select'] && $this->getConfiguration()['settings']['auto_select']; } + /** + * Allow configuration overrides at runtime based on widget context passed to + * this widget from the Entity Browser element. + * + * Widgets can override this method to replace the default behavior of + * replacing configuration with widget context if array keys match. + * + * @param array $widget_context + * The widget context. + */ + protected function handleWidgetContext($widget_context) { + foreach ($this->defaultConfiguration() as $key => $value) { + if (isset($widget_context[$key]) && isset($this->configuration[$key])) { + $this->configuration[$key] = $widget_context[$key]; + } + } + } + }