Show montant HT + notes in the entrée client autocomplete

Several entrées for the same client often share the same title
format ("EPAU - 2026-01-05"), which isn't enough to tell them apart
when linking a sortie. Append the amount and the notes/invoice
reference to each suggestion's label -- verified end to end (real
autocomplete HTTP request, then an actual save) that the richer label
still parses back to the right node id.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-09-04 12:33:08 +02:00
co-authored by Claude Sonnet 5
parent d04d0e8824
commit 0e36274d51
@@ -2,6 +2,7 @@
namespace Drupal\figli_compta_ledger\Plugin\EntityReferenceSelection;
use Drupal\Component\Utility\Html;
use Drupal\Core\Entity\Attribute\EntityReferenceSelection;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\node\Plugin\EntityReferenceSelection\NodeSelection;
@@ -35,4 +36,46 @@ class EntreeClientSelection extends NodeSelection {
return $query;
}
/**
* {@inheritdoc}
*
* Appends montant HT and the notes/détail text to the label -- several
* entrées for the same client often share the same title format ("EPAU -
* 2026-01-05"), so the amount and invoice reference are what actually let
* someone tell them apart when linking a sortie.
*/
public function getReferenceableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) {
$options = parent::getReferenceableEntities($match, $match_operator, $limit);
if (empty($options)) {
return $options;
}
$ids = [];
foreach ($options as $items) {
$ids += array_keys($items);
}
$entities = $this->entityTypeManager->getStorage('node')->loadMultiple($ids);
foreach ($options as $bundle => &$items) {
foreach ($items as $id => $label) {
$entity = $entities[$id] ?? NULL;
if (!$entity) {
continue;
}
$details = [];
if ($entity->hasField('field_montant_ht') && !$entity->get('field_montant_ht')->isEmpty()) {
$details[] = number_format((float) $entity->get('field_montant_ht')->value, 2, ',', ' ') . ' €';
}
if ($entity->hasField('field_notes') && !$entity->get('field_notes')->isEmpty()) {
$details[] = Html::escape($entity->get('field_notes')->value);
}
if ($details) {
$items[$id] = $label . ' — ' . implode(' — ', $details);
}
}
}
return $options;
}
}