diff --git a/node_authlink.module b/node_authlink.module index 4604bafed4a923ce1315ce48a40b1bf94679445e..00294ffc74511ffa7c1ee7573a78aa2737b91506 100644 --- a/node_authlink.module +++ b/node_authlink.module @@ -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], ]); diff --git a/src/Form/NodeAuthlinkNodeForm.php b/src/Form/NodeAuthlinkNodeForm.php index 35f7e5daba89b69ac401d571b437cd0867a06f35..587e0467a14bfde6662500d8dfdb7377f0e9d65f 100644 --- a/src/Form/NodeAuthlinkNodeForm.php +++ b/src/Form/NodeAuthlinkNodeForm.php @@ -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())) {