From 91e3ade4c81d44dce0a515b4e5d04fcb743b7c9c Mon Sep 17 00:00:00 2001 From: Julien Dubois Date: Wed, 31 May 2017 11:54:31 +0200 Subject: [PATCH] Use ids instead of names + support auto creates If we reference entities like medias, names are useless. Relying on entity ids is more logical. We also need to support auto created entities like taxonomy terms. If the term is not known, do not throw the exception. --- .../Driver/Fields/Drupal8/EntityReferenceHandler.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Drupal/Driver/Fields/Drupal8/EntityReferenceHandler.php b/src/Drupal/Driver/Fields/Drupal8/EntityReferenceHandler.php index 0552b6fe..143cdef0 100644 --- a/src/Drupal/Driver/Fields/Drupal8/EntityReferenceHandler.php +++ b/src/Drupal/Driver/Fields/Drupal8/EntityReferenceHandler.php @@ -14,7 +14,7 @@ public function expand($values) { $return = array(); $entity_type_id = $this->fieldInfo->getSetting('target_type'); $entity_definition = \Drupal::entityManager()->getDefinition($entity_type_id); - $label_key = $entity_definition->getKey('label'); + $label_key = $entity_definition->getKey('id'); // Determine target bundle restrictions. $target_bundle_key = NULL; @@ -31,7 +31,10 @@ public function expand($values) { $return[] = array_shift($entities); } else { - throw new \Exception(sprintf("No entity '%s' of type '%s' exists.", $value, $entity_type_id)); + // Do not throw an exception if the field has auto created items. + if (!$this->fieldConfig->getSetting('handler_settings')['auto_create']) { + throw new \Exception(sprintf("No entity '%s' of type '%s' exists.", $value, $entity_type_id)); + } } } return $return;