You are here

public function PluginRevisionController::compareEntityRevisions in Diff 8

Returns a table which shows the differences between two entity revisions.

Parameters

\Drupal\Core\Routing\RouteMatchInterface $route_match: The route match.

\Drupal\Core\Entity\ContentEntityInterface $left_revision: The left revision.

\Drupal\Core\Entity\ContentEntityInterface $right_revision: The right revision.

string $filter: If $filter == 'raw' raw text is compared (including html tags) If filter == 'raw-plain' markdown function is applied to the text before comparison.

Return value

array Table showing the diff between the two entity revisions.

1 call to PluginRevisionController::compareEntityRevisions()
NodeRevisionController::compareNodeRevisions in src/Controller/NodeRevisionController.php
Returns a table which shows the differences between two node revisions.

File

src/Controller/PluginRevisionController.php, line 116

Class

PluginRevisionController
Base class for controllers that return responses on entity revision routes.

Namespace

Drupal\diff\Controller

Code

public function compareEntityRevisions(RouteMatchInterface $route_match, ContentEntityInterface $left_revision, ContentEntityInterface $right_revision, $filter) {
  $entity_type_id = $left_revision
    ->getEntityTypeId();

  /** @var \Drupal\Core\Entity\EntityInterface $entity */
  $entity = $route_match
    ->getParameter($entity_type_id);
  $entity_type_id = $entity
    ->getEntityTypeId();
  $storage = $this
    ->entityTypeManager()
    ->getStorage($entity_type_id);

  // Get language from the entity context.
  $langcode = $entity
    ->language()
    ->getId();

  // Get left and right revision in current language.
  $left_revision = $left_revision
    ->getTranslation($langcode);
  $right_revision = $right_revision
    ->getTranslation($langcode);
  $revisions_ids = [];

  // Filter revisions of current translation and where the translation is
  // affected.
  foreach ($this
    ->getRevisionIds($storage, $entity
    ->id()) as $revision_id) {

    /** @var \Drupal\Core\Entity\ContentEntityInterface $revision */
    $revision = $storage
      ->loadRevision($revision_id);
    if ($revision
      ->hasTranslation($langcode) && $revision
      ->getTranslation($langcode)
      ->isRevisionTranslationAffected()) {
      $revisions_ids[] = $revision_id;
    }
  }
  $build = [
    '#title' => $this
      ->t('Changes to %title', [
      '%title' => $entity
        ->label(),
    ]),
    'header' => [
      '#prefix' => '<header class="diff-header">',
      '#suffix' => '</header>',
    ],
    'controls' => [
      '#prefix' => '<div class="diff-controls">',
      '#suffix' => '</div>',
    ],
  ];

  // Build the navigation links.
  $build['header']['diff_navigation'] = $this
    ->buildRevisionsNavigation($entity, $revisions_ids, $left_revision
    ->getRevisionId(), $right_revision
    ->getRevisionId(), $filter);

  // Build the layout filter.
  $build['controls']['diff_layout'] = [
    '#type' => 'item',
    '#title' => $this
      ->t('Layout'),
    '#wrapper_attributes' => [
      'class' => 'diff-controls__item',
    ],
    'filter' => $this
      ->buildLayoutNavigation($entity, $left_revision
      ->getRevisionId(), $right_revision
      ->getRevisionId(), $filter),
  ];

  // Perform comparison only if both entity revisions loaded successfully.
  if ($left_revision != FALSE && $right_revision != FALSE) {

    // Build the diff comparison with the plugin.
    if ($plugin = $this->diffLayoutManager
      ->createInstance($filter)) {
      $build = array_merge_recursive($build, $plugin
        ->build($left_revision, $right_revision, $entity));
      $build['diff']['#prefix'] = '<div class="diff-responsive-table-wrapper">';
      $build['diff']['#suffix'] = '</div>';
      $build['diff']['#attributes']['class'][] = 'diff-responsive-table';
    }
  }
  $build['#attached']['library'][] = 'diff/diff.general';
  return $build;
}