Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ISSUE-261: Adds ap:tasks title source option #262

Merged
merged 2 commits into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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