You are here

function references_dialog_entityreference_add_link in References dialog 7

Add link callback for entity references.

1 string reference to 'references_dialog_entityreference_add_link'
references_dialog_references_dialog_widgets in ./references_dialog.dialog_widgets.inc
Implements hook_references_dialog_widgets().

File

./references_dialog.dialog_widgets.inc, line 245
Implements dialogs for node reference and user reference fields.

Code

function references_dialog_entityreference_add_link($element, $widget_settings, $field, $instance) {
  $add_links = array();
  $entity_type = $field['settings']['target_type'];
  $entity_info = entity_get_info($entity_type);
  $entity_bundles = array_keys($entity_info['bundles']);
  if (!empty($field['settings']['handler_settings']['target_bundles'])) {
    $bundles = $field['settings']['handler_settings']['target_bundles'];
  }
  elseif (!empty($field['settings']['handler_settings']['view'])) {
    $name = $field['settings']['handler_settings']['view']['view_name'];
    $display = $field['settings']['handler_settings']['view']['display_name'];
    $views = views_get_view($name);
    $views_display = $views->display;
    $bundle_key = $entity_info['bundle keys']['bundle'];
    if (isset($views_display[$display]->display_options['filters'][$bundle_key])) {
      $views_filters = $views_display[$display]->display_options['filters'][$bundle_key]['value'];
    }
    else {
      if (!empty($views_display['default']->display_options['filters'][$bundle_key]['value'])) {
        $views_filters = $views_display['default']->display_options['filters'][$bundle_key]['value'];
      }
    }
    $bundles = array_keys($views_filters);
  }
  elseif (isset($entity_info['bundles'])) {

    // If the entity target bundles is empty, it means all target bundles are allowed. Fill it all up!
    $bundles = $entity_bundles;
  }

  // Create a link for each allowed bundles.
  if (isset($bundles)) {
    foreach ($bundles as $bundle) {
      if (in_array($bundle, $entity_bundles) && ($link = references_dialog_entityreference_link_helper($entity_type, $bundle))) {
        $add_links[] = $link;
      }
    }
  }
  return $add_links;
}