From d70217a0ee45b5d0d1e1ef7ef3cc38388aa2cc3a Mon Sep 17 00:00:00 2001 From: Aaron McGowan Date: Wed, 28 Jun 2017 12:16:01 -0400 Subject: [PATCH] #395: Ensure that target media bundles are specified in EntityFormProxy operations. --- .../EntityBrowser/Widget/EntityFormProxy.php | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/modules/lightning_features/lightning_media/src/Plugin/EntityBrowser/Widget/EntityFormProxy.php b/modules/lightning_features/lightning_media/src/Plugin/EntityBrowser/Widget/EntityFormProxy.php index cd464e98f..458426292 100644 --- a/modules/lightning_features/lightning_media/src/Plugin/EntityBrowser/Widget/EntityFormProxy.php +++ b/modules/lightning_features/lightning_media/src/Plugin/EntityBrowser/Widget/EntityFormProxy.php @@ -83,8 +83,9 @@ public function getForm(array &$original_form, FormStateInterface $form_state, a return $form; } + $bundles = $this->getTargetBundles($form_state); try { - $entity = $this->helper->createFromInput($value); + $entity = $this->helper->createFromInput($value, $bundles); } catch (IndeterminateBundleException $e) { return $form; @@ -126,8 +127,10 @@ public function validate(array &$form, FormStateInterface $form_state) { parent::validate($form, $form_state); $value = $this->getInputValue($form_state); + $bundles = $this->getTargetBundles($form_state); + try { - $this->helper->getBundleFromInput($value); + $this->helper->getBundleFromInput($value, TRUE, $bundles); } catch (IndeterminateBundleException $e) { $form_state->setError($form['widget'], $e->getMessage()); @@ -176,6 +179,24 @@ protected function getInputValue(FormStateInterface $form_state) { return $form_state->getValue('input'); } + /** + * Returns an array of bundles from the entity browser's widget context. + * + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The current form state. + * + * @return array + * The target media bundles. + */ + protected function getTargetBundles(FormStateInterface $form_state) { + $bundles = []; + $entity_browser_info = $form_state->get('entity_browser'); + if (!empty($entity_browser_info['widget_context']['target_bundles'])) { + $bundles = $entity_browser_info['widget_context']['target_bundles']; + } + return $bundles; + } + /** * {@inheritdoc} */