You are here

function entityreference_autocreate_validate in Entityreference Autocreate 7

Make a missing target if asked for by name.

An element_validate callback for autocomplete fields. Replaces _entityreference_autocomplete_validate().

See also

_entityreference_autocomplete_validate()

1 string reference to 'entityreference_autocreate_validate'
entityreference_autocreate_field_widget_form_alter in ./entityreference_autocreate.module
Adjust the behaviour of entityreference autocomplete widgets.

File

./entityreference_autocreate.module, line 181
Intercepts entityreference autocomplete submission validation and creates a target node on the fly if it doesn't yet exist.

Code

function entityreference_autocreate_validate($element, &$form_state, $form) {
  if (empty($element['#value'])) {
    return;
  }
  $field = field_info_field($element['#field_name']);
  $field['settings']['entityreference_autocreate'] = $element['#entityreference_autocreate_settings'];

  // Fetch an entity ID, making it on the fly if needed.
  if ($value = entityreference_autocreate_get_entity_by_title($field, $element['#value'])) {
    form_set_value($element, $value, $form_state);
    return;
  }

  // Something has failed.
  // Either could not create the target
  // (permissions or something?)
  // Or did a lookup and found two identically named targets already existing,
  // so bailed.
  $strings = array(
    '!target' => $element['#value'],
  );
  form_error($element, t('Failed to create or find a target called !target (entityreference_autocreate). This may be due to permissions, autocreate settings on the widget, or possibly if there are two targets with identical titles already on the system.', $strings));
}