diff --git a/diff.module b/diff.module index 3739d2e..af446fb 100644 --- a/diff.module +++ b/diff.module @@ -4,10 +4,8 @@ * @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(). @@ -125,30 +123,3 @@ 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 024381a..c1300fd 100644 --- a/src/Controller/GenericRevisionController.php +++ b/src/Controller/GenericRevisionController.php @@ -258,7 +258,7 @@ class GenericRevisionController extends EntityComparisonBase { // Second column. $row[] = array( 'data' => $this->l( - $this->t('< Previous difference'), diff_route($entity, $vids[$i - 1], $left_vid) + $this->t('< Previous difference'), $this->diff_route($entity, $vids[$i - 1], $left_vid) ), 'colspan' => 2, 'class' => 'rev-navigation', @@ -279,7 +279,7 @@ class GenericRevisionController extends EntityComparisonBase { // Forth column. $row[] = array( 'data' => $this->l( - $this->t('Next difference >'), diff_route($entity, $right_vid, $vids[$i]) + $this->t('Next difference >'), $this->diff_route($entity, $right_vid, $vids[$i]) ), 'colspan' => 2, 'class' => 'rev-navigation', @@ -303,25 +303,13 @@ class GenericRevisionController extends EntityComparisonBase { * Builds a table row with navigation between raw and raw-plain formats. */ protected function buildMarkdownNavigation(EntityInterface $entity, $left_vid, $right_vid, $active_filter) { - $entity_type_id = $entity->getEntityTypeId(); - - $url = $entity_type_id != 'node' ? "entity.$entity_type_id.revisions_diff": 'diff.revisions_diff'; $links['raw'] = array( 'title' => $this->t('Standard'), - 'url' => Url::fromRoute($url, array( - $entity_type_id => $entity->id(), - 'left_revision' => $left_vid, - 'right_revision' => $right_vid, - )), + 'url' => $this->diff_route($entity, $left_vid, $right_vid), ); $links['raw_plain'] = array( 'title' => $this->t('Markdown'), - 'url' => Url::fromRoute($url, array( - $entity_type_id => $entity->id(), - 'left_revision' => $left_vid, - 'right_revision' => $right_vid, - 'filter' => 'raw-plain', - )), + 'url' => $this->diff_route($entity, $left_vid, $right_vid, TRUE), ); // Set as the first element the current filter. @@ -339,4 +327,39 @@ class GenericRevisionController extends EntityComparisonBase { return $row; } + + /** + * Creates an 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. + * @param boolean $raw_plain + * Flag to determinate if the route should add a raw_plain filter. + * + * @return \Drupal\Core\Url + * The URL object. + */ + protected function diff_route(EntityInterface $entity, $left_vid, $right_vid, $raw_plain = FALSE) { + $entity_type_id = $entity->getEntityTypeId(); + // @todo Remove the diff.revisions_diff route so we avoid adding extra cases. + if ($entity->getEntityTypeId() == 'node') { + $route_name = 'diff.revisions_diff'; + } + else { + $route_name = "entity.$entity_type_id.revisions_diff"; + } + $route_parameters = [ + $entity_type_id => $entity->id(), + 'left_revision' => $left_vid, + 'right_revision' => $right_vid, + ]; + if ($raw_plain) { + $route_parameters['filter'] = 'raw-plain'; + } + return Url::fromRoute($route_name, $route_parameters); + } }