Skip to content
Snippets Groups Projects

Issue #3229479: Entity_lookup taxonomy_term restricted by VID

2 files
+ 107
21
Compare changes
  • Side-by-side
  • Inline
Files
2
  • dae1881a
    Issue #3121204 by Matroskeen, rodrigoaguilera: Add ability to choose the... · dae1881a
    Ivan Doroshenko authored
    Issue #3121204 by Matroskeen, rodrigoaguilera: Add ability to choose the operator when performing an entity lookup
@@ -32,13 +32,17 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
* - entity_reference: The entity label key.
* - file: The uri field.
* - image: The uri field.
* - operator: (optional) The comparison operator supported by entity query:
* See \Drupal\Core\Entity\Query\QueryInterface::condition() for available
* values. Defaults to '=' for scalar values and 'IN' for arrays.
* - bundle_key: (optional) The name of the bundle field on the entity type
* being queried.
* - bundle: (optional) The value to query for the bundle.
* - access_check: (optional) Indicates if access to the entity for this user
* will be checked. Default is true.
* - ignore_case: (optional) Whether to ignore case in the query. Defaults to
* true.
* false, meaning the query is case-sensitive by default. Works only with
* strict operators: '=' and 'IN'.
* - destination_field: (optional) If specified, and if the plugin's source
* value is an array, the result array's items will be themselves arrays of
* the form [destination_field => ENTITY_ID].
@@ -70,10 +74,13 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
* bundle: tags
* entity_type: taxonomy_term
* ignore_case: true
* operator: STARTS_WITH
* @endcode
*
* @codingStandardsIgnoreEnd
*
* @see \Drupal\Core\Entity\Query\QueryInterface::condition()
*
* @MigrateProcessPlugin(
* id = "entity_lookup",
* handle_multiples = TRUE
@@ -220,17 +227,22 @@ class EntityLookup extends ProcessPluginBase implements ContainerFactoryPluginIn
*/
protected function query($value) {
// Entity queries typically are case-insensitive. Therefore, we need to
// handle case sensitive filtering as a post-query step. By default, it
// filters case insensitive. Change to true if that is not the desired
// handle case-sensitive filtering as a post-query step. By default, it
// filters case-insensitive. Change to true if that is not the desired
// outcome.
$ignoreCase = !empty($this->configuration['ignore_case']) ?: FALSE;
$operator = !empty($this->configuration['operator']) ? $this->configuration['operator'] : '=';
$multiple = is_array($value);
// Apply correct operator for multiple values.
if ($multiple && $operator === '=') {
$operator = 'IN';
}
$query = $this->entityTypeManager->getStorage($this->lookupEntityType)
->getQuery()
->accessCheck($this->accessCheck)
->condition($this->lookupValueKey, $value, $multiple ? 'IN' : NULL);
->condition($this->lookupValueKey, $value, $operator);
// Sqlite and possibly others returns data in a non-deterministic order.
// Make it deterministic.
if ($multiple) {
@@ -246,8 +258,8 @@ class EntityLookup extends ProcessPluginBase implements ContainerFactoryPluginIn
return NULL;
}
// By default do a case-sensitive comparison.
if (!$ignoreCase) {
// Do a case-sensitive comparison only for strict operators.
if (!$ignoreCase && in_array($operator, ['=', 'IN'], TRUE)) {
// Returns the entity's identifier.
foreach ($results as $k => $identifier) {
$entity = $this->entityTypeManager->getStorage($this->lookupEntityType)->load($identifier);
Loading