You are here

function entityreference_autocreate_new_entity in Entityreference Autocreate 7

Create a placeholder item of the type described in the field settings.

1 call to entityreference_autocreate_new_entity()
entityreference_autocreate_get_entity_by_title in ./entityreference_autocreate.module
Fetch the named entity for the field, create it if not found.

File

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

Code

function entityreference_autocreate_new_entity($field_info, $title) {

  // Now make one of the named things.
  $entity_type = $field_info['settings']['target_type'];
  $target_bundle = entityreference_autocreate_get_target_bundle($field_info);
  if (empty($target_bundle)) {
    watchdog(__FUNCTION__, 'Cannot create new entity underneath field %field_name as the desired target bundle is undefined. See the field settings. Autocreate has trouble with view-based entityreference lookups.', array(
      '%field_name' => $field_info['field_name'],
    ), WATCHDOG_WARNING);
    return NULL;
  }

  // Select user depending on settings.
  if (!empty($field_info['settings']['entityreference_autocreate']['author_current_user'])) {
    global $user;
  }
  elseif (!empty($field_info['settings']['entityreference_autocreate']['author'])) {
    $user = user_load_by_name($field_info['settings']['entityreference_autocreate']['author']);
  }
  else {
    $user = user_load(0);
  }

  // Make a skeleton/minimal whatever entity. Probably a node.
  // @see entity_create_stub_entity($entity_type, $ids).
  $entity_info = entity_get_info($entity_type);
  $label_key = 'title';
  if (!empty($entity_info['entity keys']['label'])) {
    $label_key = $entity_info['entity keys']['label'];
  }
  $bundle_key = 'type';
  if (!empty($info['entity keys']['bundle'])) {
    $bundle_key = $info['entity keys']['bundle'];
  }
  $new_entity = NULL;

  // These two attributes seem common to each entity I've met so far.
  $new_entity_values = array(
    $bundle_key => $target_bundle,
    $label_key => $title,
  );
  switch ($entity_type) {
    case 'node':

      // Check the expected published status.
      $status = TRUE;
      if (isset($field_info['settings']['entityreference_autocreate']['status'])) {
        $status = $field_info['settings']['entityreference_autocreate']['status'];
        if ($status == -1) {

          // Use the bundle default.
          $node_options = variable_get('node_options_' . $target_bundle, array(
            'status',
            'promote',
          ));
          $status = in_array('status', $node_options);
        }
      }
      $new_entity_values += array(
        'uid' => $user->uid,
        'name' => isset($user->name) ? $user->name : '',
        'language' => LANGUAGE_NONE,
        'status' => $status,
      );
      $new_entity = entity_create($entity_type, $new_entity_values);
      break;
    case 'taxonomy_term':
      if ($vocabulair = taxonomy_vocabulary_machine_name_load($target_bundle)) {
        $new_entity_values += array(
          'vid' => $vocabulair->vid,
        );
        $new_entity = entity_create($entity_type, $new_entity_values);
      }
      break;
    case 'user':

      // Creating users on the fly is a bit risky,
      // so they are not enabled by default.
      //
      // Entity_info did not define the label_key,
      // and users dont really have bundles.
      $label_key = 'name';
      $target_bundle = 'user';
      $new_entity_values = array(
        $bundle_key => $target_bundle,
        $label_key => $title,
      );
      $new_entity = entity_create($entity_type, $new_entity_values);
      break;
    default:

      // It's some unknown/custom entity.
      // We really can't guess what shape it is.
      // It's likely that each field listed in the infos
      // $entity_info['entity keys']
      // will be required though?
      //
      // It's probably a *little* like a node...
      // but it's a crap-shoot really.
      // Hopefully entity API will take care of the rest of the abstraction
      // and validation needed from here.
      // YMMV.
      $new_entity = entity_create($entity_type, $new_entity_values);
      break;
  }

  // Allow other modules to work on this before we save it.
  drupal_alter('entityreference_autocreate_new_entity', $new_entity, $field_info, $title);
  if (empty($new_entity)) {

    // The entity is unknown so don't continue.
    drupal_set_message(t("The entity that needs to be created is unknown (entityreference_autocreate)"), 'error');
    return NULL;
  }
  entity_save($entity_type, $new_entity);

  // The return from this isn't reliable, check for an ID instead.
  $target_id = entity_id($entity_type, $new_entity);
  $uri = entity_uri($entity_type, $new_entity);
  $strings = array(
    '%entity_type' => $entity_type,
    '%target_bundle' => $target_bundle,
    '!target' => l($new_entity->{$label_key}, $uri['path']),
    '%title' => $title,
  );
  if ($target_id) {
    drupal_alter('entityreference_autocreate_new_saved_entity', $target_id, $entity_type);
    drupal_set_message(t('Created a new %entity_type %target_bundle : !target (entityreference_autocreate)', $strings));
    return $target_id;
  }
  else {

    // Can't say why, but it's probably worth complaining about.
    drupal_set_message(t("Failed to created a new %target_bundle called %title, no id returned (entityreference_autocreate)", $strings), 'error');
    return NULL;
  }
}