You are here

function farm_constraint_views_bulk_operations_form_validate in farmOS 7

Validation function for Views Bulk Operations form.

1 string reference to 'farm_constraint_views_bulk_operations_form_validate'
farm_constraint_views_bulk_operations_form_alter in modules/farm/farm_constraint/farm_constraint.module
Implements hook_views_bulk_operations_form_alter().

File

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

Code

function farm_constraint_views_bulk_operations_form_validate(&$form, &$form_state) {

  // Only validate if entities are being deleted.
  if (empty($form_state['values']['operation']) || $form_state['values']['operation'] != 'action::views_bulk_operations_delete_item') {
    return;
  }

  // If the entity type was not saved, bail.
  if (empty($form_state['values']['farm_constraint_entity_type'])) {
    return;
  }

  // Get the entity type.
  $type = $form_state['values']['farm_constraint_entity_type'];

  // Iterate through the selected entities.
  foreach ($form_state['values']['views_bulk_operations'] as $key => $id) {
    if (!empty($id)) {

      // Load the entity.
      $entities = entity_load($type, array(
        $id,
      ));
      $entity = reset($entities);

      // Get the entity bundle.
      list(, , $bundle) = entity_extract_ids($type, $entity);

      // If a constraint exists on the entity, set a form error.
      if (farm_constraint_exists($type, $bundle, $id)) {
        $entity_label = entity_label($type, $entity);
        $entity_uri = entity_uri($type, $entity);
        $message = t('The record <a href="@entity_path">@entity_label</a> cannot be deleted because it is referenced by other records.', array(
          '@entity_path' => url($entity_uri['path']),
          '@entity_label' => $entity_label,
        )) . ' ' . t('You must remove all references to this record before you can delete it.');
        form_set_error('views_bulk_operations][' . $key, $message);
      }
    }
  }
}