| Conditions | 7 | 
| Paths | 20 | 
| Total Lines | 34 | 
| Code Lines | 20 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 3 | ||
| Bugs | 0 | Features | 0 | 
| 1 | <?php | ||
| 13 |   public function expand($values) { | ||
| 14 | $return = array(); | ||
| 15 |     $entity_type_id = $this->fieldInfo->getSetting('target_type'); | ||
| 16 | $entity_definition = \Drupal::entityManager()->getDefinition($entity_type_id); | ||
| 17 | |||
| 18 | // Determine label field key. | ||
| 19 |     if ($entity_type_id !== 'user') { | ||
| 20 |       $label_key = $entity_definition->getKey('label'); | ||
| 21 | } | ||
| 22 |     else { | ||
| 23 |       // Entity Definition->getKey('label') returns false for users. | ||
| 24 | $label_key = 'name'; | ||
| 25 | } | ||
| 26 | |||
| 27 | // Determine target bundle restrictions. | ||
| 28 | $target_bundle_key = NULL; | ||
| 29 |     if ($target_bundles = $this->getTargetBundles()) { | ||
| 30 |       $target_bundle_key = $entity_definition->getKey('bundle'); | ||
| 31 | } | ||
| 32 | |||
| 33 |     foreach ($values as $value) { | ||
| 34 | $query = \Drupal::entityQuery($entity_type_id)->condition($label_key, $value); | ||
| 35 |       if ($target_bundles && $target_bundle_key) { | ||
| 36 | $query->condition($target_bundle_key, $target_bundles, 'IN'); | ||
| 37 | } | ||
| 38 |       if ($entities = $query->execute()) { | ||
| 39 | $return[] = array_shift($entities); | ||
| 40 | } | ||
| 41 |       else { | ||
| 42 |         throw new \Exception(sprintf("No entity '%s' of type '%s' exists.", $value, $entity_type_id)); | ||
| 43 | } | ||
| 44 | } | ||
| 45 | return $return; | ||
| 46 | } | ||
| 47 | |||
| 62 |