You are here

function entityreference_prepopulate_create_node_links in Entityreference prepopulate 7

Return a form element with crafted links to create nodes for a group.

Parameters

$entity_type: The entity type of the referenced entity.

$entity_id: The entity ID of the referenced entity.

$destination: Optional; The destination after a node is created. Defaults to the destination passed in the URL if exists, otherwise back to the current page.

$types: Optional; An array of type names. Restrict the created links to the given types.

1 call to entityreference_prepopulate_create_node_links()
entityreference_prepopulate_node_prepopulate_content_type_render in plugins/content_types/node_prepopulate.inc
Render callback.

File

./entityreference_prepopulate.module, line 412
Prepopulate entity reference values from URL.

Code

function entityreference_prepopulate_create_node_links($entity_type, $entity_id, $field_name, $destination = NULL, $types = NULL) {
  $wrapper = entity_metadata_wrapper($entity_type, $entity_id);
  $field = field_info_field($field_name);
  $entity = entity_load_single($entity_type, $entity_id);
  list(, , $bundle) = entity_extract_ids($entity_type, $entity);
  $types = isset($types) ? $types : array_keys(node_type_get_types());
  $names = array();
  foreach ($types as $type_name) {
    if ($field['settings']['target_type'] != $entity_type) {

      // The entity type isn't referenced by the field.
      continue;
    }
    if (!empty($field['settings']['handler_settings']['target_bundles']) && !in_array($bundle, $field['settings']['handler_settings']['target_bundles'])) {

      // The entity bundle isn't referenced by the field.
      continue;
    }
    $instance = field_info_instance('node', $field_name, $type_name);
    if (empty($instance['settings']['behaviors']['prepopulate']['status'])) {

      // The field doesn't exist on the node type, or doesn't have prepopulate
      // enabled.
      continue;
    }
    if (!node_access('create', $type_name)) {
      continue;
    }
    $names[$type_name] = node_type_get_name($type_name);
  }
  if (empty($names)) {
    return;
  }

  // Sort names.
  asort($names);

  // Build links.
  $options = array(
    'query' => array(
      $field_name => $entity_id,
    ) + drupal_get_destination(),
  );
  $items = array();
  foreach ($names as $type => $name) {
    $items[] = array(
      'data' => l($name, 'node/add/' . str_replace('_', '-', $type), $options),
    );
  }
  $element = array();
  $element['entityreference_prepopulate'] = array(
    '#theme' => 'item_list',
    '#items' => $items,
  );
  return $element;
}