public function EasyEmailController::revisionOverview in Easy Email 8
Same name and namespace in other branches
- 2.0.x src/Controller/EasyEmailController.php \Drupal\easy_email\Controller\EasyEmailController::revisionOverview()
Generates an overview table of older revisions of a Email .
Parameters
\Drupal\easy_email\Entity\EasyEmailInterface $easy_email: A Email object.
Return value
array An array as expected by drupal_render().
File
- src/
Controller/ EasyEmailController.php, line 179
Class
- EasyEmailController
- Class EasyEmailController.
Namespace
Drupal\easy_email\ControllerCode
public function revisionOverview(EasyEmailInterface $easy_email) {
$account = $this
->currentUser();
$langcode = $easy_email
->language()
->getId();
$langname = $easy_email
->language()
->getName();
$languages = $easy_email
->getTranslationLanguages();
$has_translations = count($languages) > 1;
$easy_email_storage = $this
->entityTypeManager()
->getStorage('easy_email');
$build['#title'] = $has_translations ? $this
->t('@langname revisions for %title', [
'@langname' => $langname,
'%title' => $easy_email
->label(),
]) : $this
->t('Revisions for %title', [
'%title' => $easy_email
->label(),
]);
$header = [
$this
->t('Revision'),
$this
->t('Operations'),
];
$revert_permission = $account
->hasPermission("revert all email revisions") || $account
->hasPermission('administer email entities');
$delete_permission = $account
->hasPermission("delete all email revisions") || $account
->hasPermission('administer email entities');
$rows = [];
$vids = $easy_email_storage
->revisionIds($easy_email);
$latest_revision = TRUE;
foreach (array_reverse($vids) as $vid) {
/** @var \Drupal\easy_email\EasyEmailInterface $revision */
$revision = $easy_email_storage
->loadRevision($vid);
// Only show revisions that are affected by the language that is being
// displayed.
if ($revision
->hasTranslation($langcode) && $revision
->getTranslation($langcode)
->isRevisionTranslationAffected()) {
$username = [
'#theme' => 'username',
'#account' => $revision
->getRevisionUser(),
];
// Use revision link to link to revisions that are not active.
$date = \Drupal::service('date.formatter')
->format($revision
->getRevisionCreationTime(), 'short');
if ($vid != $easy_email
->getRevisionId()) {
$link = Link::fromTextAndUrl($date, Url::fromRoute('entity.easy_email.revision', [
'easy_email' => $easy_email
->id(),
'easy_email_revision' => $vid,
]));
}
else {
$link = $easy_email
->tolink($date)
->toString();
}
$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' => \Drupal::service('renderer')
->renderPlain($username),
'message' => [
'#markup' => $revision
->getRevisionLogMessage(),
'#allowed_tags' => Xss::getHtmlTagList(),
],
],
],
];
$row[] = $column;
if ($latest_revision) {
$row[] = [
'data' => [
'#prefix' => '<em>',
'#markup' => $this
->t('Current revision'),
'#suffix' => '</em>',
],
];
foreach ($row as &$current) {
$current['class'] = [
'revision-current',
];
}
$latest_revision = FALSE;
}
else {
$links = [];
if ($revert_permission) {
$links['revert'] = [
'title' => $this
->t('Revert'),
'url' => $has_translations ? Url::fromRoute('entity.easy_email.translation_revert', [
'easy_email' => $easy_email
->id(),
'easy_email_revision' => $vid,
'langcode' => $langcode,
]) : Url::fromRoute('entity.easy_email.revision_revert', [
'easy_email' => $easy_email
->id(),
'easy_email_revision' => $vid,
]),
];
}
if ($delete_permission) {
$links['delete'] = [
'title' => $this
->t('Delete'),
'url' => Url::fromRoute('entity.easy_email.revision_delete', [
'easy_email' => $easy_email
->id(),
'easy_email_revision' => $vid,
]),
];
}
$row[] = [
'data' => [
'#type' => 'operations',
'#links' => $links,
],
];
}
$rows[] = $row;
}
}
$build['easy_email_revisions_table'] = [
'#theme' => 'table',
'#rows' => $rows,
'#header' => $header,
];
return $build;
}