Skip to content

Commit

Permalink
Merge pull request #262 from esmero/ISSUE-261
Browse files Browse the repository at this point in the history
ISSUE-261: Adds ap:tasks title source option
  • Loading branch information
DiegoPino authored Mar 27, 2023
2 parents 78f4bdb + 0622adf commit acf4bc9
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@
.idea/symfony2.xml
src/.DS_Store
src/Plugin/.DS_Store
.DS_Store
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,54 @@ 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 Expression.
$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 <em>@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) {
// 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'])) {
// 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
Expand All @@ -126,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.
// 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(
Expand Down

0 comments on commit acf4bc9

Please sign in to comment.