From d437a493b70ef56d8aebb18885b5bf345dad1719 Mon Sep 17 00:00:00 2001 From: Diego Pino Navarro Date: Mon, 27 Mar 2023 16:21:21 -0300 Subject: [PATCH 1/2] Adds ap:tasks title source option MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit e.g ```JSON { "type": "DataSet", "label": "Title from metadata", "label_spa": "Título desde metadatos", "ap:tasks" { "ap:entitytitle": "label_spa" } } ``` Will yield in an ADO/NODE with title "Título desde metadatos" In case of a wrong JMESPATH expression the original title will be preserved (if existing) or the default label key will be used. --- .gitignore | 1 + ...tPresaveSubscriberSetTitlefromMetadata.php | 54 +++++++++++++++---- 2 files changed, 46 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 3ca8fba..dbe512d 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,4 @@ .idea/symfony2.xml src/.DS_Store src/Plugin/.DS_Store +.DS_Store diff --git a/src/EventSubscriber/StrawberryfieldEventPresaveSubscriberSetTitlefromMetadata.php b/src/EventSubscriber/StrawberryfieldEventPresaveSubscriberSetTitlefromMetadata.php index 072916e..882bc71 100644 --- a/src/EventSubscriber/StrawberryfieldEventPresaveSubscriberSetTitlefromMetadata.php +++ b/src/EventSubscriber/StrawberryfieldEventPresaveSubscriberSetTitlefromMetadata.php @@ -103,17 +103,53 @@ public function onEntityPresave(StrawberryfieldCrudEvent $event) { /* @var \Drupal\strawberryfield\Field\StrawberryFieldItemList $field */ // This will try with any possible match. foreach ($field->getIterator() as $delta => $itemfield) { + /** @var \Drupal\strawberryfield\Plugin\Field\FieldType\StrawberryFieldItem $itemfield */ $full = $itemfield->provideDecoded(TRUE); - $labelsource = isset($full['label']) ? $full : $itemfield->provideFlatten(); - // @TODO Should we allow which JSON key sets the label a setting? - if (isset($labelsource['label'])) { - // Flattener should always give me an array? - if (is_array($labelsource['label'])) { - $title = reset($labelsource['label']); + /* check if ap:tasks for custom label to title is set */ + $jmespath_for_label = $full['ap:tasks']['ap:entitytitle'] ?? FALSE; + $title = NULL; + if ($jmespath_for_label) { + try { + // Evaluate the JMESPATH. + $title = $itemfield->searchPath($jmespath_for_label); + $title = is_array($title) ? reset($title) : $title; + if (!is_scalar($title) || strlen(trim($title)) == 0) { + $title = NULL; + } + } + catch (\Exception $exception) { + $this->loggerFactory->get('strawberryfield')->error($this->t('Error Setting Entity Title for Node ID @node via "ap:tasks"."ap:entitytitle": invalid JMESPath expression @jmespath', [ + '@node' => $entity->id(), + '@jmespath' => $jmespath_for_label, + ])); + // WE don't want to stop ingesting so we catch and + // continue + if (!empty($entity->label())) { + $title = $entity->label(); + } + else { + $title = NULL; + } } - else { - $title = $labelsource['label']; + } + + if (!$title) { + // Always default to original label in case the JMESPATH expression failed. + $labelsource = isset($full['label']) ? $full : $itemfield->provideFlatten(); + // @TODO Should we allow which JSON key sets the label a setting? + if (isset($labelsource['label'])) { + // Flattener should always give me an array? + if (is_array($labelsource['label'])) { + $title = reset($labelsource['label']); + } + else { + $title = $labelsource['label']; + } } + } + + if ($title && is_scalar($title)) { + // Flattener should always give me an array? if (strlen(trim($title)) > 0) { $title = Unicode::truncate($title, 128, TRUE, TRUE, 24); // we could check if originallabel != from the new title @@ -126,7 +162,7 @@ public function onEntityPresave(StrawberryfieldCrudEvent $event) { } // If at this stage we have no entity label, but maybe its just not in our metadata, - // or someone forget to set a label key. + // or someone forget to set a label key and the jmespath failed. if (!$entity->label()) { // Means we need a title, got nothing from metadata or node, dealing with it. $title = $this->t( From 0622adfd99f6b154c520de7f799acf08a263d8af Mon Sep 17 00:00:00 2001 From: Diego Pino Navarro Date: Mon, 27 Mar 2023 16:33:11 -0300 Subject: [PATCH 2/2] Small updates on inline comments --- ...erryfieldEventPresaveSubscriberSetTitlefromMetadata.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/EventSubscriber/StrawberryfieldEventPresaveSubscriberSetTitlefromMetadata.php b/src/EventSubscriber/StrawberryfieldEventPresaveSubscriberSetTitlefromMetadata.php index 882bc71..e4379ea 100644 --- a/src/EventSubscriber/StrawberryfieldEventPresaveSubscriberSetTitlefromMetadata.php +++ b/src/EventSubscriber/StrawberryfieldEventPresaveSubscriberSetTitlefromMetadata.php @@ -110,7 +110,7 @@ public function onEntityPresave(StrawberryfieldCrudEvent $event) { $title = NULL; if ($jmespath_for_label) { try { - // Evaluate the JMESPATH. + // Evaluate the JMESPath Expression. $title = $itemfield->searchPath($jmespath_for_label); $title = is_array($title) ? reset($title) : $title; if (!is_scalar($title) || strlen(trim($title)) == 0) { @@ -134,7 +134,8 @@ public function onEntityPresave(StrawberryfieldCrudEvent $event) { } if (!$title) { - // Always default to original label in case the JMESPATH expression failed. + // Defaults to the original label key in case the JMESPATH expression failed. + // But will preserve the NODE Title if that was present during the failure $labelsource = isset($full['label']) ? $full : $itemfield->provideFlatten(); // @TODO Should we allow which JSON key sets the label a setting? if (isset($labelsource['label'])) { @@ -162,7 +163,7 @@ public function onEntityPresave(StrawberryfieldCrudEvent $event) { } // If at this stage we have no entity label, but maybe its just not in our metadata, - // or someone forget to set a label key and the jmespath failed. + // or someone forget to set a label key and/or the an `ap:tasks` set jmespath failed. if (!$entity->label()) { // Means we need a title, got nothing from metadata or node, dealing with it. $title = $this->t(