You are here

function farm_inventory_entity_view_alter in farmOS 7

Implements hook_entity_view_alter().

File

modules/farm/farm_inventory/farm_inventory.module, line 58

Code

function farm_inventory_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'];

  // If inventory management is not enabled for this asset, bail.
  if (!farm_inventory_enabled($asset)) {
    return;
  }

  // Get the asset's inventory.
  $inventory = farm_inventory($asset);

  // If the inventory is an empty string, and the asset is treated as an
  // individual, then set the inventory to 1.
  if ($inventory == '' && farm_inventory_individual($asset)) {
    $inventory = '1';
  }

  // Build the inventory display.
  $output = '<strong>' . t('Inventory') . ':</strong> ' . $inventory;

  // Add it to the build array.
  $build['inventory'] = array(
    '#markup' => $output,
    '#prefix' => '<div class="inventory">',
    '#suffix' => '</div>',
    '#weight' => -120,
  );
}