You are here

public function MediaDeleteMultipleConfirmForm::buildForm in Drupal 8

@todo Change to trait or base class when #2843395 is done.

Overrides ConfirmFormBase::buildForm

See also

https://www.drupal.org/node/2843395

File

core/modules/media/src/Form/MediaDeleteMultipleConfirmForm.php, line 107

Class

MediaDeleteMultipleConfirmForm
Provides a confirmation form to delete multiple media items at once.

Namespace

Drupal\media\Form

Code

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

  /** @var \Drupal\media\MediaInterface[] $entities */
  $entities = $this->storage
    ->loadMultiple(array_keys($this->mediaItems));
  $items = [];
  foreach ($this->mediaItems 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 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,
  ];
  return parent::buildForm($form, $form_state);
}