View source
<?php
namespace Drupal\pet\Form;
use Drupal\Core\Link;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\diff\Form\RevisionOverviewForm;
use Drupal\pet\Entity\PetInterface;
class PetDiffRevisionOverviewForm extends RevisionOverviewForm {
public function buildForm(array $form, FormStateInterface $form_state, $node = NULL, PetInterface $pet = NULL) {
$account = $this->currentUser;
$langcode = $pet
->language()
->getId();
$langname = $pet
->language()
->getName();
$languages = $pet
->getTranslationLanguages();
$has_translations = count($languages) > 1;
$pet_storage = $this->entityTypeManager
->getStorage('pet');
$pagerLimit = $this->config
->get('general_settings.revision_pager_limit');
$query = $this->entityQuery
->get('pet')
->condition($pet
->getEntityType()
->getKey('id'), $pet
->id())
->pager($pagerLimit)
->allRevisions()
->sort($pet
->getEntityType()
->getKey('revision'), 'DESC')
->execute();
$vids = array_keys($query);
$revision_count = count($vids);
$build['#title'] = $has_translations ? $this
->t('@langname revisions for %title', [
'@langname' => $langname,
'%title' => $pet
->label(),
]) : $this
->t('Revisions for %title', [
'%title' => $pet
->label(),
]);
$build['pid'] = array(
'#type' => 'hidden',
'#value' => $pet
->id(),
);
$table_header = [];
$table_header['revision'] = $this
->t('Revision');
if ($revision_count > 1) {
$table_header += array(
'select_column_one' => '',
'select_column_two' => '',
);
}
$table_header['operations'] = $this
->t('Operations');
$rev_revert_perm = $account
->hasPermission('revert pet revisions') || $account
->hasPermission('administer pet entities');
$rev_delete_perm = $account
->hasPermission('delete pet revisions') || $account
->hasPermission('administer pet entities');
$revert_permission = $rev_revert_perm && $pet
->access('update');
$delete_permission = $rev_delete_perm && $pet
->access('delete');
$build['node_revisions_table'] = array(
'#type' => 'table',
'#header' => $table_header,
'#attributes' => array(
'class' => array(
'diff-revisions',
),
),
);
$build['node_revisions_table']['#attached']['library'][] = 'diff/diff.general';
$build['node_revisions_table']['#attached']['drupalSettings']['diffRevisionRadios'] = $this->config
->get('general_settings.radio_behavior');
$default_revision = $pet
->getRevisionId();
foreach ($vids as $key => $vid) {
$previous_revision = NULL;
if (isset($vids[$key + 1])) {
$previous_revision = $pet_storage
->loadRevision($vids[$key + 1]);
}
if ($revision = $pet_storage
->loadRevision($vid)) {
if ($revision
->hasTranslation($langcode) && $revision
->getTranslation($langcode)
->isRevisionTranslationAffected()) {
$username = array(
'#theme' => 'username',
'#account' => $revision
->getRevisionUser(),
);
$revision_date = $this->date
->format($revision
->getRevisionCreationTime(), 'short');
if ($vid != $pet
->getRevisionId()) {
$link = Link::fromTextAndUrl($revision_date, new Url('entity.pet.revision', [
'pet' => $pet
->id(),
'pet_revision' => $vid,
]));
}
else {
$link = $pet
->toLink($revision_date);
}
if ($vid == $default_revision) {
$row = [
'revision' => $this
->buildRevision($link, $username, $revision, $previous_revision),
];
if ($revision_count > 1) {
$row += [
'select_column_one' => $this
->buildSelectColumn('radios_left', $vid, FALSE),
'select_column_two' => $this
->buildSelectColumn('radios_right', $vid, $vid),
];
}
$row['operations'] = array(
'#prefix' => '<em>',
'#markup' => $this
->t('Current revision'),
'#suffix' => '</em>',
'#attributes' => array(
'class' => array(
'revision-current',
),
),
);
$row['#attributes'] = [
'class' => [
'revision-current',
],
];
}
else {
$route_params = array(
'pet' => $pet
->id(),
'pet_revision' => $vid,
'langcode' => $langcode,
);
$links = array();
if ($revert_permission) {
$links['revert'] = [
'title' => $vid < $pet
->getRevisionId() ? $this
->t('Revert') : $this
->t('Set as current revision'),
'url' => $has_translations ? Url::fromRoute('entity.pet.revision_revert_translation_confirm', [
'pet' => $pet
->id(),
'pet_revision' => $vid,
'langcode' => $langcode,
]) : Url::fromRoute('entity.pet.revision_revert_confirm', [
'pet' => $pet
->id(),
'pet_revision' => $vid,
]),
];
}
if ($delete_permission) {
$links['delete'] = array(
'title' => $this
->t('Delete'),
'url' => Url::fromRoute('entity.pet.revision_delete_confirm', $route_params),
);
}
$row = [
'revision' => $this
->buildRevision($link, $username, $revision, $previous_revision),
'select_column_one' => $this
->buildSelectColumn('radios_left', $vid, isset($vids[1]) ? $vids[1] : FALSE),
'select_column_two' => $this
->buildSelectColumn('radios_right', $vid, FALSE),
'operations' => [
'#type' => 'operations',
'#links' => $links,
],
];
}
$build['node_revisions_table'][] = $row;
}
}
}
if ($revision_count > 1) {
$build['submit'] = array(
'#type' => 'submit',
'#button_type' => 'primary',
'#value' => t('Compare selected revisions'),
'#attributes' => array(
'class' => array(
'diff-button',
),
),
);
}
$build['pager'] = array(
'#type' => 'pager',
);
$build['#attached']['library'][] = 'node/drupal.node.admin';
return $build;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$input = $form_state
->getUserInput();
$vid_left = $input['radios_left'];
$vid_right = $input['radios_right'];
$pid = $input['pid'];
if ($vid_left > $vid_right) {
$aux = $vid_left;
$vid_left = $vid_right;
$vid_right = $aux;
}
$redirect_url = Url::fromRoute('entity.pet.revisions_diff', array(
'pet' => $pid,
'left_revision' => $vid_left,
'right_revision' => $vid_right,
'filter' => $this->diffLayoutManager
->getDefaultLayout(),
));
$form_state
->setRedirectUrl($redirect_url);
}
}