public function RadioactivityProcessor::queueProcessDecay in Radioactivity 8.3
Same name and namespace in other branches
- 4.0.x src/RadioactivityProcessor.php \Drupal\radioactivity\RadioactivityProcessor::queueProcessDecay()
Queue processing of Radioactivity decays.
Parameters
\Drupal\field\FieldStorageConfigInterface $fieldConfig: Configuration of the field to be processed.
array $entityIds: Entity IDs to be processed.
Overrides RadioactivityProcessorInterface::queueProcessDecay
File
- src/
RadioactivityProcessor.php, line 193
Class
- RadioactivityProcessor
- Class RadioactivityProcessor.
Namespace
Drupal\radioactivityCode
public function queueProcessDecay(FieldStorageConfigInterface $fieldConfig, array $entityIds) {
$entityType = $fieldConfig
->getTargetEntityTypeId();
$fieldName = $fieldConfig
->get('field_name');
$profile = $fieldConfig
->getSetting('profile');
$halfLife = $fieldConfig
->getSetting('halflife');
$cutoff = $fieldConfig
->getSetting('cutoff');
/** @var \Drupal\Core\Entity\ContentEntityInterface[] $entities */
$entities = $this->entityTypeManager
->getStorage($entityType)
->loadMultiple($entityIds);
foreach ($entities as $entity) {
// Do not decay energy for unpublished entities.
if ($entity instanceof EntityPublishedInterface && !$entity
->isPublished()) {
continue;
}
$values = $entity
->get($fieldName)
->getValue();
$timestamp = $values[0]['timestamp'];
$energy = $values[0]['energy'];
$elapsed = $timestamp ? $this->requestTime - $timestamp : 0;
switch ($profile) {
case 'linear':
$energy = $energy > $elapsed ? $energy - $elapsed : 0;
break;
case 'decay':
$energy = $energy * pow(2, -$elapsed / $halfLife);
break;
}
if ($energy > $cutoff) {
// Set the new energy level and update the timestamp.
$entity
->get($fieldName)
->setValue([
'energy' => $energy,
'timestamp' => $this->requestTime,
]);
}
else {
// Reset energy level to 0 if they are below the cutoff value.
$entity
->get($fieldName)
->setValue([
'energy' => 0,
'timestamp' => $this->requestTime,
]);
// Dispatch Symfony event to notify listeners that the energy fell below
// the cutoff value. This is needed for Rules integration, but can be
// used by any module that wants to use events.
$event = new EnergyBelowCutoffEvent($entity);
$this->eventDispatcher
->dispatch('radioactivity_field_cutoff', $event);
}
if ($entity
->getEntityType()
->isRevisionable()) {
$entity
->setNewRevision(FALSE);
}
// Set flag so we can identify this entity save as one that just updates
// the radioactivity value.
$entity->radioactivityUpdate = TRUE;
$entity
->save();
}
}