Commit e29e7a69 authored by akalam's avatar akalam

SP-832199932200423: Added User interface for viewing authlinks for revisions.

parent 69a055a0
......@@ -200,9 +200,12 @@ function node_authlink_load_authkey($nid) {
* Get edit URL of specified node.
*
* @param $node Node object or NID.
* @param $op Operation to do with node. view, edit (default) or delete.
* @param string $op Operation to do with node. view, edit (default) or delete.
* @param null $revision_id
*
* @return bool|\Drupal\Core\GeneratedUrl|string
*/
function node_authlink_get_url($node, $op = 'view') {
function node_authlink_get_url($node, $op = 'view', $revision_id = NULL) {
if (is_numeric($node)) {
$node = Node::load($node);
}
......@@ -213,7 +216,12 @@ function node_authlink_get_url($node, $op = 'view') {
switch ($op) {
case 'view':
$route_name = 'entity.node.canonical';
if (is_numeric($revision_id)) {
$route_name = 'entity.node.revision';
}
else {
$route_name = 'entity.node.canonical';
}
break;
case 'edit':
$route_name = 'entity.node.edit_form';
......@@ -225,8 +233,12 @@ function node_authlink_get_url($node, $op = 'view') {
return FALSE;
}
$arguments = ['node' => $node->id()];
if (is_numeric($revision_id)) {
$arguments['node_revision'] = $revision_id;
}
$url = Url::fromRoute($route_name, ['node' => $node->id()], [
$url = Url::fromRoute($route_name, $arguments, [
'absolute' => TRUE,
'query' => ['authkey' => $node->authkey],
]);
......
......@@ -69,15 +69,82 @@ class NodeAuthlinkNodeForm extends FormBase {
if (!$op) {
continue;
}
$url = node_authlink_get_url($node, $op);
if ($url) {
// @todo: use a table instead.
$form['link_'.$op] = [
'#type' => 'markup',
'#markup' => "<p><strong>$op</strong>: $url</p>",
];
// If $op is view, load all revisions.
$has_revisions = FALSE;
if ($op == 'view') {
$has_revisions = TRUE;
$node_storage = \Drupal::entityManager()->getStorage('node');
$result = $node_storage->getQuery()
->allRevisions()
->condition($node->getEntityType()->getKey('id'), $node->id())
->sort($node->getEntityType()->getKey('revision'), 'DESC')
->range(0, 50)
->execute();
if (!empty($result)) {
$revision_options = [];
foreach ($result as $vid => $nid) {
$revision = $node_storage->loadRevision($vid);
$langcode = $node->language()->getId();
// Only show revisions that are affected by the language that is being
// displayed.
if ($revision->hasTranslation($langcode) && $revision->getTranslation($langcode)->isRevisionTranslationAffected()) {
// Use revision link to link to revisions that are not active.
$dateFormatter = \Drupal::service('date.formatter');
$date = $dateFormatter->format($revision->revision_timestamp->value, 'short');
if ($revision->isDefaultRevision()) {
$revision_options[$vid] = [
'text' => $this->t('Current revision'),
'url' => node_authlink_get_url($node, $op),
];
}
else {
$revision_options[$vid] = [
'text' => $date,
'url' => node_authlink_get_url($node, $op, $vid),
];
}
}
}
}
}
if ($has_revisions) {
$form['revisions'] = [
'#type' => 'select',
'#title' => $this->t('Revisions'),
'#options' => [],
];
// @todo: use a table instead.
foreach ($revision_options as $vid => $revision_option) {
$form['revisions']['#options'][$vid] = $revision_option['text'];
$form['link_' . $op . '_' . $vid] = [
'#type' => 'item',
'#markup' => "<p><strong>" . $op . "</strong>: " . $revision_option['url'] . "</p>",
'#states' => [
'visible' => [
'[name="revisions"]' => ['value' => $vid],
],
],
];
}
}
else {
$url = node_authlink_get_url($node, $op);
if ($url) {
// @todo: use a table instead.
$form['link_'.$op] = [
'#type' => 'item',
'#markup' => "<p><strong>$op</strong>: $url</p>",
];
}
}
}
if (node_authlink_load_authkey($node->id())) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment