EntityReferenceAutocomplete.php 3.34 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
<?php

/**
 * @file
 * Contains \Drupal\entity_reference/EntityReferenceAutocomplete.
 */
namespace Drupal\entity_reference;

use Drupal\Core\Entity\EntityManager;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;

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

  /**
   * The entity manager.
   *
   * @var \Drupal\Core\Entity\EntityManager
   */
  protected $entityManager;

  /**
   * Constructs a EntityReferenceAutocomplete object.
   *
   * @param \Drupal\Core\Entity\EntityManager $entity_manager
   *   The entity manager.
   */
  public function __construct(EntityManager $entity_manager) {
    $this->entityManager = $entity_manager;
  }

  /**
   * Returns matched labels based on a given field, instance and search string.
   *
   * This function can be used by other modules that wish to pass a mocked
   * definition of the field on instance.
   *
   * @param array $field
   *   The field array definition.
   * @param array $instance
   *   The instance array definition.
   * @param string $entity_type
   *   The entity type.
   * @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
   */
  public function getMatches($field, $instance, $entity_type, $entity_id = '', $prefix = '', $string = '') {
    $target_type = $field['settings']['target_type'];
    $matches = array();
    $entity = NULL;

    if ($entity_id !== 'NULL') {
68
      $entity = $this->entityManager->getStorageController($entity_type)->load($entity_id);
69
      if (!$entity || !$entity->access('view')) {
70 71 72
        throw new AccessDeniedHttpException();
      }
    }
73
    $handler = entity_reference_get_selection_handler($instance, $entity);
74 75 76

    if (isset($string)) {
      // Get an array of matching entities.
77 78
      $widget = entity_get_form_display($instance['entity_type'], $instance['bundle'], 'default')->getComponent($instance['field_name']);
      $match_operator = !empty($widget['settings']['match_operator']) ? $widget['settings']['match_operator'] : 'CONTAINS';
79
      $entity_labels = $handler->getReferenceableEntities($string, $match_operator, 10);
80 81 82 83 84 85 86 87 88 89 90 91

      // 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.
          $key = preg_replace('/\s\s+/', ' ', str_replace("\n", '', trim(decode_entities(strip_tags($key)))));
          // Names containing commas or quotes must be wrapped in quotes.
          if (strpos($key, ',') !== FALSE || strpos($key, '"') !== FALSE) {
            $key = '"' . str_replace('"', '""', $key) . '"';
          }
92
          $matches[$prefix . $key] = $label;
93 94 95 96 97 98 99 100
        }
      }
    }

    return $matches;
  }

}