You are here

function _radioactivity_update_energy in Radioactivity 7.2

Update field energy for given entity.

Parameters

string $entity_type: The type of entity such as 'node' or 'user'.

int $entity_id: The ID of the entity to update energy for.

string $field_name: The name of the radioactivity field to update.

string $language: The language code of the field, such as LANGUAGE_NONE.

int $energy_delta: The change in energy to apply to the field.

int $time: The Unix timestamp of when the energy was emitted.

bool $force: Optional parameter. Set to TRUE to replace the current energy with the value passed in $energy_delta instead of updating it.

2 calls to _radioactivity_update_energy()
RadioactivityIncident::updateEnergy in includes/RadioactivityIncident.inc
Update energy Drupal only
radioactivity_rules_action_set in ./radioactivity.rules.inc
Action: Set energy

File

./radioactivity.module, line 264
Radioactivity core functionality

Code

function _radioactivity_update_energy($entity_type, $entity_id, $field_name, $language, $energy_delta, $time, $force = FALSE) {
  $entities = entity_load($entity_type, array(
    $entity_id,
  ));
  $entity = reset($entities);

  // Ensure the entity still exists.
  if (!$entity) {
    return;
  }

  // Ensure a field value is set so it can be incremented if $force is not TRUE.
  if (!isset($entity->{$field_name}[$language])) {
    $entity->{$field_name}[$language] = array(
      0 => array(
        RADIOACTIVITY_FIELD_ENERGY => 0.0,
      ),
    );
  }
  if ($force) {
    $entity->{$field_name}[$language][0][RADIOACTIVITY_FIELD_ENERGY] = $energy_delta;
  }
  else {
    $entity->{$field_name}[$language][0][RADIOACTIVITY_FIELD_ENERGY] += $energy_delta;
  }
  $entity->{$field_name}[$language][0][RADIOACTIVITY_FIELD_TIMESTAMP] = $time;

  // Some modules use the original property.
  if (!isset($entity->original)) {
    $entity->original = $entity;
  }

  // Ensure that file_field_update() will not trigger additional usage.
  unset($entity->revision);

  // Check the field info:
  $info = field_info_field($field_name);

  // If we are using the sql storage, we can use the faster field_sql_storage_field_storage_write function:
  if ($info['storage']['type'] == 'field_sql_storage') {
    $fields = array(
      $info['id'],
    );
    field_sql_storage_field_storage_write($entity_type, $entity, 'update', $fields);

    // Clear the field cache for the entity:
    list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
    $entity_info = entity_get_info($entity_type);
    if ($entity_info['field cache']) {
      cache_clear_all("field:{$entity_type}:{$id}", 'cache_field');
    }
    if (module_exists('entitycache')) {
      entity_get_controller($entity_type)
        ->resetCache(array(
        $entity_id,
      ));
    }
  }
  else {

    // In case the field uses another type of storage, invoke the field presave and update hooks.
    field_attach_presave($entity_type, $entity);
    field_attach_update($entity_type, $entity);

    // Clear the cache for this entity.
    entity_get_controller($entity_type)
      ->resetCache(array(
      $entity_id,
    ));
  }

  // Invoke hook_radioactivity_update_energy().
  module_invoke_all('radioactivity_update_energy', $entity, $entity_type, $entity_id, $field_name, $language, $energy_delta, $time, $force);
}