1 field.module field_config_create_validate(Config $staging_config, $all_changes)

Implements hook_config_create_validate().

Related topics

File

core/modules/field/field.module, line 456
Attach custom data fields to Backdrop entities.

Code

function field_config_create_validate(Config $staging_config, $all_changes) {
  $config_name = $staging_config->getName();

  // Field validation.
  if (strpos($config_name, 'field.field.') === 0) {
    // Ensure fields are not created with existing names.
    $field_name = str_replace('field.field.', '', $config_name);
    if (field_info_field($field_name)) {
      throw new ConfigValidateException(t('The field "@name" cannot be created because another field with the same name already exists. Delete the field manually before importing the new field.', array('@name' => $field_name)));
    }

    // Do the complete field validation and rethrow any errors that occur.
    try {
      $field = $staging_config->get();
      field_validate_field($field);
    }
    catch (FieldException $e) {
      throw new ConfigValidateException($e->getMessage());
    }
  }

  // Instance validation.
  if (strpos($config_name, 'field.instance.') === 0) {
    // Ensure instances reference new or existing fields. Because we do not know
    // the configuration names of entity types, each module providing an entity
    // type must validate the type exists. See node_config_create_validate(),
    // comment_config_create_validate(), and taxonomy_config_create_validate().
    $field_name = $staging_config->get('field_name');
    $label = $staging_config->get('label');
    if (!field_info_field($field_name) && empty($all_changes['field.field.' . $field_name])) {
      throw new ConfigValidateException(t('The field instance "@label" cannot be created because the field type "@field" does not exist.', array('@label' => $label, '@field' => $field_name)));
    }

    // Do the complete instance validation and rethrow any errors that occur.
    try {
      $instance = $staging_config->get();
      field_validate_instance($instance, FALSE, FALSE);
    }
    catch (FieldException $e) {
      throw new ConfigValidateException($e->getMessage());
    }
  }
}