You are here

public function DeleteMultiple::buildForm in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/node/src/Form/DeleteMultiple.php \Drupal\node\Form\DeleteMultiple::buildForm()

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

core/modules/node/src/Form/DeleteMultiple.php, line 98
Contains \Drupal\node\Form\DeleteMultiple.

Class

DeleteMultiple
Provides a node deletion confirmation form.

Namespace

Drupal\node\Form

Code

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

  /** @var \Drupal\node\NodeInterface[] $nodes */
  $nodes = $this->storage
    ->loadMultiple(array_keys($this->nodeInfo));
  $items = [];
  foreach ($this->nodeInfo as $id => $langcodes) {
    foreach ($langcodes as $langcode) {
      $node = $nodes[$id]
        ->getTranslation($langcode);
      $key = $id . ':' . $langcode;
      $default_key = $id . ':' . $node
        ->getUntranslated()
        ->language()
        ->getId();

      // If we have a translated entity we build a nested list of translations
      // that will be deleted.
      $languages = $node
        ->getTranslationLanguages();
      if (count($languages) > 1 && $node
        ->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 content translations will be deleted:</em>', [
              '@label' => $node
                ->label(),
            ]),
          ],
          'deleted_translations' => [
            '#theme' => 'item_list',
            '#items' => $names,
          ],
        ];
      }
      elseif (!isset($items[$default_key])) {
        $items[$key] = $node
          ->label();
      }
    }
  }
  $form['nodes'] = array(
    '#theme' => 'item_list',
    '#items' => $items,
  );
  $form = parent::buildForm($form, $form_state);
  return $form;
}