You are here

function field_patterns_validate in Patterns 7

Same name and namespace in other branches
  1. 7.2 patterns_components/components/field.inc \field_patterns_validate()

File

patterns_components/components/field.inc, line 195
Patterns component for Fields.

Code

function field_patterns_validate($action, $tag, &$data) {
  $status = PATTERNS_SUCCESS;
  $msg = '';
  if ($tag == 'field_weight') {
    if (!isset($data['entity_type'])) {
      $status = PATTERNS_ERR;
      $msg = t('FieldException: Field entity type is required.');
    }
    elseif (!isset($data['bundle'])) {
      $status = PATTERNS_ERR;
      $msg = t('FieldException: Field bundle is required.');
    }
  }
  if ($tag == 'field') {
    if (!isset($data['field_name'])) {
      $status = PATTERNS_ERR;
      $msg = t('FieldException: Field name is required.');
    }
  }
  if ($tag == 'instance') {
    $exist = db_query("SELECT COUNT(*) FROM {field_config_instance} WHERE field_name = :name and entity_type = :type and bundle = :bundle", array(
      'bundle' => $data['bundle'],
      'type' => $data['entity_type'],
      'name' => $data['field_name'],
    ))
      ->fetchField();

    // TODO: count()
    if (!isset($data['field_name'])) {
      $status = PATTERNS_ERR;
      $msg = t('FieldException: Field name is required.');
    }
    elseif (!isset($data['entity_type'])) {
      $status = PATTERNS_ERR;
      $msg = t('FieldException: Field entity type is required.');
    }
    elseif (!isset($data['bundle'])) {
      $status = PATTERNS_ERR;
      $msg = t('FieldException: Field bundle is required.');
    }
    elseif ($action == PATTERNS_CREATE && $exist) {
      $status = PATTERNS_ERR;
      $msg = t('Add new field: the field name %name already exists.', array(
        '%name' => $data['field_name'],
      ));
    }
    elseif ($action == PATTERNS_MODIFY && !$exist) {
      $status = PATTERNS_ERR;
      $msg = t('FieldException: Attempt to update an instance of a nonexistent field.');
    }
    elseif ($action == PATTERNS_DELETE && !$exist) {
      $status = PATTERNS_ERR;
      $msg = t('FieldException: Attempt to delete an instance of a nonexistent field');
    }
  }
  return patterns_results($status, $msg);
}