function farm_livestock_weight_entity_view_alter in farmOS 7
Implements hook_entity_view_alter().
File
- modules/
farm/ farm_livestock/ farm_livestock_weight/ farm_livestock_weight.module, line 822 - Farm livestock weight module.
Code
function farm_livestock_weight_entity_view_alter(&$build, $type) {
// If it's not a farm_asset, or if the entity object is not available, bail.
if ($type != 'farm_asset' || empty($build['#entity'])) {
return;
}
// Alias the asset variable.
$asset = $build['#entity'];
$asset_uri = entity_uri('farm_asset', $asset);
// If it isn't an animal asset, bail.
if ($asset->type != 'animal') {
return;
}
// Get the animal's current weight.
$weight = farm_livestock_weight($asset);
// If a weight measurement isn't available, bail.
if (empty($weight)) {
return;
}
// Get the value and the units.
$value = !empty($weight['value']) ? $weight['value'] : '';
$units = !empty($weight['units']) ? $weight['units'] : '';
// If the animal has an inventory greater than 1, add "(average)".
$inventory = farm_inventory($asset);
$average_text = '';
if ($inventory > 1) {
$average_text .= ' (' . t('average') . ')';
}
// Build the weight display.
$output = '<strong>' . t('Weight') . ':</strong> ' . $value . ' ' . $units . $average_text . '<a href="' . url($asset_uri['path'] . '/weight') . t('"> (weight report) </a>');
// Load the daily live weight gain.
$dlwg = farm_livestock_weight_dlwg($asset);
if (!empty($dlwg)) {
// Add the dlwg markup.
$output .= $dlwg['markup'];
}
// Add it to the build array.
$build['weight'] = array(
'#markup' => $output,
'#prefix' => '<div class="weight">',
'#suffix' => '</div>',
'#weight' => -120,
);
}