Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / entity_browser / src / WidgetBase.php
index e2479dfc31b4e994256ec5e35ddd3c07ac01506d..f5d2ea7a895d7f9396a7010c8e04bdc682dc73ee 100644 (file)
@@ -3,6 +3,7 @@
 namespace Drupal\entity_browser;
 
 use Drupal\Component\Utility\NestedArray;
+use Drupal\Core\Access\AccessResult;
 use Drupal\Core\Entity\EntityTypeManagerInterface;
 use Drupal\Core\Plugin\PluginBase;
 use Drupal\Core\Form\FormStateInterface;
@@ -112,15 +113,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 +355,29 @@ 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];
+      }
+    }
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function access() {
+    return AccessResult::allowed();
+  }
+
 }