From 0e36274d51945f1aecfcb516e0bfdcb4bc84cc9f Mon Sep 17 00:00:00 2001 From: bach Date: Fri, 4 Sep 2026 12:33:08 +0200 Subject: [PATCH] =?UTF-8?q?Show=20montant=20HT=20+=20notes=20in=20the=20en?= =?UTF-8?q?tr=C3=A9e=20client=20autocomplete?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .../EntreeClientSelection.php | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/web/modules/custom/figli_compta_ledger/src/Plugin/EntityReferenceSelection/EntreeClientSelection.php b/web/modules/custom/figli_compta_ledger/src/Plugin/EntityReferenceSelection/EntreeClientSelection.php index 88e773f..f6bdd6e 100644 --- a/web/modules/custom/figli_compta_ledger/src/Plugin/EntityReferenceSelection/EntreeClientSelection.php +++ b/web/modules/custom/figli_compta_ledger/src/Plugin/EntityReferenceSelection/EntreeClientSelection.php @@ -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; + } + }