class EmbeddedUpdateRunner in Scheduled Updates 8
The default Embedded Update Runner.
Plugin annotation
@UpdateRunner(
id = "default_embedded",
label = @Translation("Embedded"),
description = @Translation("Handles updates that are embedded on entities via entity reference fields."),
update_types = {"embedded"}
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\scheduled_updates\Plugin\BaseUpdateRunner implements UpdateRunnerInterface uses ClassUtilsTrait
- class \Drupal\scheduled_updates\Plugin\UpdateRunner\EmbeddedUpdateRunner implements EntityMonitorUpdateRunnerInterface
- class \Drupal\scheduled_updates\Plugin\BaseUpdateRunner implements UpdateRunnerInterface uses ClassUtilsTrait
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of EmbeddedUpdateRunner
File
- src/
Plugin/ UpdateRunner/ EmbeddedUpdateRunner.php, line 27 - Contains \Drupal\scheduled_updates\Plugin\UpdateRunner\EmbeddedUpdateRunner.
Namespace
Drupal\scheduled_updates\Plugin\UpdateRunnerView source
class EmbeddedUpdateRunner extends BaseUpdateRunner implements EntityMonitorUpdateRunnerInterface {
/**
* Return all schedule updates that are referenced via Entity Reference
* fields.
*
* @return ScheduledUpdate[]
*/
protected function getEmbeddedUpdates() {
$updates = [];
/** @var String[] $fields */
if ($entity_ids = $this
->getEntityIdsReferencingReadyUpdates()) {
if ($entities = $this
->loadEntitiesToUpdate($entity_ids)) {
$field_ids = $this
->getReferencingFieldIds();
/** @var ContentEntityInterface $entity */
foreach ($entities as $entity) {
/** @var $entity_update_ids - all update ids for this entity for our fields. */
$entity_update_ids = [];
/** @var $field_update_ids - update ids keyed by field_id. */
$field_update_ids = [];
foreach ($field_ids as $field_id) {
// Store with field id.
$field_update_ids[$field_id] = $this
->getEntityReferenceTargetIds($entity, $field_id);
// Add to all for entity.
$entity_update_ids += $field_update_ids[$field_id];
}
// For all entity updates return only those ready to run.
$ready_update_ids = $this
->getReadyUpdateIds($entity_update_ids);
// Loop through updates attached to fields.
foreach ($field_update_ids as $field_id => $update_ids) {
// For updates attached to field get only those ready to run.
$field_ready_update_ids = array_intersect($update_ids, $ready_update_ids);
foreach ($field_ready_update_ids as $field_ready_update_id) {
$updates[] = [
'update_id' => $field_ready_update_id,
// If this is revisionable entity use revision id as key for Runner Plugins that care about revisions.
'entity_ids' => $entity
->getRevisionId() ? [
$entity
->getRevisionId() => $entity
->id(),
] : [
$entity
->id(),
],
'field_id' => $field_id,
'entity_type' => $this
->updateEntityType(),
];
}
}
}
}
}
return $updates;
}
/**
* {@inheritdoc}
*/
protected function getAllUpdates() {
return $this
->getEmbeddedUpdates();
}
/**
* {@inheritdoc}
*/
public function onEntityUpdate(ContentEntityInterface $entity) {
if ($this->updateUtils
->supportsRevisionUpdates($this->scheduled_update_type)) {
$this
->deactivateUpdates($entity);
$this
->reactivateUpdates($entity);
}
}
/**
* Deactivate any Scheduled Updates that are previous revision but not on current.
*
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
*/
protected function deactivateUpdates(ContentEntityInterface $entity) {
$current_update_ids = $this
->getUpdateIdsOnEntity($entity);
// Loop through all previous revisions and deactive updates not on current revision.
$revisions = $this
->getPreviousRevisionsWithUpdates($entity);
if (empty($revisions)) {
return;
}
$all_revisions_update_ids = [];
foreach ($revisions as $revision) {
// array_merge so so elements with same key are not replaced.
$all_revisions_update_ids = array_merge($all_revisions_update_ids, $this
->getUpdateIdsOnEntity($revision));
}
$all_revisions_update_ids = array_unique($all_revisions_update_ids);
$updates_ids_not_on_current = array_diff($all_revisions_update_ids, $current_update_ids);
if ($updates_ids_not_on_current) {
$storage = $this->entityTypeManager
->getStorage('scheduled_update');
foreach ($updates_ids_not_on_current as $update_id) {
/** @var ScheduledUpdateInterface $update */
$update = $storage
->load($update_id);
$update->status = ScheduledUpdateInterface::STATUS_INACTIVE;
$update
->save();
}
}
}
/**
* Reactive any updates that are on this entity that have been deactived previously.
*
* @see ::deactivateUpdates()
*
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
*/
protected function reactivateUpdates(ContentEntityInterface $entity) {
if ($update_ids = $this
->getUpdateIdsOnEntity($entity)) {
$storage = $this->entityTypeManager
->getStorage('scheduled_update');
$query = $storage
->getQuery();
$query
->condition('status', [
ScheduledUpdateInterface::STATUS_UNRUN,
ScheduledUpdateInterface::STATUS_REQUEUED,
], 'NOT IN');
$query
->condition($this->entityTypeManager
->getDefinition('scheduled_update')
->getKey('id'), $update_ids, 'IN');
$non_active_update_ids = $query
->execute();
$non_active_updates = $storage
->loadMultiple($non_active_update_ids);
foreach ($non_active_updates as $non_active_update) {
$non_active_update->status = ScheduledUpdateInterface::STATUS_UNRUN;
}
}
}
/**
* Get all update ids for this connected Update type.
*
* @todo Should results be cached per entity_id and revision_id to avoiding loading updates.
*
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
*
* @param bool $include_inactive
*
* @return array
*/
protected function getUpdateIdsOnEntity(ContentEntityInterface $entity, $include_inactive = FALSE) {
$field_ids = $this
->getReferencingFieldIds();
$update_ids = [];
foreach ($field_ids as $field_id) {
$field_update_ids = $this
->getEntityReferenceTargetIds($entity, $field_id);
// This field could reference other update bundles
// remove any that aren't of the attached scheduled update type.
foreach ($field_update_ids as $field_update_id) {
$update = $this->entityTypeManager
->getStorage('scheduled_update')
->load($field_update_id);
if ($update && $update
->bundle() == $this->scheduled_update_type
->id()) {
if (!$include_inactive) {
if ($update->status->value == ScheduledUpdateInterface::STATUS_INACTIVE) {
continue;
}
}
$update_ids[$field_update_id] = $field_update_id;
}
}
}
return $update_ids;
}
/**
* Get all previous revisions that have updates of the attached type.
*
* This function would be easier and more performant if this core issue with
* Entity Query was fixed: https://www.drupal.org/node/2649268 Without this
* fix can't filter query on type of update and whether they are active. So
* therefore all previous revisions have to be loaded.
*
* @todo Help get that core issue fixed or rewrite this function query table
* fields directly.
*
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
*
* @return \Drupal\Core\Entity\ContentEntityInterface[]
*/
protected function getPreviousRevisionsWithUpdates(ContentEntityInterface $entity) {
/** @var ContentEntityInterface[] $revisions */
$revisions = [];
$type = $entity
->getEntityType();
$query = $this->entityTypeManager
->getStorage($entity
->getEntityTypeId())
->getQuery();
$query
->allRevisions()
->condition($type
->getKey('id'), $entity
->id())
->condition($type
->getKey('revision'), $entity
->getRevisionId(), '<')
->sort($type
->getKey('revision'), 'DESC');
if ($revision_ids = $query
->execute()) {
$revision_ids = array_keys($revision_ids);
$storage = $this->entityTypeManager
->getStorage($entity
->getEntityTypeId());
foreach ($revision_ids as $revision_id) {
/** @var ContentEntityInterface $revision */
$revision = $storage
->loadRevision($revision_id);
if ($update_ids = $this
->getUpdateIdsOnEntity($revision)) {
$revisions[$revision_id] = $revision;
}
}
}
return $revisions;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
BaseUpdateRunner:: |
protected | property | ||
BaseUpdateRunner:: |
protected | property | @var \Drupal\Core\Entity\EntityTypeManagerInterface | |
BaseUpdateRunner:: |
protected | property | @var \Drupal\Core\Entity\EntityFieldManagerInterface | |
BaseUpdateRunner:: |
protected | property | The entity reference field ids target connected update types. | |
BaseUpdateRunner:: |
protected | property | If the runner is currently run by cron. | |
BaseUpdateRunner:: |
protected | property | If the runner is currently switched to a different user. | |
BaseUpdateRunner:: |
protected | property | Queue items that will be released after updates in queue are run. | |
BaseUpdateRunner:: |
protected | property | @var \Drupal\scheduled_updates\entity\ScheduledUpdateType | |
BaseUpdateRunner:: |
protected | property | ||
BaseUpdateRunner:: |
protected | function | Add conditions to a query to select updates to run. | |
BaseUpdateRunner:: |
protected | function | ||
BaseUpdateRunner:: |
public | function |
Add all updates to queue. Overrides UpdateRunnerInterface:: |
|
BaseUpdateRunner:: |
public | function |
Form constructor. Overrides PluginFormInterface:: |
1 |
BaseUpdateRunner:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
BaseUpdateRunner:: |
protected | function | Display message about updates. | |
BaseUpdateRunner:: |
public | function | Get After Run behavior configuration. | |
BaseUpdateRunner:: |
public | function |
Get the description of the Runner Plugin. Overrides UpdateRunnerInterface:: |
1 |
BaseUpdateRunner:: |
protected | function | Get all entity ids for entities that reference updates that are ready to run. | 1 |
BaseUpdateRunner:: |
public | function |
Get target entity ids for an entity reference field on a entity. Overrides UpdateRunnerInterface:: |
|
BaseUpdateRunner:: |
public | function |
Get how this runner should handle invalid entity updates. Overrides UpdateRunnerInterface:: |
|
BaseUpdateRunner:: |
public | function |
Get the Queue for this Update Runner. Overrides UpdateRunnerInterface:: |
|
BaseUpdateRunner:: |
protected | function | Get updates that are ready to be run for this Runner. | |
BaseUpdateRunner:: |
public | function |
Get all field ids that are attached to the entity type to be updated and
target this update type. Overrides UpdateRunnerInterface:: |
|
BaseUpdateRunner:: |
protected | function | Get Scheduled Update Type from the Form State. | |
BaseUpdateRunner:: |
public | function |
@inheritdoc Overrides UpdateRunnerInterface:: |
|
BaseUpdateRunner:: |
protected | function | Load multi entities to update. | 1 |
BaseUpdateRunner:: |
protected | function | Prepare an entity to be updated. | |
BaseUpdateRunner:: |
protected | function | ||
BaseUpdateRunner:: |
protected | function | Remove update from reference field value. | |
BaseUpdateRunner:: |
protected | function | Run an individual update from the queue. | |
BaseUpdateRunner:: |
public | function |
Run all updates that are in the queue. Overrides UpdateRunnerInterface:: |
|
BaseUpdateRunner:: |
protected | function | Set a entity to use a new revision is applicable. | |
BaseUpdateRunner:: |
public | function |
@inheritdoc Overrides UpdateRunnerInterface:: |
|
BaseUpdateRunner:: |
public | function |
Form submission handler. Overrides PluginFormInterface:: |
|
BaseUpdateRunner:: |
protected | function | Switch to another user to run an update if necessary. | |
BaseUpdateRunner:: |
protected | function | If the user has been switch to run an update switch the user back. | |
BaseUpdateRunner:: |
protected | function | Transfer field values from update to entity to be updated. | |
BaseUpdateRunner:: |
public | function |
Return the entity id of the entity type being updated. Overrides UpdateRunnerInterface:: |
|
BaseUpdateRunner:: |
public | function |
Form validation handler. Overrides PluginFormInterface:: |
1 |
BaseUpdateRunner:: |
public | function |
BaseUpdateRunner constructor. Overrides PluginBase:: |
|
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 | ||
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
EmbeddedUpdateRunner:: |
protected | function | Deactivate any Scheduled Updates that are previous revision but not on current. | |
EmbeddedUpdateRunner:: |
protected | function |
Get all schedule updates for this types that should be added to queue. Overrides BaseUpdateRunner:: |
|
EmbeddedUpdateRunner:: |
protected | function | Return all schedule updates that are referenced via Entity Reference fields. | |
EmbeddedUpdateRunner:: |
protected | function | Get all previous revisions that have updates of the attached type. | |
EmbeddedUpdateRunner:: |
protected | function | Get all update ids for this connected Update type. | |
EmbeddedUpdateRunner:: |
public | function |
Fires when entity of type to be updated is changed. Overrides EntityMonitorUpdateRunnerInterface:: |
|
EmbeddedUpdateRunner:: |
protected | function | Reactive any updates that are on this entity that have been deactived previously. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
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. | |
UpdateRunnerInterface:: |
constant | |||
UpdateRunnerInterface:: |
constant | |||
UpdateRunnerInterface:: |
constant | |||
UpdateRunnerInterface:: |
constant | |||
UpdateRunnerInterface:: |
constant | |||
UpdateRunnerInterface:: |
constant | |||
UpdateRunnerInterface:: |
constant | |||
UpdateRunnerInterface:: |
constant | |||
UpdateRunnerInterface:: |
constant | |||
UpdateRunnerInterface:: |
constant | |||
UpdateRunnerInterface:: |
constant | |||
UpdateRunnerInterface:: |
constant |