radioactivity.module in Radioactivity 8.2
Same filename and directory in other branches
Contains radioactivity.module..
File
radioactivity.moduleView source
<?php
/**
* @file
* Contains radioactivity.module..
*/
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*/
function radioactivity_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the radioactivity module.
case 'help.page.radioactivity':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Radioactivity') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_theme().
*/
function radioactivity_theme() {
$theme = [];
return $theme;
}
/**
* Implements hook_cron().
*/
function radioactivity_cron() {
radioactivity_process_emits();
radioactivity_process_decay();
}
/**
* Apply decay to entities.
*/
function radioactivity_process_decay() {
$field_type_plugin_manager = \Drupal::service('plugin.manager.field.field_type');
$entity_manager = \Drupal::service('entity.manager');
if (!($field_storage_configs = $entity_manager
->getStorage('field_storage_config')
->loadByProperties(array(
'type' => 'radioactivity',
)))) {
return;
}
$time_now = time();
foreach ($field_storage_configs as $field_storage) {
// Only act when field has data.
if ($field_storage
->hasData()) {
$radioactivity_profile = $field_storage
->getSetting('profile');
// Simple view counter.
if ($radioactivity_profile == 0) {
continue;
}
$halflife = $field_storage
->getSetting('halflife', 60 * 60 * 12);
$granularity = $field_storage
->getSetting('granularity', 60 * 60);
$field_name = $field_storage
->get('field_name');
$entity_type_id = $field_storage
->getTargetEntityTypeId();
$query = \Drupal::entityQuery($entity_type_id)
->condition($field_name . '.timestamp', time() - 5, ' < ')
->condition($field_name . '.energy', 0, ' > ');
$nids = $query
->execute();
$entities = \Drupal::service('entity_type.manager')
->getStorage($entity_type_id)
->loadMultiple($nids);
// Loop through entities:
foreach ($entities as $entity) {
$timestamp = $entity->{$field_name}->timestamp;
$energy = $entity->{$field_name}->energy;
switch ($radioactivity_profile) {
case 1:
break;
case 2:
$energy = $energy * pow(2, ($timestamp - $time_now) / $halflife);
break;
}
$entity->{$field_name}
->setValue([
'energy' => $energy,
'timestamp' => $time_now,
]);
$entity
->save();
}
}
}
}
/**
* Process emits from the queue.
*/
function radioactivity_process_emits() {
$entity_manager = \Drupal::service('entity.manager');
// Get emits.
$incidents = \Drupal::state()
->get('radioactivity_incidents');
// Immediately empty the emits queue.
\Drupal::state()
->set('radioactivity_incidents', []);
// Process emits, group them for each entity
$entity_types = [];
foreach ($incidents as $incident) {
$entity_type_id = $incident
->getEntityTypeId();
$entity_id = $incident
->getEntityId();
$field_name = $incident
->getFieldName();
if (!isset($entity_types[$entity_type_id])) {
$entity_types[$entity_type_id] = [];
}
if (!isset($entity_types[$entity_type_id][$entity_id])) {
$entity_types[$entity_type_id][$entity_id] = [];
}
if (!isset($entity_types[$entity_type_id][$entity_id][$field_name])) {
$entity_types[$entity_type_id][$entity_id][$field_name] = 0;
}
$entity_types[$entity_type_id][$entity_id][$field_name] += $incident
->getEnergy();
}
foreach ($entity_types as $entity_type => $emit_entities) {
$entities = \Drupal::service('entity_type.manager')
->getStorage($entity_type)
->loadMultiple(array_keys($emit_entities));
foreach ($entities as $entity) {
foreach ($emit_entities[$entity
->id()] as $field_name => $energy) {
$entity->{$field_name}->energy += $energy;
}
$entity
->save();
}
}
}
Functions
Name | Description |
---|---|
radioactivity_cron | Implements hook_cron(). |
radioactivity_help | Implements hook_help(). |
radioactivity_process_decay | Apply decay to entities. |
radioactivity_process_emits | Process emits from the queue. |
radioactivity_theme | Implements hook_theme(). |