You are here

function bundle_inherit_validate_field_instance_creation in Bundle Inherit 7

Additional validation function to the field_ui_field_overview_form.

While adding existing field instance, get this form is created for and set form error if any of this children has instance of this field.

1 string reference to 'bundle_inherit_validate_field_instance_creation'
bundle_inherit_form_field_ui_field_overview_form_alter in ./bundle_inherit.module
Implements hook_form_FORMID_alter().

File

./bundle_inherit.module, line 146
Bundle Inherit module.

Code

function bundle_inherit_validate_field_instance_creation($form, &$form_state) {
  $form_values = $form_state['values']['fields'];
  if (!empty($form_values['_add_existing_field']['field_name'])) {
    $children = bundle_inherit_bundle_get_children_all($form['#entity_type'], $form['#bundle']);
    $bundles_with_instance = array();
    foreach ($children as $child) {
      $prior_instance = field_info_instance($form['#entity_type'], $form_values['_add_existing_field']['field_name'], $child);
      if (!empty($prior_instance)) {
        $bundles_with_instance[] = $prior_instance['bundle'];
      }
    }
    if (count($bundles_with_instance) > 0) {
      $string = implode(", ", $bundles_with_instance);
      form_set_error('fields][_add_existing_field', t("Instance of the field %field can't be attached to %bundle bundle because this field instances are already attached to some of this bundle children bundles: %children", array(
        '%bundle' => $form['#bundle'],
        '%field' => $form_values['_add_existing_field']['field_name'],
        '%children' => $string,
      )));
    }
  }
}