You are here

function farm_movement_populate_name in farmOS 7

Helper function for populating a log's name when it contains movement info.

Parameters

Entity $log: The log entity.

2 calls to farm_movement_populate_name()
farm_livestock_move_form_submit in modules/farm/farm_livestock/farm_livestock.farm_quick.move.inc
Submit function for movement quick form.
farm_movement_entity_presave in modules/farm/farm_movement/farm_movement.module
Implements hook_entity_presave().

File

modules/farm/farm_movement/farm_movement.module, line 261
Farm movement.

Code

function farm_movement_populate_name($log) {

  // If the log already has a name, bail.
  if (!empty($log->name)) {
    return;
  }

  // Create an entity wrapper for the log.
  $log_wrapper = entity_metadata_wrapper('log', $log);

  // If there are no assets, bail.
  if (empty($log_wrapper->field_farm_asset)) {
    return;
  }

  // If the movement field collection doesn't exist, bail.
  if (!isset($log_wrapper->field_farm_movement) || empty($log_wrapper->field_farm_movement
    ->value())) {
    return;
  }

  // If there are no "movement to" areas, bail.
  if (empty($log_wrapper->field_farm_movement->field_farm_move_to)) {
    return;
  }

  // If there are more than one assets, summarize.
  $asset_names = '';
  $count_assets = count($log_wrapper->field_farm_asset);
  if ($count_assets > 1) {
    $asset_names .= $log_wrapper->field_farm_asset[0]->name
      ->value() . ' (+ ' . ($count_assets - 1) . ' ' . t('more') . ')';
  }
  else {
    $asset_names .= $log_wrapper->field_farm_asset[0]->name
      ->value();
  }

  // If there are more than one areas, summarize.
  $area_names = '';
  $count_areas = count($log_wrapper->field_farm_movement->field_farm_move_to);
  if ($count_areas > 1) {
    $area_names .= $log_wrapper->field_farm_movement->field_farm_move_to[0]->name
      ->value() . ' (+ ' . ($count_areas - 1) . ' ' . t('more') . ')';
  }
  else {
    $area_names .= $log_wrapper->field_farm_movement->field_farm_move_to[0]->name
      ->value();
  }

  // Build the log name.
  $log->name = t('Move') . ' ' . $asset_names . ' to ' . $area_names;
}