/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { $original_widget = $this->getCurrentWidget($form_state); if ($new_widget = $this->entity_browser->getWidgetSelector()->submit($form, $form_state)) { $this->setCurrentWidget($new_widget, $form_state); } // Only call widget submit if we didn't change the widget. if ($original_widget == $this->getCurrentWidget($form_state)) { $this->entity_browser->getWidgets()->get($this->getCurrentWidget($form_state))->submit($form[$form['#browser_parts']['widget']], $form, $form_state); $this->entity_browser->getSelectionDisplay()->submit($form, $form_state); } if (!$this->isSelectionCompleted($form_state)) { $form_state->setRebuild(); } else { $this->entity_browser->getDisplay()->selectionCompleted($this->getSelectedEntities($form_state)); } }
/** * Form constructor for the entity selection step. * * @param array $form * An associative array containing the structure of the form. * @param \Drupal\Core\Form\FormStateInterface $form_state * The current state of the form. * * @return array * The form structure. */ public function buildSelectStep(array &$form, FormStateInterface $form_state) { $entity_element = $form_state->get('entity_element'); /** @var \Drupal\embed\EmbedButtonInterface $embed_button */ $embed_button = $form_state->get('embed_button'); $entity = $form_state->get('entity'); $form['attributes']['data-entity-type'] = array('#type' => 'value', '#value' => $entity_element['data-entity-type']); $label = $this->t('Label'); // Attempt to display a better label if we can by getting it from // the label field definition. $entity_type = $this->entityTypeManager->getDefinition($entity_element['data-entity-type']); if ($entity_type->isSubclassOf('\\Drupal\\Core\\Entity\\FieldableEntityInterface') && $entity_type->hasKey('label')) { $field_definitions = $this->entityManager()->getBaseFieldDefinitions($entity_type->id()); if (isset($field_definitions[$entity_type->getKey('label')])) { $label = $field_definitions[$entity_type->getKey('label')]->getLabel(); } } $form['#title'] = $this->t('Select @type to embed', array('@type' => $entity_type->getLowercaseLabel())); if ($this->entityBrowser) { $this->eventDispatcher->addListener(Events::REGISTER_JS_CALLBACKS, [$this, 'registerJSCallback']); $form['attributes']['entity_browser']['#theme_wrappers'] = ['container']; $form['attributes']['entity_browser']['browser'] = $this->entityBrowser->getDisplay()->displayEntityBrowser($form_state); $form['attributes']['entity_browser']['entity-id'] = ['#type' => 'hidden', '#default_value' => $entity ? $entity->id() : '', '#attributes' => ['class' => ['eb-target']]]; $form['#attached']['library'][] = 'entity_browser/common'; $form['#attached']['drupalSettings']['entity_browser'] = [$this->entityBrowser->getDisplay()->getUuid() => ['cardinality' => 1]]; $form['attributes']['data-entity-id'] = array('#type' => 'value', '#title' => $entity_element['data-entity-id']); } else { $form['attributes']['data-entity-id'] = array('#type' => 'entity_autocomplete', '#target_type' => $entity_element['data-entity-type'], '#title' => $label, '#default_value' => $entity, '#required' => TRUE, '#description' => $this->t('Type label and pick the right one from suggestions. Note that the unique ID will be saved.')); if ($bundles = $embed_button->getTypeSetting('bundles')) { $form['attributes']['data-entity-id']['#selection_settings']['target_bundles'] = $bundles; } } $form['attributes']['data-entity-uuid'] = array('#type' => 'value', '#title' => $entity_element['data-entity-uuid']); $form['actions'] = array('#type' => 'actions'); $form['actions']['save_modal'] = array('#type' => 'submit', '#value' => $this->t('Next'), '#button_type' => 'primary', '#submit' => array(), '#ajax' => array('callback' => '::submitSelectStep', 'event' => 'click'), '#attributes' => ['class' => ['js-button-next']]); return $form; }
/** * {@inheritdoc} */ public function getPlugin(EntityBrowserInterface $entity_browser) { return $entity_browser->getDisplay(); }