You are here

public function VisualInlineDiffLayout::build in Diff 8

Builds a diff comparison between two revisions.

This method is responsible for building the diff comparison between revisions of the same entity. It can build a table, navigation links and headers of a diff comparison.

Parameters

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

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

\Drupal\Core\Entity\ContentEntityInterface $entity: The entity.

Return value

array The modified build array that the plugin builds.

Overrides DiffLayoutInterface::build

File

src/Plugin/diff/Layout/VisualInlineDiffLayout.php, line 131

Class

VisualInlineDiffLayout
Provides Visual Inline diff layout.

Namespace

Drupal\diff\Plugin\diff\Layout

Code

public function build(ContentEntityInterface $left_revision, ContentEntityInterface $right_revision, ContentEntityInterface $entity) {

  // Build the revisions data.
  $build = $this
    ->buildRevisionsData($left_revision, $right_revision);
  $this->entityTypeManager
    ->getStorage($entity
    ->getEntityTypeId())
    ->resetCache([
    $entity
      ->id(),
  ]);

  // Build the view modes filter.
  $options = [];

  // Get all view modes for entity type.
  $view_modes = $this->entityDisplayRepository
    ->getViewModeOptionsByBundle($entity
    ->getEntityTypeId(), $entity
    ->bundle());
  foreach ($view_modes as $view_mode => $view_mode_info) {

    // Skip view modes that are not used in the front end.
    if (in_array($view_mode, [
      'rss',
      'search_index',
    ])) {
      continue;
    }
    $options[$view_mode] = [
      'title' => $view_mode_info,
      'url' => PluginRevisionController::diffRoute($entity, $left_revision
        ->getRevisionId(), $right_revision
        ->getRevisionId(), 'visual_inline', [
        'view_mode' => $view_mode,
      ]),
    ];
  }
  $active_option = array_keys($options);
  $active_view_mode = $this->requestStack
    ->getCurrentRequest()->query
    ->get('view_mode') ?: reset($active_option);
  $filter = $options[$active_view_mode];
  unset($options[$active_view_mode]);
  array_unshift($options, $filter);
  $build['controls']['view_mode'] = [
    '#type' => 'item',
    '#title' => $this
      ->t('View mode'),
    '#wrapper_attributes' => [
      'class' => 'diff-controls__item',
    ],
    'filter' => [
      '#type' => 'operations',
      '#links' => $options,
    ],
  ];
  $view_builder = $this->entityTypeManager
    ->getViewBuilder($entity
    ->getEntityTypeId());

  // Trigger exclusion of interactive items like on preview.
  $left_revision->in_preview = TRUE;
  $right_revision->in_preview = TRUE;
  $left_view = $view_builder
    ->view($left_revision, $active_view_mode);
  $right_view = $view_builder
    ->view($right_revision, $active_view_mode);

  // Avoid render cache from being built.
  unset($left_view['#cache']);
  unset($right_view['#cache']);
  $html_1 = $this->renderer
    ->render($left_view);
  $html_2 = $this->renderer
    ->render($right_view);
  $this->htmlDiff
    ->setOldHtml($html_1);
  $this->htmlDiff
    ->setNewHtml($html_2);
  $this->htmlDiff
    ->build();
  $build['diff'] = [
    '#markup' => $this->htmlDiff
      ->getDifference(),
    '#weight' => 10,
  ];
  $build['#attached']['library'][] = 'diff/diff.visual_inline';
  return $build;
}