You are here

public function DeleteMultiple::buildForm in Media entity 8

Form constructor.

Parameters

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

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

Return value

array The form structure.

Overrides ConfirmFormBase::buildForm

File

src/Form/DeleteMultiple.php, line 96

Class

DeleteMultiple
Provides a media deletion confirmation form.

Namespace

Drupal\media_entity\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $this->entityInfo = $this->tempStoreFactory
    ->get('media_multiple_delete_confirm')
    ->get(\Drupal::currentUser()
    ->id());
  if (empty($this->entityInfo)) {
    return new RedirectResponse($this
      ->getCancelUrl()
      ->setAbsolute()
      ->toString());
  }

  /** @var \Drupal\media_entity\MediaInterface[] $entities */
  $entities = $this->storage
    ->loadMultiple(array_keys($this->entityInfo));
  $items = [];
  foreach ($this->entityInfo as $id => $langcodes) {
    foreach ($langcodes as $langcode) {
      $entity = $entities[$id]
        ->getTranslation($langcode);
      $key = $id . ':' . $langcode;
      $default_key = $id . ':' . $entity
        ->getUntranslated()
        ->language()
        ->getId();

      // If we have a translated entity we build a nested list of translations
      // that will be deleted.
      $languages = $entity
        ->getTranslationLanguages();
      if (count($languages) > 1 && $entity
        ->isDefaultTranslation()) {
        $names = [];
        foreach ($languages as $translation_langcode => $language) {
          $names[] = $language
            ->getName();
          unset($items[$id . ':' . $translation_langcode]);
        }
        $items[$default_key] = [
          'label' => [
            '#markup' => $this
              ->t('@label (Original translation) - <em>The following media translations will be deleted:</em>', [
              '@label' => $entity
                ->label(),
            ]),
          ],
          'deleted_translations' => [
            '#theme' => 'item_list',
            '#items' => $names,
          ],
        ];
      }
      elseif (!isset($items[$default_key])) {
        $items[$key] = $entity
          ->label();
      }
    }
  }
  $form['entities'] = [
    '#theme' => 'item_list',
    '#items' => $items,
  ];
  $form = parent::buildForm($form, $form_state);
  return $form;
}