You are here

class EntityBrowserForm in Entity Browser 8

Same name and namespace in other branches
  1. 8.2 src/Form/EntityBrowserForm.php \Drupal\entity_browser\Form\EntityBrowserForm

The entity browser form.

Hierarchy

Expanded class hierarchy of EntityBrowserForm

File

src/Form/EntityBrowserForm.php, line 21

Namespace

Drupal\entity_browser\Form
View source
class EntityBrowserForm extends FormBase implements EntityBrowserFormInterface, BaseFormIdInterface {

  /**
   * UUID generator service.
   *
   * @var \Drupal\Component\Uuid\UuidInterface
   */
  protected $uuidGenerator;

  /**
   * The entity browser object.
   *
   * @var \Drupal\entity_browser\EntityBrowserInterface
   */
  protected $entityBrowser;

  /**
   * The entity browser selection storage.
   *
   * @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface
   */
  protected $selectionStorage;

  /**
   * The renderer service.
   *
   * @var \Drupal\Core\Render\RendererInterface
   */
  protected $renderer;

  /**
   * Constructs a EntityBrowserForm object.
   *
   * @param \Drupal\Component\Uuid\UuidInterface $uuid_generator
   *   The UUID generator service.
   * @param \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface $selection_storage
   *   Selection storage.
   * @param \Drupal\Core\Render\RendererInterface $renderer
   *   The renderer service.
   * @param \Drupal\Core\Messenger\MessengerInterface $messenger
   *   The messenger.
   */
  public function __construct(UuidInterface $uuid_generator, KeyValueStoreExpirableInterface $selection_storage, RendererInterface $renderer, MessengerInterface $messenger) {
    $this->uuidGenerator = $uuid_generator;
    $this->selectionStorage = $selection_storage;
    $this->renderer = $renderer;
    $this->messenger = $messenger;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('uuid'), $container
      ->get('entity_browser.selection_storage'), $container
      ->get('renderer'), $container
      ->get('messenger'));
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'entity_browser_' . $this->entityBrowser
      ->id() . '_form';
  }

  /**
   * {@inheritdoc}
   */
  public function getBaseFormId() {
    return 'entity_browser_form';
  }

  /**
   * {@inheritdoc}
   */
  public function setEntityBrowser(EntityBrowserInterface $entity_browser) {
    $this->entityBrowser = $entity_browser;
  }

  /**
   * {@inheritdoc}
   */
  public function getEntityBrowser() {
    return $this->entityBrowser;
  }

  /**
   * Initializes form state.
   *
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   Form state object.
   */
  protected function init(FormStateInterface $form_state) {

    // Flag that this form has been initialized.
    $form_state
      ->set('entity_form_initialized', TRUE);
    if ($this
      ->getRequest()->query
      ->has('uuid')) {
      $form_state
        ->set([
        'entity_browser',
        'instance_uuid',
      ], $this
        ->getRequest()->query
        ->get('uuid'));
    }
    else {
      $form_state
        ->set([
        'entity_browser',
        'instance_uuid',
      ], $this->uuidGenerator
        ->generate());
    }
    $form_state
      ->set([
      'entity_browser',
      'selected_entities',
    ], []);
    $form_state
      ->set([
      'entity_browser',
      'validators',
    ], []);
    $form_state
      ->set([
      'entity_browser',
      'widget_context',
    ], []);
    $form_state
      ->set([
      'entity_browser',
      'selection_completed',
    ], FALSE);

    // Initialize form state with persistent data, if present.
    if ($storage = $this->selectionStorage
      ->get($form_state
      ->get([
      'entity_browser',
      'instance_uuid',
    ]))) {
      foreach ($storage as $key => $value) {
        $form_state
          ->set([
          'entity_browser',
          $key,
        ], $value);
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {

    // During the initial form build, add this form object to the form state and
    // allow for initial preparation before form building and processing.
    if (!$form_state
      ->has('entity_form_initialized')) {
      $this
        ->init($form_state);
    }
    $this
      ->isFunctionalForm();
    $form['#attributes']['class'][] = 'entity-browser-form';
    if (!empty($form_state
      ->get([
      'entity_browser',
      'instance_uuid',
    ]))) {
      $form['#attributes']['data-entity-browser-uuid'] = $form_state
        ->get([
        'entity_browser',
        'instance_uuid',
      ]);
    }
    $form['#browser_parts'] = [
      'widget_selector' => 'widget_selector',
      'widget' => 'widget',
      'selection_display' => 'selection_display',
    ];
    if (!($current_widget_id = $this
      ->getCurrentWidget($form_state))) {
      $this->messenger
        ->addWarning($this
        ->t('No widgets are available.'));
      return $form;
    }
    $this->entityBrowser
      ->getWidgetSelector()
      ->setDefaultWidget($current_widget_id);
    $form[$form['#browser_parts']['widget_selector']] = $this->entityBrowser
      ->getWidgetSelector()
      ->getForm($form, $form_state);
    $widget = $this->entityBrowser
      ->getWidget($current_widget_id);
    if ($widget
      ->access()
      ->isAllowed()) {
      $form[$form['#browser_parts']['widget']] = $widget
        ->getForm($form, $form_state, $this->entityBrowser
        ->getAdditionalWidgetParameters());
    }
    else {
      $this->messenger
        ->addWarning($this
        ->t('Access to the widget forbidden.'));
    }

    // Add cache access cache metadata from the widgets to the form directly as
    // it is affected.
    foreach ($this->entityBrowser
      ->getWidgets() as $widget) {

      /** @var \Drupal\entity_browser\WidgetInterface $widget */
      $this->renderer
        ->addCacheableDependency($form, $widget
        ->access());
    }
    $form[$form['#browser_parts']['selection_display']] = $this->entityBrowser
      ->getSelectionDisplay()
      ->getForm($form, $form_state);
    if ($this->entityBrowser
      ->getDisplay() instanceof DisplayAjaxInterface) {
      $this->entityBrowser
        ->getDisplay()
        ->addAjax($form);
    }
    $form['#attached']['library'][] = 'entity_browser/entity_browser';
    return $form;
  }

  /**
   * Check if entity browser with selected configuration combination can work.
   */
  protected function isFunctionalForm() {

    /** @var \Drupal\entity_browser\WidgetInterface $widget */
    foreach ($this->entityBrowser
      ->getWidgets() as $widget) {

      /** @var \Drupal\entity_browser\SelectionDisplayInterface $selectionDisplay */
      $selectionDisplay = $this->entityBrowser
        ->getSelectionDisplay();
      if ($widget
        ->requiresJsCommands() && !$selectionDisplay
        ->supportsJsCommands()) {
        throw new ConfigException('Used entity browser selection display cannot work in combination with settings defined for used selection widget.');
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $this->entityBrowser
      ->getWidgetSelector()
      ->validate($form, $form_state);
    $this->entityBrowser
      ->getWidgets()
      ->get($this
      ->getCurrentWidget($form_state))
      ->validate($form, $form_state);
    $this->entityBrowser
      ->getSelectionDisplay()
      ->validate($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $original_widget = $this
      ->getCurrentWidget($form_state);
    if ($new_widget = $this->entityBrowser
      ->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->entityBrowser
        ->getWidgets()
        ->get($this
        ->getCurrentWidget($form_state))
        ->submit($form[$form['#browser_parts']['widget']], $form, $form_state);
      $this->entityBrowser
        ->getSelectionDisplay()
        ->submit($form, $form_state);
    }
    if (!$this
      ->isSelectionCompleted($form_state)) {
      $form_state
        ->setRebuild();
    }
    else {
      $this->entityBrowser
        ->getDisplay()
        ->selectionCompleted($this
        ->getSelectedEntities($form_state));
    }
  }

  /**
   * Returns the widget that is currently selected.
   *
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   *
   * @return string
   *   ID of currently selected widget.
   */
  protected function getCurrentWidget(FormStateInterface $form_state) {

    // Do not use has() as that returns TRUE if the value is NULL.
    if (!$form_state
      ->get('entity_browser_current_widget')) {
      $form_state
        ->set('entity_browser_current_widget', $this->entityBrowser
        ->getFirstWidget());
    }
    return $form_state
      ->get('entity_browser_current_widget');
  }

  /**
   * Sets widget that is currently active.
   *
   * @param string $widget
   *   New active widget UUID.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   Form state.
   */
  protected function setCurrentWidget($widget, FormStateInterface $form_state) {
    $form_state
      ->set('entity_browser_current_widget', $widget);
  }

  /**
   * Indicates selection is done.
   *
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   Form state.
   *
   * @return bool
   *   Indicates selection is done.
   */
  protected function isSelectionCompleted(FormStateInterface $form_state) {
    return (bool) $form_state
      ->get([
      'entity_browser',
      'selection_completed',
    ]);
  }

  /**
   * Returns currently selected entities.
   *
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   Form state.
   *
   * @return \Drupal\Core\Entity\EntityInterface[]
   *   Array of currently selected entities.
   */
  protected function getSelectedEntities(FormStateInterface $form_state) {
    return $form_state
      ->get([
      'entity_browser',
      'selected_entities',
    ]);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
EntityBrowserForm::$entityBrowser protected property The entity browser object.
EntityBrowserForm::$renderer protected property The renderer service.
EntityBrowserForm::$selectionStorage protected property The entity browser selection storage.
EntityBrowserForm::$uuidGenerator protected property UUID generator service.
EntityBrowserForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
EntityBrowserForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
EntityBrowserForm::getBaseFormId public function Returns a string identifying the base form. Overrides BaseFormIdInterface::getBaseFormId
EntityBrowserForm::getCurrentWidget protected function Returns the widget that is currently selected.
EntityBrowserForm::getEntityBrowser public function Returns the entity browser entity. Overrides EntityBrowserFormInterface::getEntityBrowser
EntityBrowserForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
EntityBrowserForm::getSelectedEntities protected function Returns currently selected entities.
EntityBrowserForm::init protected function Initializes form state.
EntityBrowserForm::isFunctionalForm protected function Check if entity browser with selected configuration combination can work.
EntityBrowserForm::isSelectionCompleted protected function Indicates selection is done.
EntityBrowserForm::setCurrentWidget protected function Sets widget that is currently active.
EntityBrowserForm::setEntityBrowser public function Sets entity browser entity. Overrides EntityBrowserFormInterface::setEntityBrowser
EntityBrowserForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
EntityBrowserForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
EntityBrowserForm::__construct public function Constructs a EntityBrowserForm object.
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.