You are here

public function RevisionOverviewForm::buildForm in Support Ticketing System 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

modules/support_ticket/src/Form/RevisionOverviewForm.php, line 106
Contains \Drupal\support_ticket\Form\RevisionOverviewForm

Class

RevisionOverviewForm
Provides a form for revision overview page.

Namespace

Drupal\support_ticket\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $support_ticket = NULL) {
  $account = $this->currentUser;
  $support_ticket_store = $this->entityManager
    ->getStorage('support_ticket');
  $type = $support_ticket
    ->getType();
  $vids = array_reverse($support_ticket_store
    ->revisionIds($support_ticket));
  $revision_count = count($vids);
  $build = array(
    '#title' => $this
      ->t('Revisions for %title', array(
      '%title' => $support_ticket
        ->label(),
    )),
    'stid' => array(
      '#type' => 'hidden',
      '#value' => $support_ticket->stid->value,
    ),
  );
  $table_header = array(
    'revision' => $this
      ->t('Revision'),
    'operations' => $this
      ->t('Operations'),
  );

  // Allow comparisons only if there are 2 or more revisions.
  if ($revision_count > 1) {
    $table_header += array(
      'select_column_one' => '',
      'select_column_two' => '',
    );
  }
  $rev_revert_perm = $account
    ->hasPermission("revert {$type} revisions") || $account
    ->hasPermission('revert all revisions') || $account
    ->hasPermission('administer support tickets');
  $rev_delete_perm = $account
    ->hasPermission("delete {$type} revisions") || $account
    ->hasPermission('delete all revisions') || $account
    ->hasPermission('administer support tickets');
  $revert_permission = $rev_revert_perm && $support_ticket
    ->access('update');
  $delete_permission = $rev_delete_perm && $support_ticket
    ->access('delete');

  // Contains the table listing the revisions.
  $build['support_ticket_revisions_table'] = array(
    '#type' => 'table',
    '#header' => $table_header,
    '#attributes' => array(
      'class' => array(
        'diff-revisions',
      ),
    ),
  );
  $build['support_ticket_revisions_table']['#attached']['library'][] = 'diff/diff.general';
  $build['support_ticket_revisions_table']['#attached']['drupalSettings']['diffRevisionRadios'] = $this->config
    ->get('general_settings.radio_behavior');

  // Add rows to the table.
  foreach ($vids as $vid) {
    if ($revision = $support_ticket_store
      ->loadRevision($vid)) {

      // Markup for revision log.
      if ($revision->revision_log->value != '') {
        $revision_log = '<p class="revision-log">' . Xss::filter($revision->revision_log->value) . '</p>';
      }
      else {
        $revision_log = '';
      }

      // Username to be rendered.
      $username = array(
        '#theme' => 'username',
        '#account' => $revision->uid->entity,
      );
      $revision_date = $this->date
        ->format($revision
        ->getRevisionCreationTime(), 'short');

      // Default revision.
      if ($revision
        ->isDefaultRevision()) {
        $date_username_markup = $this
          ->t('!date by !username', array(
          '!date' => $this
            ->l($revision_date, Url::fromRoute('entity.support_ticket.canonical', array(
            'support_ticket' => $support_ticket
              ->id(),
          ))),
          '!username' => $this->renderer
            ->render($username),
        ));
        $row = array(
          'revision' => array(
            '#markup' => $date_username_markup . $revision_log,
          ),
          'operations' => array(
            '#markup' => SafeMarkup::format('%placeholder', array(
              '%placeholder' => $this
                ->t('current revision'),
            )),
          ),
          '#attributes' => array(
            'class' => array(
              'revision-current',
            ),
          ),
        );

        // Allow comparisons only if there are 2 or more revisions.
        if ($revision_count > 1) {
          $row += array(
            'select_column_one' => array(
              '#type' => 'radio',
              '#title_display' => 'invisible',
              '#name' => 'radios_left',
              '#return_value' => $vid,
              '#default_value' => FALSE,
            ),
            'select_column_two' => array(
              '#type' => 'radio',
              '#title_display' => 'invisible',
              '#name' => 'radios_right',
              '#default_value' => $vid,
              '#return_value' => $vid,
            ),
          );
        }
      }
      else {
        $route_params = array(
          'support_ticket' => $support_ticket
            ->id(),
          'support_ticket_revision' => $vid,
        );

        // Add links based on permissions.
        if ($revert_permission) {
          $links['revert'] = array(
            'title' => $this
              ->t('Revert'),
            'url' => Url::fromRoute('support_ticket.revision_revert_confirm', $route_params),
          );
        }
        if ($delete_permission) {
          $links['delete'] = array(
            'title' => $this
              ->t('Delete'),
            'url' => Url::fromRoute('support_ticket.revision_delete_confirm', $route_params),
          );
        }
        $date_username_markup = $this
          ->t('!date by !username', array(
          '!date' => $this
            ->l($revision_date, Url::fromRoute('entity.support_ticket.revision', $route_params)),
          '!username' => $this->renderer
            ->render($username),
        ));

        // Here we don't have to deal with 'only one revision' case because
        // if there's only one revision it will also be the default one,
        // entering on the first branch of this if else statement.
        $row = array(
          'revision' => array(
            '#markup' => $date_username_markup . $revision_log,
          ),
          'select_column_one' => array(
            '#type' => 'radio',
            '#title_display' => 'invisible',
            '#name' => 'radios_left',
            '#return_value' => $vid,
            '#default_value' => isset($vids[1]) ? $vids[1] : FALSE,
          ),
          'select_column_two' => array(
            '#type' => 'radio',
            '#title_display' => 'invisible',
            '#name' => 'radios_right',
            '#return_value' => $vid,
            '#default_value' => FALSE,
          ),
          'operations' => array(
            '#type' => 'operations',
            '#links' => $links,
          ),
        );
      }

      // Add the row to the table.
      $build['support_ticket_revisions_table'][] = $row;
    }
  }

  // Allow comparisons only if there are 2 or more revisions.
  if ($revision_count > 1) {
    $build['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Compare'),
      '#attributes' => array(
        'class' => array(
          'diff-button',
        ),
      ),
    );
  }
  return $build;
}