You are here

function _cer_get_target_bundles in Corresponding Entity References 7.2

Find all bundles reference-able by a given field. If all bundles are reference-able, an empty array is returned.

Parameters

$field: Field info array as returned by field_info_field().

Return value

array

2 calls to _cer_get_target_bundles()
_cer_find_channels in ./cer.admin.inc
The purpose of this function is to answer this question: I am a field instance. Which other fields reference the entity that owns me? And of those instances, which ones can I reference? The answer is returned as an array of CER keys: "entity1…
_cer_find_referrers in ./cer.admin.inc
Find all fields that can reference the given entity type and bundle.

File

./cer.admin.inc, line 223
Administrative functionality, separated for performance purposes.

Code

function _cer_get_target_bundles($field) {
  $target_bundles = array();

  // If the reference field is using a view, load the view and see if it's filtering by the entity
  // type's bundle filter. If it is, the filter values are the target bundles. Otherwise,
  // assume that all bundles can be referenced.
  //
  // @todo Support contextual filters?
  //
  // NOTE: Selection handlers (i.e., $field['settings']['handler']) are plugins owned by
  // Entity Reference. There is no method defined to get an array of referenceable
  // bundles, but hopefully, if CER gains enough traction in the community, such a
  // method can be added to the EntityReference_SelectionHandler interface. This
  // function could then be deprecated, which would be a more flexible, future-proof
  // method of finding a field's target bundles.
  //
  if ($field['settings']['handler'] == 'views') {
    $view_name = $field['settings']['handler_settings']['view']['view_name'];
    $view = views_get_view($view_name);
    if ($view) {
      $view
        ->set_display($field['settings']['handler_settings']['view']['display_name']);
      $info = entity_get_info($field['settings']['target_type']);
      if ($info['entity keys']['bundle'] && ($handler = $view->display_handler
        ->get_handler('filter', $info['entity keys']['bundle']))) {
        $target_bundles = $handler->value;
      }
    }
    else {
      drupal_set_message(t('Could not get target bundles for %field (failed to load view %view).', array(
        '%view' => $view_name,
        '%field' => $field['field_name'],
      )), 'error');
    }
  }
  elseif (isset($field['settings']['handler_settings']['target_bundles'])) {
    $target_bundles = $field['settings']['handler_settings']['target_bundles'];
  }
  else {
    $info = entity_get_info($field['settings']['target_type']);
    $target_bundles = array_keys($info['bundles']);
  }
  return $target_bundles;
}