diff --git a/diff.module b/diff.module index af446fb..3739d2e 100644 --- a/diff.module +++ b/diff.module @@ -4,8 +4,10 @@ * @file Contains hooks. */ +use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Url; /** * Implements hook_form_BASE_FORM_ID_alter(). @@ -123,3 +125,30 @@ function _diff_field_label($entity_type, $field_name) { arsort($labels); return array_keys($labels); } + +/** + * Returns the url object for diff. + * + * @param \Drupal\Core\Entity\EntityInterface $entity + * The entity to be compared. + * @param $left_vid + * Vid of the left revision. + * @param $right_vid + * Vid of the right revision. + * + * @return \Drupal\Core\Url + */ +function diff_route(EntityInterface $entity, $left_vid, $right_vid) { + $entity_type_id = $entity->getEntityTypeId(); + if ($entity->getEntityTypeId() == 'node') { + $route_name = 'diff.revisions_diff'; + } + else { + $route_name = "entity.$entity_type_id.revisions_diff"; + } + return Url::fromRoute($route_name, [ + $entity_type_id => $entity->id(), + 'left_revision' => $left_vid, + 'right_revision' => $right_vid, + ]); +} \ No newline at end of file diff --git a/src/Controller/GenericRevisionController.php b/src/Controller/GenericRevisionController.php index 7a1fd0c..024381a 100644 --- a/src/Controller/GenericRevisionController.php +++ b/src/Controller/GenericRevisionController.php @@ -210,9 +210,9 @@ class GenericRevisionController extends EntityComparisonBase { '#account' => $revision->uid->entity, ); $revision_date = $this->date->format($revision->getRevisionCreationTime(), 'short'); - $url = $entity_type_id != 'node' ? "entity.$entity_type_id.revisions_diff": 'entity.node.revision'; + $route_name = $entity_type_id != 'node' ? "entity.$entity_type_id.revisions_diff": 'entity.node.revision'; $revision_link = $this->t($revision_log . '@date', array( - '@date' => $this->l($revision_date, Url::fromRoute($url, array( + '@date' => $this->l($revision_date, Url::fromRoute($route_name, array( $entity_type_id => $revision->id(), $entity_type_id . '_revision' => $revision->getRevisionId(), ))), @@ -246,8 +246,6 @@ class GenericRevisionController extends EntityComparisonBase { * Returns the navigation row for diff table. */ protected function buildRevisionsNavigation(EntityInterface $entity, $vids, $left_vid, $right_vid) { - $entity_type_id = $entity->getEntityTypeId(); - $entity_id = $entity->id(); $revisions_count = count($vids); $i = 0; @@ -257,17 +255,10 @@ class GenericRevisionController extends EntityComparisonBase { $i += 1; } if ($i != 0) { - $url = $entity_type_id != 'node' ? "entity.$entity_type_id.revisions_diff": 'diff.revisions_diff'; // Second column. $row[] = array( 'data' => $this->l( - $this->t('< Previous difference'), - Url::fromRoute($url, - array( - $entity_type_id => $entity_id, - 'left_revision' => $vids[$i - 1], - 'right_revision' => $left_vid, - )) + $this->t('< Previous difference'), diff_route($entity, $vids[$i - 1], $left_vid) ), 'colspan' => 2, 'class' => 'rev-navigation', @@ -285,17 +276,10 @@ class GenericRevisionController extends EntityComparisonBase { $i += 1; } if ($revisions_count != $i && $vids[$i - 1] != $vids[$revisions_count - 1]) { - $url = $entity_type_id != 'node' ? "entity.$entity_type_id.revisions_diff": 'diff.revisions_diff'; // Forth column. $row[] = array( 'data' => $this->l( - $this->t('Next difference >'), - Url::fromRoute($url, - array( - $entity_type_id => $entity_id, - 'left_revision' => $right_vid, - 'right_revision' => $vids[$i], - )) + $this->t('Next difference >'), diff_route($entity, $right_vid, $vids[$i]) ), 'colspan' => 2, 'class' => 'rev-navigation',