You are here

public function CandidateRevisionsNodeForm::buildForm in Node Revision Delete 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 FormInterface::buildForm

File

src/Form/CandidateRevisionsNodeForm.php, line 99

Class

CandidateRevisionsNodeForm
Class CandidateRevisionsForm.

Namespace

Drupal\node_revision_delete\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $node_type = NULL, NodeInterface $node = NULL) {

  // Table header.
  $header = [
    $this
      ->t('Revision ID'),
    [
      'data' => $this
        ->t('Revision'),
      // Hide the description on narrow width devices.
      'class' => [
        RESPONSIVE_PRIORITY_MEDIUM,
      ],
    ],
    [
      'data' => $this
        ->t('Operations'),
      // Hide the Operations on narrow width devices.
      'class' => [
        RESPONSIVE_PRIORITY_MEDIUM,
      ],
    ],
  ];

  // Getting the nid.
  $nid = $node
    ->id();

  // Getting the node revisions.
  $revisions = $this->nodeRevisionDelete
    ->getCandidatesRevisionsByNids([
    $nid,
  ]);
  $rows = [];
  foreach ($revisions as $revision) {

    // Loading the revisions.

    /** @var \Drupal\Core\Entity\RevisionLogInterface $revision */
    $revision = $this->entityTypeManager
      ->getStorage('node')
      ->loadRevision($revision);
    $username = [
      '#theme' => 'username',
      '#account' => $revision
        ->getRevisionUser(),
    ];

    // Build link to view revision.
    $date = $this->dateFormatter
      ->format($revision->revision_timestamp->value, 'short');
    $revision_url = new Url('entity.node.revision', [
      'node' => $revision
        ->id(),
      'node_revision' => $revision
        ->getRevisionId(),
    ]);
    $revision_link = Link::fromTextAndUrl($date, $revision_url)
      ->toRenderable();
    $revision_info = [
      '#type' => 'inline_template',
      '#template' => '{% trans %}{{ date }} by {{ username }}{% endtrans %}{% if message %}<p class="revision-log">{{ message }}</p>{% endif %}',
      '#context' => [
        'date' => $this->renderer
          ->renderPlain($revision_link),
        'username' => $this->renderer
          ->renderPlain($username),
        'message' => [
          '#markup' => $revision->revision_log->value,
          '#allowed_tags' => Xss::getHtmlTagList(),
        ],
      ],
    ];

    // Getting the vid.
    $vid = $revision
      ->getRevisionId();

    // The route parameters.
    $route_parameters_destination = [
      'node_type' => $node_type,
      'node' => $nid,
    ];

    // Return to the same page after save the content type.
    $destination = Url::fromRoute('node_revision_delete.candidate_revisions_node', $route_parameters_destination)
      ->toString();
    $destination_options = [
      'query' => [
        'destination' => $destination,
      ],
    ];

    // The route parameters.
    $route_parameters_dropbutton = [
      'node' => $nid,
      'node_revision' => $vid,
    ];
    $dropbutton = [
      '#type' => 'dropbutton',
      '#links' => [
        // Action to delete revisions.
        'delete' => [
          'title' => $this
            ->t('Delete'),
          'url' => Url::fromRoute('node.revision_delete_confirm', $route_parameters_dropbutton, $destination_options),
        ],
      ],
    ];
    $rows[$vid] = [
      $vid,
      [
        'data' => $revision_info,
      ],
      [
        'data' => $dropbutton,
      ],
    ];
  }
  $node_url = $node
    ->toUrl()
    ->toString();
  $caption = $this
    ->t('Candidates revisions for node <a href=":url">%title</a>', [
    ':url' => $node_url,
    '%title' => $node
      ->getTitle(),
  ]);
  $form['candidate_revisions'] = [
    '#type' => 'tableselect',
    '#caption' => $caption,
    '#header' => $header,
    '#options' => $rows,
    '#empty' => $this
      ->t('There are not candidates revisions to be deleted.'),
    '#sticky' => TRUE,
  ];
  $form['actions']['#type'] = 'actions';
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Delete revisions'),
    '#button_type' => 'primary',
  ];

  // Adding donation text.
  $form['#prefix'] = Donation::getDonationText();
  return $form;
}