You are here

radioactivity.module in Radioactivity 4.0.x

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\Form\FormStateInterface;
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 radioactivity field') . '</dt>';
      $output .= '<dd>' . t('Two Radioactivity fields are available. When you start with Radioactivity module, you are advised to use the Radioactivity Reference field. The Radioactivity field (General section) is for existing sites and will de deprecated in the future.') . '</dd>';
      $output .= '<dd>' . t('Attach the Radioactivity (Reference) field to your entity.') . '</dd>';
      $output .= '<dd>' . t('Configure the field settings if required, default should be sufficient. A detailed explanation is available in README.txt.') . '</dd>';
      $output .= '<dt>' . t('Manage form display') . '</dt>';
      $output .= '<dd>' . t('Enable the widget to allow editors to change the energy value.') . '</dd>';
      $output .= '<dt>' . t('Manage display') . '</dt>';
      $output .= '<dd>' . t('Use the Emitter widget in the view modes that should be measured for popularity. Disable the widget in view modes that are not relevant for popularity.') . '</dd>';
      $output .= '<dd>' . t('Configure the Emitter with the amount of energy that will be added per view.') . '</dd>';
      $output .= '<dt>' . t('Use cron') . '</dt>';
      $output .= '<dd>' . t('Cron must be enabled to update the energy levels.') . '</dd>';
      $output .= '</dl>';
      $output .= '<h3>' . t('Using with views') . '</h3>';
      $output .= '<dl>';
      $output .= '<dt>' . t('Configure display') . '</dt>';
      $output .= '<dd>' . t('When using the Radioactivity Reference field, use the Relation ship "Radioactivity referenced from ..." to include the Radioactivity data into your Content view.') . '</dd>';
      $output .= '<dd>' . t('To sorting the view on popularity sort the results on descending "energy".') . '</dd>';
      $output .= '</dl>';
      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;
  }
}

/**
 * Implements hook_form_FORM_ID_alter() for 'field_config_edit_form'.
 */
function radioactivity_form_field_config_edit_form_alter(array &$form, FormStateInterface $form_state) {
  $fieldConfig = $form_state
    ->getFormObject()
    ->getEntity();
  if ($fieldConfig
    ->getType() !== 'radioactivity_reference') {
    return;
  }
  if (isset($form['required'])) {
    $form['required']['#disabled'] = TRUE;
    $form['required']['#default_value'] = TRUE;
  }
}

/**
 * Returns a unique emission counter.
 *
 * Used to uniquely identify an emitter on a page. This was build as a static
 * counter to work across multiple entities and/or multiple formatters and
 * fields displayed on the same page.
 *
 * @return int
 *   The emit ID.
 */
function radioactivity_unique_emit_id() {
  static $id = 0;
  return $id++;
}