From 357822e66d0edf2a04f302e898cb22c10717abad Mon Sep 17 00:00:00 2001 From: Seth Shaw Date: Mon, 15 Aug 2022 15:25:42 -0700 Subject: [PATCH 1/3] add optional pass_target_id argument --- src/EntityReferenceConverter.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/EntityReferenceConverter.php b/src/EntityReferenceConverter.php index 4b75697..2c78460 100644 --- a/src/EntityReferenceConverter.php +++ b/src/EntityReferenceConverter.php @@ -18,6 +18,7 @@ class EntityReferenceConverter { * An array of arguments defined in the mapping. * Expected keys are: * - link_field: The field used to store the URI we will use. + * - pass_target_id: (true/false) If it should return a target ID when no term is found. * * @return mixed * Either the replaced URI string OR the targeted entity if no URI. @@ -35,9 +36,12 @@ public static function linkFieldPassthrough($target, array $arguments) { elseif ($ent) { return $ent->get('name')->value; } + elseif (array_key_exists('pass_target_id', $arguments) && $arguments['pass_target_id']) { + return $target['target_id']; + } } - // We don't have a value to pass, so don't bother converting. - return $target; + // Nothing worked, so return nothing. + return ''; } } From c3fd8cf311ab94f495c0a8ca802a2139938188da Mon Sep 17 00:00:00 2001 From: Seth Shaw Date: Mon, 15 Aug 2022 15:49:49 -0700 Subject: [PATCH 2/3] style --- src/EntityReferenceConverter.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/EntityReferenceConverter.php b/src/EntityReferenceConverter.php index 2c78460..9166b18 100644 --- a/src/EntityReferenceConverter.php +++ b/src/EntityReferenceConverter.php @@ -18,7 +18,8 @@ class EntityReferenceConverter { * An array of arguments defined in the mapping. * Expected keys are: * - link_field: The field used to store the URI we will use. - * - pass_target_id: (true/false) If it should return a target ID when no term is found. + * - pass_target_id: (true/false) If it should return a target ID + * when no term is found. * * @return mixed * Either the replaced URI string OR the targeted entity if no URI. From 5932a7900a054ed7a89423811afb11cf71caa9c0 Mon Sep 17 00:00:00 2001 From: Seth Shaw Date: Mon, 15 Aug 2022 16:32:22 -0700 Subject: [PATCH 3/3] Clarify docs; add label when passed entity --- src/EntityReferenceConverter.php | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/EntityReferenceConverter.php b/src/EntityReferenceConverter.php index 9166b18..cb8f32c 100644 --- a/src/EntityReferenceConverter.php +++ b/src/EntityReferenceConverter.php @@ -18,15 +18,24 @@ class EntityReferenceConverter { * An array of arguments defined in the mapping. * Expected keys are: * - link_field: The field used to store the URI we will use. - * - pass_target_id: (true/false) If it should return a target ID - * when no term is found. + * - pass_target_id: (true/false) should we return a target ID + * when no term is found? * * @return mixed - * Either the replaced URI string OR the targeted entity if no URI. + * In order of availability: + * - the replaced URI string, + * - the target entity's label, + * - (if pass_target_id is true) the target entity's ID, + * - or an empty string. */ public static function linkFieldPassthrough($target, array $arguments) { - if (is_a($target, 'Drupal\Core\Entity\FieldableEntityInterface') && !empty($target->get($arguments['link_field'])->uri)) { - return $target->get($arguments['link_field'])->uri; + if (is_a($target, 'Drupal\Core\Entity\FieldableEntityInterface')) { + if (empty($target->get($arguments['link_field'])->uri)) { + return $target->label(); + } + else { + return $target->get($arguments['link_field'])->uri; + } } if (is_array($target) && array_key_exists('target_id', $target)) {