The ability to automatically create an entity in case it doesn't already exist, reference it on the field and backreference it (if possible),

Something similar as this:
http://drupal.org/project/noderefcreate
http://drupal.org/project/autocreate

There is also a module for creating the referenced node in a popup, but I don't think this would be in the scope of this module.

However, I believe the requested functionality would.

CommentFileSizeAuthor
#7 erc.zip2.78 KBransom
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Damien Tournoud’s picture

Status: Active » Closed (won't fix)

Thanks for reporting this, but this type of feature is not in the scope of the module. Any other module can provide a new widget on top of the entityreference field, this module is only providing the base functionality.

Fidelix’s picture

Makes sense, Damien.
Thank you for the attention.

If someone takes interest in toying with this:
The noderefcreate module is small (164 lines), and this function seems to be doing the whole work:

<?php
function noderefcreate_autocomplete_validate($element, &$form_state, $form) {
  $field = $form_state['field'][$element['#field_name']][$element['#language']]['field'];
  $instance = $form_state['field'][$element['#field_name']][$element['#language']]['instance'];

  $value = $element['#value'];
  $nid = NULL;

  if (strlen(trim($value)) > 0) {
    // Check whether we have an explicit "[nid:n]" input.
    preg_match('/^(?:\s*|(.*) )?\[\s*nid\s*:\s*(\d+)\s*\]$/', $value, $matches);
    if (!empty($matches)) {
      // Explicit nid. Check that the 'title' part matches the actual title for
      // the nid.
      list(, $title, $nid) = $matches;
      if (!empty($title)) {
        $real_title = db_select('node', 'n')
          ->fields('n', array('title'))
          ->condition('n.nid', $nid)
          ->execute()
          ->fetchField();
        if (trim($title) != trim($real_title)) {
          form_error($element, t('%name: title mismatch. Please check your selection.', array('%name' => $instance['label'])));
        }
      }
    }
    else {
      // No explicit nid (the submitted value was not populated by autocomplete
      // selection). Get the nid of a referencable node from the entered title.
      $options = array(
        'string' => $value,
        'match' => 'equals',
        'ids' => NULL,
        'limit' => 1,
      );
      $reference = node_reference_potential_references($field, $options);
      if ($reference) {
        // @todo The best thing would be to present the user with an
        // additional form, allowing the user to choose between valid
        // candidates with the same title. ATM, we pick the first
        // matching candidate...
        $nid = key($reference);
      }
      else {
        $newnode = NULL;
        if (is_array($field['settings']['referenceable_types'])) {
          foreach (array_filter($field['settings']['referenceable_types']) as $related_type) {
            $newnode->type = $related_type;
          }
        }
        global $user;
        $newnode->uid = $user->uid;
        $newnode->title = $value;
        $newnode->language = $element['#language'];
        if (module_exists('comment')) {
          $newnode->comment = variable_get("comment_$newnode->type", COMMENT_NODE_OPEN);
        }
        node_save($newnode);
        $nid = $newnode->nid;
      }
    }
  } else {
    if (strlen($value) > 0) {
      // if we got here then the user filled the name with spaces
      form_error($element, t('%name: title is empty. Please check your input.', array('%name' => $instance['label'])));
    }
  }

  // Set the element's value as the node id that was extracted from the entered
  // input.
  form_set_value($element, $nid, $form_state);
}
?>

IMO, this seems doable and not so painful.

Yuri’s picture

Although out of scope, a great idea! I make much use of the noderefcreate module.
Just to elaborate a bit more on possibilities: until now creating and populating one field was only possible.
Would be nice to have an Entity Reference Create module that allows to create and populate multiple (if not all) fields of the referenced entity.
That would take away a lot of work that is now done when referencing an entity, navigating to the referenced entity, filling in additional fields, refreshing/opening/closing windows etc. Even more, I think that the nested-entity-fields-reference-create-field is the future. :-)

EDIT: I just realize that something similar is already present in the Field Collection module. Wow I love Drupal.

Fidelix’s picture

Yuri, can you point to where you got that information? Thanks!

Yuri’s picture

The concept of creating other entities while editing an entity, has taken form in the Field Collection module http://drupal.org/project/field_collection. However, that module still needs to mature a bit. But it includes views integration and development goes fast.

ransom’s picture

subscribing

ransom’s picture

FileSize
2.78 KB

I've had some problems addressing the proper settings perhaps someone with more entity knowledge can help? It also needs an administrative front end to select the entity type to auto create & if you want to be fancy the ability to choose which type during setup with maybe some ajax on a non identified content type during auto-complete.

This is virtually a strait port of node ref create that will create an undefined and editable node. Please note also code is not up to drupal, and there's no guarantee of anything than a starting point for those who know what they are doing.

bryancasler’s picture

#1411972: Field Collections and TheVerge Possibly of interest to folks here.

michaek’s picture

I've put @ransom's code, with some changes, in a sandbox here: http://drupal.org/sandbox/michaek/1451014

I would describe it as minimally functional, but I wouldn't feel at all good about using it on a production site.

fox_01’s picture

Here's a module that display a form of the sub related entity and if it doesn't exists then the empty form shows up. Maybe thats what you are looking for.

relation edit widget

nickbits’s picture

I have been searching for this very functionality. Just incase others are also looking for it, I believe you should be looking at the Entity Connect module.

valderama’s picture

Another option very similar to Entity Connect is http://drupal.org/project/references_dialog