You are here

function farm_group_form_farm_asset_form_alter in farmOS 7

Implements hook_form_FORM_ID_alter().

File

modules/farm/farm_group/farm_group.module, line 176

Code

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

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

  // Get a list of active groups.
  $active_groups = farm_group_options();

  // Get the asset's current group membership.
  $membership = farm_group_asset_membership($asset);

  // Build a list of default options for the group select list below.
  $current_groups = array();
  if (!empty($membership)) {
    foreach ($membership as $group) {
      if (!empty($group->id)) {
        $current_groups[$group->id] = $group->id;
      }
    }
  }

  // Add a field for assigning group membership.
  $form['group'] = array(
    '#type' => 'fieldset',
    '#title' => t('Group membership'),
    '#description' => t('Set the current group membership for this asset. An observation log will be created automatically that assigns the new membership.'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#weight' => 100,
    '#tree' => TRUE,
  );

  // If there are active groups available, show a multi-select field.
  if (!empty($active_groups)) {
    $form['group']['group'] = array(
      '#type' => 'select',
      '#title' => t('Select group(s)'),
      '#multiple' => TRUE,
      '#options' => $active_groups,
      '#default_value' => $current_groups,
    );
  }

  // Add a field for creating a new group.
  $form['group']['create'] = array(
    '#type' => 'textfield',
    '#title' => t('Create a new group'),
    '#description' => t('Optionally enter the name of a new group to be created. The assets will become members of this group.'),
  );

  // Add validate and submit functions and put the fieldset into the general
  // field group.
  $form['actions']['submit']['#validate'][] = 'farm_group_asset_form_validate';
  $form['actions']['submit']['#submit'][] = 'farm_group_asset_form_submit';
  $form['#group_children']['group'] = 'group_farm_general';
}