You are here

field_validation_ui.module in Field Validation 7.2

Add validation rules to field instance.

File

field_validation_ui.module
View source
<?php

/**
 * @file
 * Add validation rules to field instance.
 */

/**
 * Implementation of hook_ctools_plugin_directory().
 */
function field_validation_ui_ctools_plugin_directory($module, $plugin) {
  if ($module == 'ctools' && $plugin == 'export_ui') {
    return 'plugins/' . $plugin;
  }
}

/**
 * Implements hook_menu().
 */
function field_validation_ui_menu() {
  $items = array();
  foreach (entity_get_info() as $entity_type => $entity_info) {
    if ($entity_info['fieldable']) {
      foreach ($entity_info['bundles'] as $bundle_name => $bundle_info) {
        if (isset($bundle_info['admin'])) {

          // Extract path information from the bundle.
          $path = $bundle_info['admin']['path'];

          // Different bundles can appear on the same path (e.g. %node_type and
          // %comment_node_type). To allow field_ui_menu_load() to extract the
          // actual bundle object from the translated menu router path
          // arguments, we need to identify the argument position of the bundle
          // name string ('bundle argument') and pass that position to the menu
          // loader. The position needs to be casted into a string; otherwise it
          // would be replaced with the bundle name string.
          if (isset($bundle_info['admin']['bundle argument'])) {
            $bundle_arg = $bundle_info['admin']['bundle argument'];
            $bundle_pos = (string) $bundle_arg;
          }
          else {
            $bundle_arg = $bundle_name;
            $bundle_pos = '0';
          }

          // This is the position of the %field_ui_menu placeholder in the
          // items below.
          $field_position = count(explode('/', $path)) + 1;

          // Extract access information, providing defaults.
          $access = array_intersect_key($bundle_info['admin'], drupal_map_assoc(array(
            'access callback',
            'access arguments',
          )));
          $access += array(
            'access callback' => 'user_access',
            'access arguments' => array(
              'administer site configuration',
            ),
          );
          $items["{$path}/fields/%field_ui_menu/validation"] = array(
            'load arguments' => array(
              $entity_type,
              $bundle_arg,
              $bundle_pos,
              '%map',
            ),
            'title' => 'Validation',
            'page callback' => 'field_validation_ui_callback_dispatch',
            'page arguments' => array(
              $field_position,
            ),
            'type' => MENU_LOCAL_TASK,
            'file' => 'field_validation_ui.admin.inc',
          ) + $access;
          $items[$path . '/fields/%field_ui_menu/validation/add'] = array(
            'load arguments' => array(
              $entity_type,
              $bundle_arg,
              $bundle_pos,
              '%map',
            ),
            'title' => 'Add validation',
            'page callback' => 'drupal_get_form',
            'page arguments' => array(
              'field_validation_ui_manage_rule',
              $field_position,
              'add',
            ),
            'type' => MENU_CALLBACK,
            'file' => 'field_validation_ui.admin.inc',
          ) + $access;
          $items["{$path}/fields/%field_ui_menu/validation/edit"] = array(
            'load arguments' => array(
              $entity_type,
              $bundle_arg,
              $bundle_pos,
              '%map',
            ),
            'title' => 'Edit rule',
            'page callback' => 'drupal_get_form',
            'page arguments' => array(
              'field_validation_ui_manage_rule',
              $field_position,
              'edit',
            ),
            'type' => MENU_CALLBACK,
            'file' => 'field_validation_ui.admin.inc',
          ) + $access;
          $items["{$path}/fields/%field_ui_menu/validation/overwrite"] = array(
            'load arguments' => array(
              $entity_type,
              $bundle_arg,
              $bundle_pos,
              '%map',
            ),
            'title' => 'Overwrite rule',
            'page callback' => 'drupal_get_form',
            'page arguments' => array(
              'field_validation_ui_manage_rule',
              $field_position,
              'overwrite',
            ),
            'type' => MENU_CALLBACK,
            'file' => 'field_validation_ui.admin.inc',
          ) + $access;
          $items["{$path}/fields/%field_ui_menu/validation/delete"] = array(
            'load arguments' => array(
              $entity_type,
              $bundle_arg,
              $bundle_pos,
              '%map',
            ),
            'title' => 'Delete rule',
            'page callback' => 'drupal_get_form',
            'page arguments' => array(
              'field_validation_ui_delete_rule',
            ),
            'type' => MENU_CALLBACK,
            'file' => 'field_validation_ui.admin.inc',
          ) + $access;
        }
      }
    }
  }
  return $items;
}

/**
 * Implements hook_theme().
 */
function field_validation_ui_theme() {
  return array(
    'field_validation_ui_manage_add_rule' => array(
      'variables' => array(
        'instance' => NULL,
      ),
    ),
    'field_validation_ui_manage_overview' => array(
      'variables' => array(
        'rules' => NULL,
        'instance' => NULL,
      ),
    ),
  );
}

/**
 * Implements hook_module_implements_alter().
 *
 * Ensures the call to field_validation_ui_form_field_ui_field_overview_form_alter()
 * function runs after any invocation of the form_alter() by other modules, e.g.
 * Field Group module.
 */
function field_validation_ui_module_implements_alter(&$implementations, $hook) {
  if ($hook == 'form_alter' && array_key_exists('field_validation_ui', $implementations)) {
    $group = $implementations['field_validation_ui'];
    unset($implementations['field_validation_ui']);
    $implementations['field_validation_ui'] = $group;
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 * Using hook_form_field_ui_field_overview_form_alter.
 */
function field_validation_ui_form_field_ui_field_overview_form_alter(&$form, &$form_state) {
  $entity_type = $form['#entity_type'];
  $bundle = $form['#bundle'];
  $bundle = field_extract_bundle($entity_type, $bundle);
  $admin_path = _field_ui_bundle_admin_path($entity_type, $bundle);
  $table =& $form['fields'];

  // Find the operations column and number of existing operations, because
  // other modules may alter the form to add operations before or after us.
  foreach ($table['#header'] as $key => $header) {
    if (is_array($header) && !empty($header['data'])) {
      $op_col = $key;
      $op_count = $header['colspan'];
      continue;
    }
  }

  // Increment the colspan.
  $table['#header'][$op_col]['colspan'] = $op_count + 1;
  $instances = field_info_instances($entity_type, $bundle);
  foreach (element_children($table) as $key) {
    if (array_key_exists($key, $instances)) {
      $field = field_info_field($instances[$key]['field_name']);
      $admin_field_path = $admin_path . '/fields/' . $instances[$key]['field_name'];
      $table[$key]['validation'] = array(
        '#type' => 'link',
        '#title' => t('validate'),
        '#href' => $admin_field_path . '/validation',
        '#options' => array(
          'attributes' => array(
            'title' => t('Manage validation rules.'),
          ),
        ),
      );
    }
    else {
      $table[$key]['validation'] = array(
        '#markup' => '',
      );
    }
  }
}