You are here

function farm_movement_form_farm_asset_form_alter in farmOS 7

Implements hook_form_FORM_ID_alter().

File

modules/farm/farm_movement/farm_movement.location.inc, line 65
Code for managing the location of assets with movement logs.

Code

function farm_movement_form_farm_asset_form_alter(&$form, &$form_state, $form_id) {

  // Get the farm asset entity from the form.
  $asset = $form['farm_asset']['#value'];

  // Get the asset's current location.
  $areas = farm_movement_asset_location($asset);
  $area_names = array();
  if (!empty($areas)) {
    foreach ($areas as $area) {
      if (!empty($area->name)) {

        // Get the area name.
        $name = $area->name;

        // If the area name contains commas, wrap it in quotes.
        if (strpos($area->name, ',') !== FALSE) {
          $name = '"' . $area->name . '"';
        }

        // Add the name to the list.
        $area_names[] = $name;
      }
    }
  }

  // Assemble the list of areas into a string.
  $location = implode(', ', $area_names);

  // Add a field for setting the asset's current location.
  $form['location'] = array(
    '#type' => 'fieldset',
    '#title' => t('Location'),
    '#description' => t('Set the current areas(s) that this asset is in. Separate multiple areas with commas. A movement observation log will be created automatically if you change this field.'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#weight' => 100,
    '#tree' => TRUE,
  );
  $form['location']['areas'] = array(
    '#type' => 'textfield',
    '#title' => t('Current location'),
    '#autocomplete_path' => 'taxonomy/autocomplete/field_farm_area',
    '#default_value' => $location,
    '#maxlength' => NULL,
  );

  // Add validation function to validate location input.
  $form['actions']['submit']['#validate'][] = 'farm_movement_asset_location_validate';

  // Add submit function to process the location.
  $form['actions']['submit']['#submit'][] = 'farm_movement_asset_location_submit';

  // Put the location fieldset into the "General" field group.
  $form['#group_children']['location'] = 'group_farm_general';
}