You are here

function entityreference_get_selection_handler in Entity reference 8

Same name and namespace in other branches
  1. 7 entityreference.module \entityreference_get_selection_handler()

Get the selection handler for a given entityreference field.

6 calls to entityreference_get_selection_handler()
AutocompleteTagsWidget::elementValidate in lib/Drupal/entityreference/Plugin/field/widget/AutocompleteTagsWidget.php
Implements Drupal\entityreference\Plugin\field\widget\DefaultAutocompleteWidget::elementValidate()
AutocompleteWidget::elementValidate in lib/Drupal/entityreference/Plugin/field/widget/AutocompleteWidget.php
Implements Drupal\entityreference\Plugin\field\widget\DefaultAutocompleteWidget::elementValidate()
AutocompleteWidgetBase::getLabels in lib/Drupal/entityreference/Plugin/field/widget/AutocompleteWidgetBase.php
Get the entity labels.
entityReferenceSelectionAccessTest::assertReferencable in lib/Drupal/entityreference/Tests/entityReferenceSelectionAccessTest.php
entityreference_autocomplete_callback in ./entityreference.module
Menu callback: autocomplete the label of an entity.

... See full list

File

./entityreference.module, line 97
Provides a field that can reference other entities.

Code

function entityreference_get_selection_handler($field, $instance = NULL, EntityInterface $entity = NULL) {
  $target_entity_type = $field['settings']['target_type'];

  // Check if the entity type does exist and has a base table.
  $entity_info = entity_get_info($target_entity_type);
  if (empty($entity_info['base table'])) {
    return new Drupal\entityreference\Plugin\Type\Selection\SelectionBroken($field, $instance);
  }
  $plugin = entityreference_get_plugin_manager('selection')
    ->getDefinition($field['settings']['handler']);
  $class = $plugin['class'];
  if ($field['settings']['handler'] == 'base') {

    // TODO: This seems hardcoded, should be improved.
    // What we want to do, is call the right class, based on the entity
    // for refined access control and settings.
    // Also Since we are using PSR-0 how can we allow having any entity?
    // e.g. $class_name = 'SelectionEntityType' . $target_entity_type
    // Convert the entity type name to camel-case.
    $camel_case = str_replace('_', ' ', $target_entity_type);
    $camel_case = ucwords($camel_case);
    $camel_case = str_replace(' ', ' ', $camel_case);
    if (class_exists($class_name = 'Drupal\\entityreference\\Plugin\\Type\\Selection\\SelectionEntityType' . $camel_case)) {
      return new $class_name($field, $instance, $entity);
    }
  }
  if (class_exists($class)) {
    return new $class($field, $instance, $entity);
  }

  // Class does not exist.
  return Drupal\entityreference\Plugin\Type\Selection\SelectionBroken($field, $instance, $entity);
}