class DiffController in Entity Share 8.3
Returns responses for Diff support routes.
Hierarchy
- class \Drupal\Core\Controller\ControllerBase implements ContainerInjectionInterface uses LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\entity_share_diff\Controller\DiffController uses StringTranslationTrait
Expanded class hierarchy of DiffController
File
- modules/
entity_share_diff/ src/ Controller/ DiffController.php, line 20
Namespace
Drupal\entity_share_diff\ControllerView source
class DiffController extends ControllerBase {
use StringTranslationTrait;
/**
* The remote manager service.
*
* @var \Drupal\entity_share_client\Service\RemoteManagerInterface
*/
private $remoteManager;
/**
* The diff field builder plugin manager.
*
* @var \Drupal\Core\Diff\DiffFormatter
*/
public $diffFormatter;
/**
* The entity parser service.
*
* @var \Drupal\entity_share_diff\Service\entityParser
*/
public $entityParser;
/**
* The date formatter service.
*
* @var \Drupal\Core\Datetime\DateFormatterInterface
*/
private $dateFormatter;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
$instance = parent::create($container);
$instance->remoteManager = $container
->get('entity_share_client.remote_manager');
$instance->resourceTypeRepository = $container
->get('jsonapi.resource_type.repository');
$instance->diffFormatter = $container
->get('diff.formatter');
$instance->entityParser = $container
->get('entity_share_diff.entity_parser');
$instance->dateFormatter = $container
->get('date.formatter');
return $instance;
}
/**
* Returns a table showing the differences between local and remote entities.
*
* @param int $left_revision_id
* 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(int $left_revision_id, RemoteInterface $remote, string $channel_id, string $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_id);
$this->entityParser
->validateNeedToProcess($left_revision
->uuid(), FALSE);
$local_values = $this->entityParser
->prepareLocalEntity($left_revision);
$left_yaml = explode("\n", Yaml::encode($local_values));
// Get the right/remote revision.
$url = $channels_infos[$channel_id]['url'];
$prepared_url = EntityShareUtility::prepareUuidsFilteredUrl($url, [
$uuid,
]);
$response = $this->remoteManager
->jsonApiRequest($remote, 'GET', $prepared_url);
$json = Json::decode((string) $response
->getBody());
// There will be only one result.
$entity_data = current(EntityShareUtility::prepareData($json['data']));
$this->entityParser
->validateNeedToProcess($entity_data['id'], TRUE);
$remote_values = $this->entityParser
->prepareRemoteEntity($entity_data, $remote);
$right_yaml = explode("\n", Yaml::encode($remote_values));
$header = $this
->prepareHeaderlabels($left_revision, $entity_data);
return $this
->diffGenerator($left_yaml, $right_yaml, $header);
}
/**
* Helper: prepare left and right header labels.
*
* @param \Drupal\Core\Entity\ContentEntityInterface $left_entity
* The Drupal entity (local).
* @param array $remote_entity_data
* Used for remote entity: entity data coming from JSON:API.
*
* @return array
* Array with left and right header labels.
*/
protected function prepareHeaderlabels(ContentEntityInterface $left_entity, array $remote_entity_data) {
$header = [];
// Changes diff table header.
if (method_exists($left_entity, 'getChangedTime')) {
$left_changed = $this->dateFormatter
->format($left_entity
->getChangedTime(), 'short');
$header['left_label'] = $this
->t('Local entity: @changed', [
'@changed' => $left_changed,
]);
}
else {
$header['left_label'] = $this
->t('Local entity');
}
// Changes diff table header.
$right_changed = $this->entityParser
->getRemoteChangedTime($remote_entity_data);
if ($right_changed) {
$right_changed = $this->dateFormatter
->format($right_changed, 'short');
$header['right_label'] = $this
->t('Remote entity: @changed', [
'@changed' => $right_changed,
]);
}
else {
$header['right_label'] = $this
->t('Remote entity');
}
return $header;
}
/**
* Helper.
*
* @param string[] $left_entity
* Array of lines of YAML file representing the local entity.
* @param string[] $right_entity
* Array of lines of YAML file representing the remote entity.
* @param array $header
* Header labels.
*
* @return array
* A table render array.
*/
protected function diffGenerator(array $left_entity, array $right_entity, array $header = []) {
$element = [];
$diff = new Diff($left_entity, $right_entity);
$this->diffFormatter->show_header = FALSE;
$this->diffFormatter->htmlOutput = TRUE;
$output = $this->diffFormatter
->format($diff);
// Add the CSS for the inline diff.
$element['#attached']['library'][] = 'system/diff';
$element['diff'] = [
'#type' => 'table',
'#attributes' => [
'class' => [
'diff',
],
],
'#header' => [
[
'data' => $header['left_label'],
'colspan' => '2',
],
[
'data' => $header['right_label'],
'colspan' => '2',
],
],
'#rows' => $output,
];
return $element;
}
}
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:: |
public | property | The diff field builder plugin manager. | |
DiffController:: |
public | property | The entity parser service. | |
DiffController:: |
private | property | The remote manager service. | |
DiffController:: |
public | function | Returns a table showing the differences between local and remote entities. | |
DiffController:: |
public static | function |
Instantiates a new instance of this class. Overrides ControllerBase:: |
|
DiffController:: |
protected | function | Helper. | |
DiffController:: |
protected | function | Helper: prepare left and right header labels. | |
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. | |
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. |