You are here

function farm_group_asset_form_submit in farmOS 7

Submit handler for processing the asset group field.

Parameters

array $form: The form array.

array $form_state: The form state array.

1 string reference to 'farm_group_asset_form_submit'
farm_group_form_farm_asset_form_alter in modules/farm/farm_group/farm_group.module
Implements hook_form_FORM_ID_alter().

File

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

Code

function farm_group_asset_form_submit(array $form, array &$form_state) {

  // Only proceed if the group multiselect has a value, or a new group is
  // being created.
  if (empty($form_state['values']['group']['group']) && empty($form_state['values']['group']['create'])) {
    return;
  }

  // If no groups are being created, and the selected groups match the
  // default value (meaning nothing is changing), bail.
  if (empty($form_state['values']['group']['create']) && $form_state['values']['group']['group'] == $form['group']['group']['#default_value']) {
    return;
  }

  // If an asset doesn't exist, bail.
  if (empty($form_state['values']['farm_asset'])) {
    return;
  }

  // Grab the asset.
  $asset = $form_state['values']['farm_asset'];

  // Load the selected groups.
  $groups = array();
  if (!empty($form_state['values']['group']['group'])) {
    $groups = farm_asset_load_multiple($form_state['values']['group']['group']);
  }

  // If a new group needs to be created, create it and add it to the list.
  if (!empty($form_state['values']['group']['create'])) {

    // Build the new group.
    $values = array(
      'name' => check_plain($form_state['values']['group']['create']),
      'type' => 'group',
    );
    $new_group = entity_create('farm_asset', $values);

    // Save the group and print a message.
    farm_asset_save($new_group);

    // Print a message.
    $label = entity_label('farm_asset', $new_group);
    $uri = entity_uri('farm_asset', $new_group);
    drupal_set_message(t('Group created:') . ' ' . l($label, $uri['path']));

    // Add the group to the array.
    $groups[] = $new_group;
  }

  // Create an observation log to set the group membership.
  farm_group_membership_set($asset, $groups);
}