class DiffController in Entity Share 8.2
Returns responses for Diff support routes.
Hierarchy
- class \Drupal\Core\Controller\ControllerBase implements ContainerInjectionInterface uses LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\diff\Controller\PluginRevisionController
- class \Drupal\entity_share_client\Controller\DiffController
- class \Drupal\diff\Controller\PluginRevisionController
Expanded class hierarchy of DiffController
File
- modules/
entity_share_client/ src/ Controller/ DiffController.php, line 19
Namespace
Drupal\entity_share_client\ControllerView source
class DiffController extends PluginRevisionController {
/**
* The remote manager service.
*
* @var \Drupal\entity_share_client\Service\RemoteManagerInterface
*/
private $remoteManager;
/**
* The request service.
*
* @var \Drupal\entity_share_client\Service\RequestServiceInterface
*/
private $requestService;
/**
* The jsonapi helper.
*
* @var \Drupal\entity_share_client\Service\JsonapiHelperInterface
*/
private $jsonapiHelper;
/**
* The route match service.
*
* @var \Drupal\Core\Routing\ResettableStackedRouteMatchInterface
*/
private $routeMatch;
/**
* The date formatter service.
*
* @var \Drupal\Core\Datetime\DateFormatterInterface
*/
private $dateFormatter;
/**
* The resource type repository.
*
* @var \Drupal\jsonapi\ResourceType\ResourceTypeRepositoryInterface
*/
protected $resourceTypeRepository;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
$instance = parent::create($container);
$instance->remoteManager = $container
->get('entity_share_client.remote_manager');
$instance->requestService = $container
->get('entity_share_client.request');
$instance->jsonapiHelper = $container
->get('entity_share_client.jsonapi_helper');
$instance->routeMatch = $container
->get('current_route_match');
$instance->dateFormatter = $container
->get('date.formatter');
$instance->resourceTypeRepository = $container
->get('jsonapi.resource_type.repository');
return $instance;
}
/**
* Returns a table showing the differences between local and remote entities.
*
* @param int $left_revision
* The revision id of the local entity.
* @param \Drupal\entity_share_client\Entity\RemoteInterface $remote
* The remote from which the entity is from.
* @param string $channel_id
* The channel ID from which the entity is from. Used to handle language.
* @param string $uuid
* The UUID of the entity.
*
* @return array
* Table showing the diff between the local and remote entities.
*/
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;
}
/**
* {@inheritdoc}
*/
public function compareEntityRevisions(RouteMatchInterface $route_match, ContentEntityInterface $left_revision, ContentEntityInterface $right_revision, $filter) {
$entity = $left_revision;
// 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);
$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>',
],
];
// 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));
unset($build['header']);
unset($build['controls']);
// Changes diff table header.
$left_changed = '';
if (method_exists($left_revision, 'getChangedTime')) {
$left_changed = $this->dateFormatter
->format($left_revision
->getChangedTime(), 'short');
}
$build['diff']['#header'][0]['data']['#markup'] = $this
->t('Local entity: @changed', [
'@changed' => $left_changed,
]);
$right_changed = '';
if (method_exists($right_revision, 'getChangedTime')) {
$right_changed = $this->dateFormatter
->format($right_revision
->getChangedTime(), 'short');
}
$build['diff']['#header'][1]['data']['#markup'] = $this
->t('Remote entity: @changed', [
'@changed' => $right_changed,
]);
$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;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ControllerBase:: |
protected | property | The configuration factory. | |
ControllerBase:: |
protected | property | The current user service. | 1 |
ControllerBase:: |
protected | property | The entity form builder. | |
ControllerBase:: |
protected | property | The entity manager. | |
ControllerBase:: |
protected | property | The entity type manager. | |
ControllerBase:: |
protected | property | The form builder. | 2 |
ControllerBase:: |
protected | property | The key-value storage. | 1 |
ControllerBase:: |
protected | property | The language manager. | 1 |
ControllerBase:: |
protected | property | The module handler. | 2 |
ControllerBase:: |
protected | property | The state service. | |
ControllerBase:: |
protected | function | Returns the requested cache bin. | |
ControllerBase:: |
protected | function | Retrieves a configuration object. | |
ControllerBase:: |
private | function | Returns the service container. | |
ControllerBase:: |
protected | function | Returns the current user. | 1 |
ControllerBase:: |
protected | function | Retrieves the entity form builder. | |
ControllerBase:: |
protected | function | Retrieves the entity manager service. | |
ControllerBase:: |
protected | function | Retrieves the entity type manager. | |
ControllerBase:: |
protected | function | Returns the form builder service. | 2 |
ControllerBase:: |
protected | function | Returns a key/value storage collection. | 1 |
ControllerBase:: |
protected | function | Returns the language manager service. | 1 |
ControllerBase:: |
protected | function | Returns the module handler. | 2 |
ControllerBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
ControllerBase:: |
protected | function | Returns the state storage service. | |
DiffController:: |
private | property | The date formatter service. | |
DiffController:: |
private | property | The jsonapi helper. | |
DiffController:: |
private | property | The remote manager service. | |
DiffController:: |
private | property | The request service. | |
DiffController:: |
protected | property | The resource type repository. | |
DiffController:: |
private | property | The route match service. | |
DiffController:: |
public | function | Returns a table showing the differences between local and remote entities. | |
DiffController:: |
public | function |
Returns a table which shows the differences between two entity revisions. Overrides PluginRevisionController:: |
|
DiffController:: |
public static | function |
Instantiates a new instance of this class. Overrides PluginRevisionController:: |
|
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginRevisionController:: |
protected | property | Wrapper object for writing/reading configuration from diff.plugins.yml. | |
PluginRevisionController:: |
protected | property | The field diff layout plugin manager service. | |
PluginRevisionController:: |
protected | property | The diff entity comparison service. | |
PluginRevisionController:: |
protected | property | The request stack. | |
PluginRevisionController:: |
protected | function | Builds a navigation dropdown button between the layout plugins. | |
PluginRevisionController:: |
protected | function | Creates navigation links between the previous changes and the new ones. | |
PluginRevisionController:: |
public static | function | Creates an url object for diff. | |
PluginRevisionController:: |
public | function | Get all the revision ids of given entity id. | |
PluginRevisionController:: |
public | function | Constructs a PluginRevisionController object. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |