You are here

function farm_sensor_entity_load in farmOS 7

Implements hook_entity_load().

File

modules/farm/farm_sensor/farm_sensor.module, line 61

Code

function farm_sensor_entity_load($entities, $type) {

  // Only act on farm_asset entities.
  if ($type != 'farm_asset') {
    return;
  }

  // Iterate through the loaded assets...
  foreach ($entities as $entity) {

    // Only act on sensors...
    if ($entity->type != 'sensor') {
      continue;
    }

    // Load the sensor information from the {farm_sensor} database.
    $query = db_select('farm_sensor', 's');
    $query
      ->fields('s');
    $query
      ->condition('id', $entity->id);
    $result = $query
      ->execute();
    $sensor_info = $result
      ->fetchAssoc();

    // Only continue if sensor info was returned.
    if (empty($sensor_info)) {
      continue;
    }

    // Assign the entity's sensor type.
    $entity->sensor_type = $sensor_info['type'];

    // Assign the entity's sensor settings.
    $entity->sensor_settings = unserialize($sensor_info['settings']);
  }
}