You are here

radioactivity.module in Radioactivity 8.3

Provides a field type which can be used as a hotness metric.

File

radioactivity.module
View source
<?php

/**
 * @file
 * Provides a field type which can be used as a hotness metric.
 */
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\radioactivity\EntityOperations;

/**
 * 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('With the Radioactivity module you can measure popularity of your content. In combination with Views you can makes lists of popular content.') . '</p>';
      $output .= '<h3>' . t('Configuration') . '</h3>';
      $output .= '<dl>';
      $output .= '<dt>' . t('Add field') . '</dt>';
      $output .= '<dd>' . t('Attach the Radioactivity field to your entity') . '</dd>';
      $output .= '<dd>' . t('Configure field on field settings if required, default should be sufficient') . '</dd>';
      $output .= '<dt>' . t('Manage display') . '</dt>';
      $output .= '<dd>' . t('On manage display, set field as enabled') . '</dd>';
      $output .= '<dd>' . t('Configure field on manage display page setting energy and emit mode, ensure enabled on the displays you want to record activity on.') . '</dd>';
      $output .= '<dt>' . t('Cron') . '</dt>';
      $output .= '<dd>' . t('Energy will be updated when cron is run') . '</dd></ul>';
      $output .= '</dl>';
      $output .= '<h3>' . t('Using with views') . '</h3>';
      $output .= '<p>' . t("For sorting use the field associated with your entity using the 'energy' component of the field, sort using asc or desc.") . '</p>';
      return $output;
    default:
  }
}

/**
 * Implements hook_page_attachments_alter().
 */
function radioactivity_page_attachments_alter(array &$attachments) {
  \Drupal::service('radioactivity.storage')
    ->getConfiguredStorage()
    ->injectSettings($attachments);
}

/**
 * Implements hook_entity_load().
 */
function radioactivity_entity_load($entities, $type) {

  // In order for the field formatters to be rendered we need to make sure
  // the field actually has something in it to trigger the formatters.
  $fields = radioactivity_get_field_names();

  /** @var \Drupal\Core\Entity\FieldableEntityInterface[] $entities */
  foreach ($entities as &$entity) {
    foreach ($fields as $field_name) {
      if (is_a($entity, FieldableEntityInterface::class) && $entity
        ->hasField($field_name)) {
        if (!$entity
          ->get($field_name)->energy) {
          $entity
            ->get($field_name)->energy = 0;
          $entity
            ->get($field_name)->timestamp = 0;
        }
      }
    }
  }
}

/**
 * Get a list of Radioactivity field names.
 */
function radioactivity_get_field_names() {
  static $fields;
  if (is_array($fields)) {
    return $fields;
  }
  $fields = [];
  $entity_type_manager = \Drupal::service('entity_type.manager');

  /** @var \Drupal\field\Entity\FieldStorageConfig[] $field_storage_configs */
  if (!($field_storage_configs = $entity_type_manager
    ->getStorage('field_storage_config')
    ->loadByProperties([
    'type' => 'radioactivity',
  ]))) {
    return $fields;
  }
  foreach ($field_storage_configs as $field_storage) {
    $fields[] = $field_storage
      ->get('field_name');
  }
  return $fields;
}

/**
 * Implements hook_cron().
 */
function radioactivity_cron() {

  /** @var \Drupal\radioactivity\RadioactivityProcessorInterface $processor */
  $processor = \Drupal::service('radioactivity.processor');
  $processor
    ->processDecay();
  $processor
    ->processIncidents();
}

/**
 * Implements hook_entity_presave().
 */
function radioactivity_entity_presave(EntityInterface $entity) {
  return \Drupal::service('class_resolver')
    ->getInstanceFromDefinition(EntityOperations::class)
    ->entityPresave($entity);
}

/**
 * Implements hook_module_implements_alter().
 */
function radioactivity_module_implements_alter(&$implementations, $hook) {
  if ($hook === 'entity_presave') {

    // Make sure the radioactivity's entity presave action occurs after the
    // content moderation's entity presave action.
    $group = $implementations['radioactivity'];
    unset($implementations['radioactivity']);
    $implementations['radioactivity'] = $group;
  }
}