You are here

function inline_entity_form_reference_form_validate in Inline Entity Form 7

Same name and namespace in other branches
  1. 8 inline_entity_form.module \inline_entity_form_reference_form_validate()

Validates the form for adding existing entities.

Parameters

$reference_form: The reference entity form.

$form_state: The form state of the parent form.

1 string reference to 'inline_entity_form_reference_form_validate'
inline_entity_form_reference_form in ./inline_entity_form.module
Provides the form for adding existing entities through an autocomplete field.

File

./inline_entity_form.module, line 1114
Provides a widget for inline management (creation, modification, removal) of referenced entities. The primary use case is the parent -> children one (for example, order -> line items), where the child entities are never managed outside the…

Code

function inline_entity_form_reference_form_validate(&$reference_form, &$form_state) {
  $ief_id = $reference_form['#ief_id'];
  $entity_type = $reference_form['#entity_type'];
  $parents_path = implode('][', $reference_form['#parents']);

  // Instantiate controller to access labels
  $instance = $form_state['inline_entity_form'][$ief_id]['instance'];
  $controller = inline_entity_form_get_controller($instance);
  $labels = $controller
    ->labels();
  $form_values = drupal_array_get_nested_value($form_state['values'], $reference_form['#parents']);
  $attach_entity = entity_load_single($entity_type, $form_values['entity_id']);

  // Check to see if entity is already referenced by current IEF widget
  if (!empty($attach_entity)) {
    foreach ($form_state['inline_entity_form'][$ief_id]['entities'] as $key => $value) {
      if ($value['entity'] == $attach_entity) {
        form_set_error($parents_path . '][existing_entity', t('The selected @label has already been added.', array(
          '@label' => $labels['singular'],
        )));
        break;
      }
    }
  }
  else {
    form_set_error($parents_path . '][existing_entity', t('The selected @label is not valid.', array(
      '@label' => $labels['singular'],
    )));
  }
}