You are here

public function DiffController::compareEntities in Entity Share 8.2

Returns a table showing the differences between local and remote entities.

Parameters

int $left_revision: The revision id of the local entity.

\Drupal\entity_share_client\Entity\RemoteInterface $remote: The remote from which the entity is from.

string $channel_id: The channel ID from which the entity is from. Used to handle language.

string $uuid: The UUID of the entity.

Return value

array Table showing the diff between the local and remote entities.

1 string reference to 'DiffController::compareEntities'
entity_share_client.routing.yml in modules/entity_share_client/entity_share_client.routing.yml
modules/entity_share_client/entity_share_client.routing.yml

File

modules/entity_share_client/src/Controller/DiffController.php, line 92

Class

DiffController
Returns responses for Diff support routes.

Namespace

Drupal\entity_share_client\Controller

Code

public function compareEntities($left_revision, RemoteInterface $remote, $channel_id, $uuid) {

  // Reload the remote to have config overrides applied.
  $remote = $this
    ->entityTypeManager()
    ->getStorage('remote')
    ->load($remote
    ->id());
  $channels_infos = $this->remoteManager
    ->getChannelsInfos($remote);

  // Get the left/local revision.
  $entity_type_id = $channels_infos[$channel_id]['channel_entity_type'];
  $storage = $this
    ->entityTypeManager()
    ->getStorage($entity_type_id);
  $left_revision = $storage
    ->loadRevision($left_revision);

  // Get the right/remote revision.
  $url = $channels_infos[$channel_id]['url'];
  $parsed_url = UrlHelper::parse($url);
  $query = $parsed_url['query'];
  $query['filter']['uuid-filter'] = [
    'condition' => [
      'path' => 'id',
      'operator' => 'IN',
      'value' => array_values([
        $uuid,
      ]),
    ],
  ];
  $query = UrlHelper::buildQuery($query);
  $prepared_url = $parsed_url['path'] . '?' . $query;
  $http_client = $this->remoteManager
    ->prepareJsonApiClient($remote);
  $response = $this->requestService
    ->request($http_client, 'GET', $prepared_url);
  $json = Json::decode((string) $response
    ->getBody());
  $entity_type = $storage
    ->getEntityType();
  $entity_keys = $entity_type
    ->getKeys();
  $resource_type = $this->resourceTypeRepository
    ->get($entity_type_id, $left_revision
    ->bundle());
  $id_public_name = $resource_type
    ->getPublicName($entity_keys['id']);

  // There will be only one result.
  foreach (EntityShareUtility::prepareData($json['data']) as $entity_data) {

    // Force the remote entity id to be the same as the local entity otherwise
    // the diff is not helpful.
    $entity_data['attributes'][$id_public_name] = $left_revision
      ->id();
    $right_revision = $this->jsonapiHelper
      ->extractEntity($entity_data);
  }
  $build = $this
    ->compareEntityRevisions($this->routeMatch, $left_revision, $right_revision, 'split_fields');
  return $build;
}