EntityReferenceAutocomplete.php 3.94 KB
Newer Older
1 2 3 4 5 6
<?php

/**
 * @file
 * Contains \Drupal\entity_reference/EntityReferenceAutocomplete.
 */
7

8 9
namespace Drupal\entity_reference;

10
use Drupal\Component\Utility\String;
11
use Drupal\Component\Utility\Tags;
12
use Drupal\Core\Entity\EntityManagerInterface;
13
use Drupal\Core\Field\FieldDefinitionInterface;
14
use Drupal\entity_reference\Plugin\Type\SelectionPluginManager;
15
use Drupal\field\FieldConfigInterface;
16 17 18 19 20 21 22 23 24 25
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;

/**
 * Helper class to get autocompletion results for entity reference.
 */
class EntityReferenceAutocomplete {

  /**
   * The entity manager.
   *
26
   * @var \Drupal\Core\Entity\EntityManagerInterface
27 28 29
   */
  protected $entityManager;

30 31 32 33 34 35 36
  /**
   * The Entity reference selection handler plugin manager.
   *
   * @var \Drupal\entity_reference\Plugin\Type\SelectionPluginManager
   */
  protected $selectionHandlerManager;

37 38 39
  /**
   * Constructs a EntityReferenceAutocomplete object.
   *
40
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
41
   *   The entity manager.
42 43
   * @param \Drupal\entity_reference\Plugin\Type\SelectionPluginManager $selection_manager
   *   The Entity reference selection handler plugin manager.
44
   */
45
  public function __construct(EntityManagerInterface $entity_manager, SelectionPluginManager $selection_manager) {
46
    $this->entityManager = $entity_manager;
47
    $this->selectionHandlerManager = $selection_manager;
48 49 50
  }

  /**
51
   * Returns matched labels based on a given search string.
52 53 54 55
   *
   * This function can be used by other modules that wish to pass a mocked
   * definition of the field on instance.
   *
56 57
   * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
   *   The field definition.
58 59
   * @param string $entity_type
   *   The entity type.
60 61
   * @param string $bundle
   *   The entity bundle.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
   * @param string $entity_id
   *   (optional) The entity ID the entity reference field is attached to.
   *   Defaults to ''.
   * @param string $prefix
   *   (optional) A prefix for all the keys returned by this function.
   * @param string $string
   *   (optional) The label of the entity to query by.
   *
   * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
   *   Thrown when the current user doesn't have access to the specifies entity.
   *
   * @return array
   *   A list of matched entity labels.
   *
   * @see \Drupal\entity_reference\EntityReferenceController
   */
78
  public function getMatches(FieldDefinitionInterface $field_definition, $entity_type, $bundle, $entity_id = '', $prefix = '', $string = '') {
79 80 81 82
    $matches = array();
    $entity = NULL;

    if ($entity_id !== 'NULL') {
83
      $entity = $this->entityManager->getStorage($entity_type)->load($entity_id);
84
      if (!$entity || !$entity->access('view')) {
85 86 87
        throw new AccessDeniedHttpException();
      }
    }
88
    $handler = $this->selectionHandlerManager->getSelectionHandler($field_definition, $entity);
89 90 91

    if (isset($string)) {
      // Get an array of matching entities.
92
      $widget = entity_get_form_display($entity_type, $bundle, 'default')->getComponent($field_definition->getName());
93
      $match_operator = !empty($widget['settings']['match_operator']) ? $widget['settings']['match_operator'] : 'CONTAINS';
94
      $entity_labels = $handler->getReferenceableEntities($string, $match_operator, 10);
95 96 97 98 99 100 101

      // Loop through the entities and convert them into autocomplete output.
      foreach ($entity_labels as $values) {
        foreach ($values as $entity_id => $label) {
          $key = "$label ($entity_id)";
          // Strip things like starting/trailing white spaces, line breaks and
          // tags.
102
          $key = preg_replace('/\s\s+/', ' ', str_replace("\n", '', trim(String::decodeEntities(strip_tags($key)))));
103
          // Names containing commas or quotes must be wrapped in quotes.
104
          $key = Tags::encode($key);
105
          $matches[] = array('value' => $prefix . $key, 'label' => $label);
106 107 108 109 110 111 112 113
        }
      }
    }

    return $matches;
  }

}