You are here

public function EntityEmbedDialog::validateSelectStep in Entity Embed 8

Form validation handler for the entity selection step.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

1 call to EntityEmbedDialog::validateSelectStep()
EntityEmbedDialog::validateForm in src/Form/EntityEmbedDialog.php
Form validation handler.

File

src/Form/EntityEmbedDialog.php, line 583

Class

EntityEmbedDialog
Provides a form to embed entities by specifying data attributes.

Namespace

Drupal\entity_embed\Form

Code

public function validateSelectStep(array $form, FormStateInterface $form_state) {
  if ($form_state
    ->hasValue([
    'entity_browser',
    'entities',
  ])) {
    if (count($form_state
      ->getValue([
      'entity_browser',
      'entities',
    ])) > 0) {
      $id = $form_state
        ->getValue([
        'entity_browser',
        'entities',
        0,
      ])
        ->id();
    }
    $element = $form['entity_browser'];
  }
  else {
    $id = trim($form_state
      ->getValue([
      'entity_id',
    ]));
    $element = $form['entity_id'];
  }
  $entity_type = $form_state
    ->getValue([
    'attributes',
    'data-entity-type',
  ]);
  if (!isset($id)) {
    $form_state
      ->setError($element, $this
      ->t('No entity selected.'));
    return;
  }
  if ($entity = $this->entityTypeManager
    ->getStorage($entity_type)
    ->load($id)) {
    if (!$entity
      ->access('view')) {
      $form_state
        ->setError($element, $this
        ->t('Unable to access @type entity @id.', [
        '@type' => $entity_type,
        '@id' => $id,
      ]));
    }
    else {
      if ($uuid = $entity
        ->uuid()) {
        $form_state
          ->setValueForElement($form['attributes']['data-entity-uuid'], $uuid);
      }
      else {
        $form_state
          ->setError($element, $this
          ->t('Cannot embed @type entity @id because it does not have a UUID.', [
          '@type' => $entity_type,
          '@id' => $id,
        ]));
      }

      // Ensure that at least one Entity Embed Display plugin is present
      // before proceeding to the next step. Raise an error otherwise.
      $embed_button = $form_state
        ->get('embed_button');
      $display_plugin_options = $this
        ->getDisplayPluginOptions($embed_button, $entity);

      // If no plugin is available after taking the intersection, raise error.
      // Also log an exception.
      if (empty($display_plugin_options)) {
        $form_state
          ->setError($element, $this
          ->t('No display options available for the selected %entity-type. Please select another %entity_type.', [
          '%entity_type' => $entity
            ->getEntityType()
            ->getLabel(),
        ]));
        $this
          ->logger('entity_embed')
          ->warning('No display options available for "@type:" entity "@id" while embedding using button "@button". Please ensure that at least one Entity Embed Display plugin is allowed for this embed button which is available for this entity.', [
          '@type' => $entity_type,
          '@id' => $entity
            ->id(),
          '@button' => $embed_button
            ->id(),
        ]);
      }
    }
  }
  else {
    $form_state
      ->setError($element, $this
      ->t('Unable to load @type entity @id.', [
      '@type' => $entity_type,
      '@id' => $id,
    ]));
  }
}