You are here

function farm_constraint_form_alter in farmOS 7

Implements hook_form_alter().

File

modules/farm/farm_constraint/farm_constraint.module, line 172
Farm constraint module.

Code

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

  // This alters forms for deleting individual entities.
  // Extract the entity type, bundle, and ID from the form.
  $type = NULL;
  $bundle = NULL;
  $id = NULL;
  switch ($form_id) {

    // Farm asset delete form.
    case 'farm_asset_delete_form':
      $type = 'farm_asset';
      if (!empty($form['farm_asset']['#value'])) {
        $bundle = $form['farm_asset']['#value']->type;
        $id = $form['farm_asset']['#value']->id;
      }
      break;

    // Log delete form.
    case 'log_delete_form':
      $type = 'log';
      if (!empty($form['log']['#value'])) {
        $bundle = $form['log']['#value']->type;
        $id = $form['log']['#value']->id;
      }
      break;

    // User cancel form.
    case 'user_cancel_confirm_form':
      $type = 'user';
      if (!empty($form['_account']['#value'])) {
        $bundle = '';
        $id = $form['_account']['#value']->uid;
      }
      break;

    // Taxonomy term edit form.
    // The taxonomy module uses the same form for editing and deleting,
    // differentiated by the presence of $form_state['confirm_delete'].
    case 'taxonomy_form_term':
      $type = 'taxonomy_term';
      if (!empty($form_state['confirm_delete']) && !empty($form['#term'])) {
        $bundle = $form['#term']->vocabulary_machine_name;
        $id = $form['#term']->tid;
      }
      break;
  }

  // If the entity type and ID were not found, bail (bundle will be blank on
  // user entities).
  if (empty($type) || empty($id)) {
    return;
  }

  // Check to see if any constraints exist for this entity. If not, bail.
  if (!farm_constraint_exists($type, $bundle, $id)) {
    return;
  }

  // From here on, we know that constraints exist, so we want to prevent
  // deletion of the entity.
  // If this is a user, remove additional options.
  if ($type == 'user') {
    unset($form['user_cancel_method']);
    unset($form['user_cancel_confirm']);
    unset($form['user_cancel_notify']);
  }

  // Override the page title.
  drupal_set_title('Unable to delete this record');

  // Modify the form description.
  $form['description']['#markup'] = t('This record cannot be deleted because it is referenced by other records.') . ' ' . t('You must remove all references to this record before you can delete it.');

  // Remove submit handlers and buttons.
  unset($form['#submit']);
  unset($form['actions']);
}