class UpdateUtils in Scheduled Updates 8
Service to determine information about Scheduled Update Types.
Hierarchy
- class \Drupal\scheduled_updates\UpdateUtils implements UpdateUtilsInterface uses ClassUtilsTrait
Expanded class hierarchy of UpdateUtils
1 file declares its use of UpdateUtils
- BaseUpdateRunner.php in src/
Plugin/ BaseUpdateRunner.php - Contains \Drupal\scheduled_updates\Plugin\BaseUpdateRunner.
1 string reference to 'UpdateUtils'
1 service uses UpdateUtils
File
- src/
UpdateUtils.php, line 14
Namespace
Drupal\scheduled_updatesView source
class UpdateUtils implements UpdateUtilsInterface {
use ClassUtilsTrait;
/**
* The entity field manager.
*
* @var \Drupal\Core\Entity\EntityFieldManagerInterface
*/
protected $entityFieldManager;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The bundle info service.
*
* @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
*/
protected $bundleInfo;
/**
* UpdateUtils constructor.
*
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $bundleInfo
*/
public function __construct(EntityFieldManagerInterface $entityFieldManager, EntityTypeManagerInterface $entityTypeManager, EntityTypeBundleInfoInterface $bundleInfo) {
$this->entityFieldManager = $entityFieldManager;
$this->entityTypeManager = $entityTypeManager;
$this->bundleInfo = $bundleInfo;
}
/**
* {@inheritdoc}
*/
public function supportsRevisionUpdates(ScheduledUpdateTypeInterface $scheduledUpdateType) {
$type_definition = $this
->getUpdateTypeDefinition($scheduledUpdateType);
if ($type_definition && $type_definition
->isRevisionable()) {
return TRUE;
}
return FALSE;
}
/**
* {@inheritdoc}
*/
public function supportsRevisionBundleDefault(ScheduledUpdateTypeInterface $scheduledUpdateType) {
if ($type_definition = $this
->getUpdateTypeDefinition($scheduledUpdateType)) {
$bundle_type_id = $type_definition
->getBundleEntityType();
$bundle_class = $this->entityTypeManager
->getDefinition($bundle_type_id)
->getClass();
// Core doesn't have a standard method for determining if entities of bundle should have new revisions.
// This should be updated if an interface is create for this.
// @todo Check Entity API has a solution for this and also check it's interface.
if ($this
->implementsInterface($bundle_class, [
'Drupal\\node\\NodeTypeInterface',
])) {
return TRUE;
}
}
return FALSE;
}
/**
* {@inheritdoc}
*/
public function isRevisionableUpdate(ScheduledUpdateInterface $update) {
return $this
->supportsRevisionUpdates($this
->getUpdateType($update));
}
/**
* {@inheritdoc}
*/
public function getUpdateType(ScheduledUpdateInterface $update) {
return $this->entityTypeManager
->getStorage('scheduled_update_type')
->load($update
->bundle());
}
/**
* {@inheritdoc}
*/
public function getUpdateTypeLabel(ScheduledUpdateInterface $update) {
return $this
->getUpdateType($update)
->label();
}
/**
* {@inheritdoc}
*/
public function getRevisionDefault(ContentEntityInterface $entity_to_update) {
$bundle_type_id = $entity_to_update
->getEntityType()
->getBundleEntityType();
$bundle = $this->entityTypeManager
->getStorage($bundle_type_id)
->load($entity_to_update
->bundle());
// This should exist because of previous check in
// supportsRevisionBundleDefault but just in case.
if ($bundle instanceof NodeTypeInterface) {
return $bundle
->isNewRevision();
}
return FALSE;
}
/**
* {@inheritdoc}
*/
public function getLatestRevision($entity_type_id, $entity_id) {
if ($latest_revision_id = $this
->getLatestRevisionId($entity_type_id, $entity_id)) {
return $this->entityTypeManager
->getStorage($entity_type_id)
->loadRevision($latest_revision_id);
}
return NULL;
}
/**
* {@inheritdoc}
*/
public function getLatestRevisionId($entity_type_id, $entity_id) {
if ($storage = $this->entityTypeManager
->getStorage($entity_type_id)) {
$revision_ids = $storage
->getQuery()
->allRevisions()
->condition($this->entityTypeManager
->getDefinition($entity_type_id)
->getKey('id'), $entity_id)
->sort($this->entityTypeManager
->getDefinition($entity_type_id)
->getKey('revision'), 'DESC')
->pager(1)
->execute();
if ($revision_ids) {
$revision_id = array_keys($revision_ids)[0];
return $revision_id;
}
}
return NULL;
}
/**
* {@inheritdoc}
*/
public function setRevisionCreationTime(ContentEntityInterface $entity) {
$revision_timestamp_interfaces = [
'Drupal\\entity\\Revision\\EntityRevisionLogInterface',
'Drupal\\node\\NodeInterface',
];
if ($this
->implementsInterface($entity, $revision_timestamp_interfaces)) {
/** @var \Drupal\entity\Revision\EntityRevisionLogInterface|\Drupal\node\NodeInterface $entity */
$entity
->setRevisionCreationTime(\Drupal::time()
->getRequestTime());
}
}
/**
* {@inheritdoc}
*/
public function supportsRevisionOwner(ScheduledUpdateTypeInterface $scheduledUpdateType) {
if ($definition = $this
->getUpdateTypeDefinition($scheduledUpdateType)) {
return $this
->definitionClassImplementsInterface($definition, $this
->revisionOwnerInterfaces());
}
return FALSE;
}
/**
* {@inheritdoc}
*/
public function supportsOwner(ScheduledUpdateTypeInterface $scheduledUpdateType) {
if ($type = $this
->getUpdateTypeDefinition($scheduledUpdateType)) {
return $this
->definitionClassImplementsInterface($type, [
'Drupal\\user\\EntityOwnerInterface',
]);
}
return FALSE;
}
/**
* Get the entity definition for the entity to be updated.
*
* @param \Drupal\scheduled_updates\ScheduledUpdateTypeInterface $scheduledUpdateType
*
* @return array|\Drupal\Core\Entity\EntityTypeInterface|null
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
protected function getUpdateTypeDefinition(ScheduledUpdateTypeInterface $scheduledUpdateType) {
if ($update_entity_type = $scheduledUpdateType
->getUpdateEntityType()) {
return $this->entityTypeManager
->getDefinition($update_entity_type);
}
return NULL;
}
/**
* {@inheritdoc}
*/
public function getPreviousRevision(ContentEntityInterface $entity) {
$storage = $this->entityTypeManager
->getStorage($entity
->getEntityTypeId());
$query = $storage
->getQuery();
$type = $entity
->getEntityType();
$query
->allRevisions()
->condition($type
->getKey('id'), $entity
->id())
->condition($type
->getKey('revision'), $entity
->getRevisionId(), '<')
->sort($type
->getKey('revision'), 'DESC')
->pager(1);
$revision_ids = $query
->execute();
if ($revision_ids) {
$revision_id = array_keys($revision_ids)[0];
return $storage
->loadRevision($revision_id);
}
return NULL;
}
/**
* {@inheritdoc}
*/
public function bundleOptions($entity_type) {
$info = $this->bundleInfo
->getBundleInfo($entity_type);
$options = [];
foreach ($info as $bundle => $bundle_info) {
$options[$bundle] = $bundle_info['label'];
}
return $options;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ClassUtilsTrait:: |
protected | function | ||
ClassUtilsTrait:: |
protected | function | Determines if the class for an entity type definition implements and interface. | |
ClassUtilsTrait:: |
protected | function | ||
ClassUtilsTrait:: |
protected | function | ||
ClassUtilsTrait:: |
protected | function | Get the entity owner if applicable. | |
ClassUtilsTrait:: |
protected | function | Get the revision owner for an ContentEntity. | |
ClassUtilsTrait:: |
protected | function | Determines if an object or class name implements any interfaces in a list. | |
ClassUtilsTrait:: |
protected | function | Get class names of interfaces that support revision ownership. | |
ClassUtilsTrait:: |
protected | function | ||
ClassUtilsTrait:: |
protected | function | ||
ClassUtilsTrait:: |
public | function | ||
ClassUtilsTrait:: |
protected | function | ||
UpdateUtils:: |
protected | property | The bundle info service. | |
UpdateUtils:: |
protected | property | The entity field manager. | |
UpdateUtils:: |
protected | property | The entity type manager. | |
UpdateUtils:: |
public | function |
Create select element bundle options for entity type. Overrides UpdateUtilsInterface:: |
|
UpdateUtils:: |
public | function |
Loads the latest revision of a specific entity. Overrides UpdateUtilsInterface:: |
|
UpdateUtils:: |
public | function |
Returns the revision ID of the latest revision of the given entity. Overrides UpdateUtilsInterface:: |
|
UpdateUtils:: |
public | function |
Get the directly previous revision. Overrides UpdateUtilsInterface:: |
|
UpdateUtils:: |
public | function |
Get whether a new revision should be created by default for this entity. Overrides UpdateUtilsInterface:: |
|
UpdateUtils:: |
public | function |
Get the update type for an update. Overrides UpdateUtilsInterface:: |
|
UpdateUtils:: |
protected | function | Get the entity definition for the entity to be updated. | |
UpdateUtils:: |
public | function |
Overrides UpdateUtilsInterface:: |
|
UpdateUtils:: |
public | function |
Determines if an update supports revisions Overrides UpdateUtilsInterface:: |
|
UpdateUtils:: |
public | function |
Set revision creation time for entities that support it. Overrides UpdateUtilsInterface:: |
|
UpdateUtils:: |
public | function |
Determines if the entity type being updated supports ownership. Overrides UpdateUtilsInterface:: |
|
UpdateUtils:: |
public | function |
Determine if the entity type being update support default revision
setting. Overrides UpdateUtilsInterface:: |
|
UpdateUtils:: |
public | function |
Determines if entity type being updated supports Revision Ownership. Overrides UpdateUtilsInterface:: |
|
UpdateUtils:: |
public | function |
Determine a scheduled update type supports creating new revisions on
update. Overrides UpdateUtilsInterface:: |
|
UpdateUtils:: |
public | function | UpdateUtils constructor. |