/**
  * Build the default links (Read more) for a support ticket.
  *
  * @param \Drupal\support_ticket\SupportTicketInterface $entity
  *   The support ticket object.
  * @param string $view_mode
  *   A view mode identifier.
  *
  * @return array
  *   An array that can be processed by drupal_pre_render_links().
  */
 protected static function buildLinks(SupportTicketInterface $entity, $view_mode)
 {
     $links = array();
     // Always display a read more link on teasers because we have no way
     // to know when a teaser view is different than a full view.
     if ($view_mode == 'teaser') {
         $support_ticket_title_stripped = strip_tags($entity->label());
         $links['support_ticket-readmore'] = array('title' => t('Read more<span class="visually-hidden"> about @title</span>', array('@title' => $support_ticket_title_stripped)), 'url' => $entity->urlInfo(), 'language' => $entity->language(), 'attributes' => array('rel' => 'tag', 'title' => $support_ticket_title_stripped));
     }
     return array('#theme' => 'links__support_ticket__support_ticket', '#links' => $links, '#attributes' => array('class' => array('links', 'inline')));
 }
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, FormStateInterface $form_state)
 {
     $this->supportTicketStorage->deleteRevision($this->revision->getRevisionId());
     $this->logger('content')->notice('@type: deleted %title revision %revision.', array('@type' => $this->revision->bundle(), '%title' => $this->revision->label(), '%revision' => $this->revision->getRevisionId()));
     $support_ticket_type = $this->supportTicketTypeStorage->load($this->revision->bundle())->label();
     drupal_set_message(t('Revision from %revision-date of @type %title has been deleted.', array('%revision-date' => format_date($this->revision->getRevisionCreationTime()), '@type' => $support_ticket_type, '%title' => $this->revision->label())));
     $form_state->setRedirect('entity.support_ticket.canonical', array('support_ticket' => $this->revision->id()));
     if ($this->connection->query('SELECT COUNT(DISTINCT vid) FROM {support_ticket_field_revision} WHERE stid = :stid', array(':stid' => $this->revision->id()))->fetchField() > 1) {
         $form_state->setRedirect('entity.support_ticket.version_history', array('support_ticket' => $this->revision->id()));
     }
 }
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, FormStateInterface $form_state)
 {
     $revision = $this->prepareRevertedRevision($this->revision);
     // The revision timestamp will be updated when the revision is saved. Keep the
     // original one for the confirmation message.
     $original_revision_timestamp = $revision->getRevisionCreationTime();
     $revision->revision_log = t('Copy of the revision from %date.', array('%date' => format_date($original_revision_timestamp)));
     $revision->save();
     $this->logger('content')->notice('@type: reverted %title revision %revision.', array('@type' => $this->revision->bundle(), '%title' => $this->revision->label(), '%revision' => $this->revision->getRevisionId()));
     drupal_set_message(t('@type %title has been reverted to the revision from %revision-date.', array('@type' => support_ticket_get_type_label($this->revision), '%title' => $this->revision->label(), '%revision-date' => format_date($original_revision_timestamp))));
     $form_state->setRedirect('entity.support_ticket.version_history', array('support_ticket' => $this->revision->id()));
 }
 /**
  * Generates an overview table of older revisions of a support ticket.
  *
  * @param \Drupal\support_ticket\SupportTicketInterface $support_ticket
  *   A support_ticket object.
  *
  * @return array
  *   An array as expected by drupal_render().
  */
 public function revisionOverview(SupportTicketInterface $support_ticket)
 {
     $account = $this->currentUser();
     $support_ticket_storage = $this->entityManager()->getStorage('support_ticket');
     $type = $support_ticket->getType();
     $build = array();
     $build['#title'] = $this->t('Revisions for %title', array('%title' => $support_ticket->label()));
     $header = array($this->t('Revision'), $this->t('Operations'));
     $revert_permission = ($account->hasPermission("revert {$type} revisions") || $account->hasPermission('revert all revisions') || $account->hasPermission('administer support tickets')) && $support_ticket->access('update');
     $delete_permission = ($account->hasPermission("delete {$type} revisions") || $account->hasPermission('delete all revisions') || $account->hasPermission('administer support tickets')) && $support_ticket->access('delete');
     $rows = array();
     $vids = $support_ticket_storage->revisionIds($support_ticket);
     foreach (array_reverse($vids) as $vid) {
         $revision = $support_ticket_storage->loadRevision($vid);
         $username = ['#theme' => 'username', '#account' => $revision->uid->entity];
         // Use revision link to link to revisions that are not active.
         $date = $this->dateFormatter->format($revision->revision_timestamp->value, 'short');
         if ($vid != $support_ticket->getRevisionId()) {
             $link = $this->l($date, new Url('entity.support_ticket.revision', ['support_ticket' => $support_ticket->id(), 'support_ticket_revision' => $vid]));
         } else {
             $link = $support_ticket->link($date);
         }
         $row = [];
         $column = ['data' => ['#type' => 'inline_template', '#template' => '{% trans %}{{ date }} by {{ username }}{% endtrans %}{% if message %}<p class="revision-log">{{ message }}</p>{% endif %}', '#context' => ['date' => $link, 'username' => $this->renderer->renderPlain($username), 'message' => ['#markup' => $revision->revision_log->value]]]];
         // @todo Simplify once https://www.drupal.org/node/2334319 lands.
         $this->renderer->addCacheableDependency($column['data'], $username);
         $row[] = $column;
         if ($vid == $support_ticket->getRevisionId()) {
             $row[0]['class'] = ['revision-current'];
             $row[] = ['data' => ['#prefix' => '<em>', '#markup' => $this->t('current revision'), '#suffix' => '</em>'], 'class' => ['revision-current']];
         } else {
             $links = [];
             if ($revert_permission) {
                 $links['revert'] = ['title' => $this->t('Revert'), 'url' => Url::fromRoute('support_ticket.revision_revert_confirm', ['support_ticket' => $support_ticket->id(), 'support_ticket_revision' => $vid])];
             }
             if ($delete_permission) {
                 $links['delete'] = ['title' => $this->t('Delete'), 'url' => Url::fromRoute('support_ticket.revision_delete_confirm', ['support_ticket' => $support_ticket->id(), 'support_ticket_revision' => $vid])];
             }
             $row[] = ['data' => ['#type' => 'operations', '#links' => $links]];
         }
         $rows[] = $row;
     }
     $build['support_ticket_revisions_table'] = array('#theme' => 'table', '#rows' => $rows, '#header' => $header, '#attached' => array('library' => array('support_ticket/drupal.support_ticket.admin')));
     return $build;
 }
 /**
  * Returns a table which shows the differences between two support ticket revisions.
  *
  * @param SupportTicketInterface $support_ticket
  *   The support ticket whose revisions are compared.
  * @param $left_vid
  *   Vid of the support ticket revision from the left.
  * @param $right_vid
  *   Vid of the support ticket revision from the right.
  * @param $filter
  *   If $filter == 'raw' raw text is compared (including html tags)
  *   If filter == 'raw-plain' markdown function is applied to the text before comparison.
  *
  * @return array
  *   Table showing the diff between the two support ticket revisions.
  */
 public function compareSupportTicketRevisions(SupportTicketInterface $support_ticket, $left_vid, $right_vid, $filter)
 {
     $diff_rows = array();
     $build = array('#title' => $this->t('Revisions for %title', array('%title' => $support_ticket->label())));
     if (!in_array($filter, array('raw', 'raw-plain'))) {
         $filter = 'raw';
     } elseif ($filter == 'raw-plain') {
         $filter = 'raw_plain';
     }
     // Support Ticket storage service.
     $storage = $this->entityManager()->getStorage('support_ticket');
     $left_revision = $storage->loadRevision($left_vid);
     $right_revision = $storage->loadRevision($right_vid);
     $vids = $storage->revisionIds($support_ticket);
     $diff_rows[] = $this->buildRevisionsNavigation($support_ticket->id(), $vids, $left_vid, $right_vid);
     $diff_rows[] = $this->buildMarkdownNavigation($support_ticket->id(), $left_vid, $right_vid, $filter);
     $diff_header = $this->buildTableHeader($left_revision, $right_revision);
     // Perform comparison only if both support ticket revisions loaded successfully.
     if ($left_revision != FALSE && $right_revision != FALSE) {
         $fields = $this->compareRevisions($left_revision, $right_revision);
         $support_ticket_base_fields = $this->entityManager()->getBaseFieldDefinitions('support_ticket');
         // Check to see if we need to display certain fields or not based on
         // selected view mode display settings.
         foreach ($fields as $field_name => $field) {
             // If we are dealing with support tickets only compare those fields
             // set as visible from the selected view mode.
             $view_mode = $this->config->get('support_ticket_type_settings.' . $support_ticket->getType() . '.view_mode');
             // If no view mode is selected use the default view mode.
             if ($view_mode == NULL) {
                 $view_mode = 'default';
             }
             $visible = entity_get_display('support_ticket', $support_ticket->getType(), $view_mode)->getComponent($field_name);
             if ($visible == NULL && !array_key_exists($field_name, $support_ticket_base_fields)) {
                 unset($fields[$field_name]);
             }
         }
         // Build the diff rows for each field and append the field rows
         // to the table rows.
         foreach ($fields as $field) {
             $field_label_row = '';
             if (!empty($field['#name'])) {
                 $field_label_row = array('data' => $this->t('Changes to %name', array('%name' => $field['#name'])), 'colspan' => 4, 'class' => array('field-name'));
             }
             $field_diff_rows = $this->getRows($field['#states'][$filter]['#left'], $field['#states'][$filter]['#right']);
             // Add the field label to the table only if there are changes to that field.
             if (!empty($field_diff_rows) && !empty($field_label_row)) {
                 $diff_rows[] = array($field_label_row);
             }
             // Add field diff rows to the table rows.
             $diff_rows = array_merge($diff_rows, $field_diff_rows);
         }
         // Add the CSS for the diff.
         $build['#attached']['library'][] = 'diff/diff.general';
         $theme = $this->config->get('general_settings.theme');
         if ($theme) {
             if ($theme == 'default') {
                 $build['#attached']['library'][] = 'diff/diff.default';
             } elseif ($theme == 'github') {
                 $build['#attached']['library'][] = 'diff/diff.github';
             }
         } elseif ($theme == NULL) {
             $build['#attached']['library'][] = 'diff/diff.github';
         }
         $build['diff'] = array('#type' => 'table', '#header' => $diff_header, '#rows' => $diff_rows, '#empty' => $this->t('No visible changes'), '#attributes' => array('class' => array('diff')));
         $build['back'] = array('#type' => 'link', '#attributes' => array('class' => array('button', 'diff-button')), '#title' => $this->t('Back to Revision Overview'), '#url' => Url::fromRoute('entity.support_ticket.version_history', ['support_ticket' => $support_ticket->id()]));
         return $build;
     } else {
         // @todo When task 'Convert drupal_set_message() to a service' (2278383)
         //   will be merged use the corresponding service instead.
         drupal_set_message($this->t('Selected support ticket revisions could not be loaded.'), 'error');
     }
 }