I'm using entity reference and entity reference autocreate to make entities on the fly if they don't exist. Works like a charm. Now I would like to make a nicer tag display. The standard comma seperated input-box is working, but isn't user friendly.

The jQuery Plugin chosen creates a tag system like I want, but it uses the select widget instead. I made changes to chosen so I'm able to add items dynamically, and I also made changes to entity reference autocreate (otherwise it's not possible to save entities from a select)

The entity is created, but I get this error: "An illegal choice has been detected. Please contact the site administrator." That's because I added new options to the select with jQuery. Any idea what the best way is to get rid of this message?

This is the code that sends the new and old entity id's to the form:

function entityreference_autocreate_validate_select ($element, &$form_state, $form){
  if (!empty($element['#value'])) {
    $field = field_info_field($element['#field_name']);

    $field['settings']['entityreference_autocreate'] = $element['#entityreference_autocreate_settings'];
    $entities = $element['#value'];
    foreach ($entities as $id) {
      if ($target_id = entityreference_autocreate_get_entity_by_id($field, $id)) {
        $value[] = array(
          'target_id' => $target_id,
  // Update the values.
  form_set_value($element, $value, $form_state);
  • What is your question ?
    – tenken
    Commented May 5, 2015 at 16:23
  • Changed my question, sorry if it wasn't clear. Commented May 5, 2015 at 16:58
  • The entity reference stuff isn't the core of the question, if you focus on the "Illegal choice problem" you might see this question as a duplicate of that one drupal.stackexchange.com/questions/31846/…
    – Ben
    Commented May 6, 2015 at 0:34


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.