You are here

function entityreference_autocreate_get_target_bundle in Entityreference Autocreate 7

Find the desired target entity type (bundle name).

Utility function. Abstracted as we need to know this often, and we also need to handle the basic vs views-based lookup each time.

Returns either an array (which usually indicates a config problem) or just the first element in that array.

Parameters

$field_info: The field info from the current context.

bool $multiple: If set, return an array of bundle names.

Return value

string|array|null The bundle name.

3 calls to entityreference_autocreate_get_target_bundle()
entityreference_autocreate_field_widget_form_alter in ./entityreference_autocreate.module
Adjust the behaviour of entityreference autocomplete widgets.
entityreference_autocreate_get_entity_by_title in ./entityreference_autocreate.module
Fetch the named entity for the field, create it if not found.
entityreference_autocreate_new_entity in ./entityreference_autocreate.module
Create a placeholder item of the type described in the field settings.

File

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

Code

function entityreference_autocreate_get_target_bundle($field_info, $multiple = FALSE) {

  // We look in different places depending on the selected lookup 'handler'.
  $settings = $field_info['settings'];
  if ($settings['handler'] == 'base') {

    // The autocomplete is easy.
    if (!empty($settings['handler_settings']['target_bundles'])) {
      $target_bundles = $settings['handler_settings']['target_bundles'];
    }
  }
  if ($settings['handler'] == 'views') {

    // Figuring the base entity bundle from views is difficult.
    // We have required it to be defined via UI, not introspection.
    // So the needed data is not in field_info (base),
    // it was packaged with the widget settings (instance).
    // Need to retrieve it from there...

    #$field_instance = field_info_instance($entity_type, $field_info['field_name'], $bundle);

    // @Squash warning in case it's undefined due to an upgrade.
    $target_bundles = array(
      @$settings['entityreference_autocreate']['bundle'],
    );
    if (empty($target_bundles)) {
      watchdog('entityreference_autocreate', 'Deducing the bundle from a view config is not yet possible, or it failed. Giving up, sorry.', array(), WATCHDOG_NOTICE);
      return NULL;
    }
  }

  // Fallback.
  if (empty($target_bundles)) {

    // So, not all entities have bundles. 'user' doesn't. Fake it for now.
    // User entities are special - they have no bundle.
    // (or it's 'user' but not explicit about it in the entityreference options)
    // I guess there may be other entities like that also. Try to catch them,
    // by assuming that their entity type and their bundle id are the same.
    $target_bundles = array(
      $settings['target_type'],
    );
    watchdog('entityreference_autocreate', 'Guessing that the "bundle" for things of type %entity_type is %target_bundle. This may be wrong, please report what you are trying to do as an issue.', array(
      '%entity_type' => $settings['target_type'],
      '%target_bundle' => reset($target_bundles),
    ), WATCHDOG_NOTICE);
  }
  if (count($target_bundles) != 1) {
    watchdog('entityreference_autocreate', 'Can only autocreate an entity if there is exactly one target bundle. Check the widget settings for %field_name', array(
      '%field_name' => $field_info['field_name'],
    ), WATCHDOG_NOTICE);
  }
  if (empty($target_bundles)) {
    watchdog('entityreference_autocreate', 'No valid target bundle setting found for field %field_name', array(
      '%field_name' => $field_info['field_name'],
    ), WATCHDOG_NOTICE);
    return NULL;
  }
  return $multiple ? $target_bundles : reset($target_bundles);
}