class StateInformation in Entity Share 8.2
Same name and namespace in other branches
- 8.3 modules/entity_share_client/src/Service/StateInformation.php \Drupal\entity_share_client\Service\StateInformation
Class StateInformation.
@package Drupal\entity_share_client\Service
Hierarchy
- class \Drupal\entity_share_client\Service\StateInformation implements StateInformationInterface uses StringTranslationTrait
Expanded class hierarchy of StateInformation
1 string reference to 'StateInformation'
- entity_share_client.services.yml in modules/
entity_share_client/ entity_share_client.services.yml - modules/entity_share_client/entity_share_client.services.yml
1 service uses StateInformation
- entity_share_client.state_information in modules/
entity_share_client/ entity_share_client.services.yml - Drupal\entity_share_client\Service\StateInformation
File
- modules/
entity_share_client/ src/ Service/ StateInformation.php, line 16
Namespace
Drupal\entity_share_client\ServiceView source
class StateInformation implements StateInformationInterface {
use StringTranslationTrait;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The resource type repository.
*
* @var \Drupal\jsonapi\ResourceType\ResourceTypeRepositoryInterface
*/
protected $resourceTypeRepository;
/**
* StateInformation constructor.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\jsonapi\ResourceType\ResourceTypeRepositoryInterface $resource_type_repository
* The resource type repository.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, ResourceTypeRepositoryInterface $resource_type_repository) {
$this->entityTypeManager = $entity_type_manager;
$this->resourceTypeRepository = $resource_type_repository;
}
/**
* {@inheritdoc}
*/
public function getStatusInfo(array $data) {
$status_info = [
'label' => $this
->t('Undefined'),
'class' => 'entity-share-undefined',
'info_id' => StateInformationInterface::INFO_ID_UNDEFINED,
'local_entity_link' => NULL,
'local_revision_id' => NULL,
];
// Get the entity type and entity storage.
$parsed_type = explode('--', $data['type']);
$entity_type_id = $parsed_type[0];
try {
$entity_storage = $this->entityTypeManager
->getStorage($entity_type_id);
} catch (\Exception $exception) {
$status_info = [
'label' => $this
->t('Unknown entity type'),
'class' => 'entity-share-undefined',
'info_id' => StateInformationInterface::INFO_ID_UNKNOWN,
'local_entity_link' => NULL,
'local_revision_id' => NULL,
];
return $status_info;
}
// Check if an entity already exists.
$existing_entities = $entity_storage
->loadByProperties([
'uuid' => $data['id'],
]);
if (empty($existing_entities)) {
$status_info = [
'label' => $this
->t('New entity'),
'class' => 'entity-share-new',
'info_id' => StateInformationInterface::INFO_ID_NEW,
'local_entity_link' => NULL,
'local_revision_id' => NULL,
];
}
else {
/** @var \Drupal\Core\Entity\ContentEntityInterface $existing_entity */
$existing_entity = array_shift($existing_entities);
$resource_type = $this->resourceTypeRepository
->get($parsed_type[0], $parsed_type[1]);
$changed_public_name = FALSE;
if ($resource_type
->hasField('changed')) {
$changed_public_name = $resource_type
->getPublicName('changed');
}
if (!empty($data['attributes'][$changed_public_name]) && method_exists($existing_entity, 'getChangedTime')) {
$entity_changed_time = 0;
// If the website is using backward compatible timestamps output.
// @see https://www.drupal.org/node/2859657.
// The value is casted in integer for
// https://www.drupal.org/node/2837696.
if (is_numeric($data['attributes'][$changed_public_name])) {
$entity_changed_time = (int) $data['attributes'][$changed_public_name];
}
elseif ($changed_datetime = \DateTime::createFromFormat(\DateTime::RFC3339, $data['attributes'][$changed_public_name])) {
$entity_changed_time = $changed_datetime
->getTimestamp();
}
$entity_keys = $entity_storage
->getEntityType()
->getKeys();
// Case of translatable entity.
if (isset($entity_keys['langcode']) && !empty($entity_keys['langcode'])) {
$entity_language_id = $data['attributes'][$resource_type
->getPublicName($entity_keys['langcode'])];
// Entity has the translation.
if ($existing_entity
->hasTranslation($entity_language_id)) {
$existing_translation = $existing_entity
->getTranslation($entity_language_id);
$existing_entity_changed_time = $existing_translation
->getChangedTime();
// Existing entity.
if ($entity_changed_time != $existing_entity_changed_time) {
$status_info = [
'label' => $this
->t('Entities not synchronized'),
'class' => 'entity-share-changed',
'info_id' => StateInformationInterface::INFO_ID_CHANGED,
'local_entity_link' => $existing_entity
->toUrl(),
'local_revision_id' => $existing_entity
->getRevisionId(),
];
}
else {
$status_info = [
'label' => $this
->t('Entities synchronized'),
'class' => 'entity-share-up-to-date',
'info_id' => StateInformationInterface::INFO_ID_SYNCHRONIZED,
'local_entity_link' => $existing_entity
->toUrl(),
'local_revision_id' => $existing_entity
->getRevisionId(),
];
}
}
else {
$status_info = [
'label' => $this
->t('New translation'),
'class' => 'entity-share-new',
'info_id' => StateInformationInterface::INFO_ID_NEW_TRANSLATION,
'local_entity_link' => $existing_entity
->toUrl(),
'local_revision_id' => $existing_entity
->getRevisionId(),
];
}
}
else {
$existing_entity_changed_time = $existing_entity
->getChangedTime();
// Existing entity.
if ($entity_changed_time != $existing_entity_changed_time) {
$status_info = [
'label' => $this
->t('Entities not synchronized'),
'class' => 'entity-share-changed',
'info_id' => StateInformationInterface::INFO_ID_CHANGED,
'local_entity_link' => $existing_entity
->toUrl(),
'local_revision_id' => $existing_entity
->getRevisionId(),
];
}
else {
$status_info = [
'label' => $this
->t('Entities synchronized'),
'class' => 'entity-share-up-to-date',
'info_id' => StateInformationInterface::INFO_ID_SYNCHRONIZED,
'local_entity_link' => $existing_entity
->toUrl(),
'local_revision_id' => $existing_entity
->getRevisionId(),
];
}
}
}
}
return $status_info;
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
StateInformation:: |
protected | property | The entity type manager. | |
StateInformation:: |
protected | property | The resource type repository. | |
StateInformation:: |
public | function |
Check if an entity already exists or not and get status info. Overrides StateInformationInterface:: |
|
StateInformation:: |
public | function | StateInformation constructor. | |
StateInformationInterface:: |
constant | The info id in the case of a changed entity or translation. | ||
StateInformationInterface:: |
constant | The info id in the case of a new entity. | ||
StateInformationInterface:: |
constant | The info id in the case of a new entity translation. | ||
StateInformationInterface:: |
constant | The info id in the case of a synchronized entity or translation. | ||
StateInformationInterface:: |
constant | The info id in the case of an undefined state. | ||
StateInformationInterface:: |
constant | The info id in the case of an unknown entity type. | ||
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. |