Builds an EntityQuery to get referencable entities.
string|null $match: (Optional) Text to match the label against. Defaults to NULL.
string $match_operator: (Optional) The operation the matching should be done with. Defaults to "CONTAINS".
\Drupal\Core\Entity\Query\QueryInterface The EntityQuery object with the basic conditions and sorting applied to it.
public function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') {
$target_type = $this->field['settings']['target_type'];
$entity_info = entity_get_info($target_type);
$query = \Drupal::entityQuery($target_type);
if (!empty($this->instance['settings']['handler_settings']['target_bundles'])) {
$query
->condition($entity_info['entity_keys']['bundle'], $this->instance['settings']['handler_settings']['target_bundles'], 'IN');
}
if (isset($match) && isset($entity_info['entity_keys']['label'])) {
$query
->condition($entity_info['entity_keys']['label'], $match, $match_operator);
}
// Add entity-access tag.
$query
->addTag($this->field['settings']['target_type'] . '_access');
// Add the Selection handler for
// entity_reference_query_entity_reference_alter().
$query
->addTag('entity_reference');
$query
->addMetaData('field', $this->field);
$query
->addMetaData('entity_reference_selection_handler', $this);
// Add the sort option.
if (!empty($this->instance['settings']['handler_settings']['sort'])) {
$sort_settings = $this->instance['settings']['handler_settings']['sort'];
if ($sort_settings['field'] != '_none') {
$query
->sort($sort_settings['field'], $sort_settings['direction']);
}
}
return $query;
}