From 0efc41f121fc74ec5289e0092d0d10ec67131777 Mon Sep 17 00:00:00 2001 From: bach Date: Sun, 6 Sep 2026 11:11:37 +0200 Subject: [PATCH] Create a new Client term on the fly instead of rejecting unknown names updateField()'s client branch previously rejected any name that didn't match an existing "Client" term, on the assumption the front-end's datalist restricted input to known names -- it doesn't, it only suggests them, so this blocked adding a genuinely new client from the inline edit even though the content type itself allows it. Now creates the term (same "autocreate" behavior as a standard Drupal entity reference autocomplete widget) rather than erroring. Frontend also appends the newly created name to allClientsList so it shows up in the filter dropdown/datalist immediately, not just after a reload picks it up via fetchClientNames(). Verified live: typed a brand-new client name inline, save succeeded, confirmed the taxonomy term was actually created in the database and the filter datalist updated immediately -- then reverted the test node and deleted the test term. --- .../custom/figli_compta_ledger/js/home.js | 7 +++++++ .../Controller/LedgerActionsController.php | 19 ++++++++++++------- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/web/modules/custom/figli_compta_ledger/js/home.js b/web/modules/custom/figli_compta_ledger/js/home.js index c194392..4fef4b2 100644 --- a/web/modules/custom/figli_compta_ledger/js/home.js +++ b/web/modules/custom/figli_compta_ledger/js/home.js @@ -816,6 +816,13 @@ const result = await updateLigneField(item.nid, field, newValue); const row = this.rows.find((r) => r.id === item.id); if (row) row[field] = result.value; + // A client name with no existing match gets created on the fly + // (server-side) rather than rejected -- reflect it in the + // filter dropdown/datalist immediately instead of only after a + // reload picks up the new taxonomy term via fetchClientNames(). + if (field === 'client' && result.value && !this.allClientsList.includes(result.value)) { + this.allClientsList = [...this.allClientsList, result.value].sort(); + } } catch (err) { this.typeUpdateError = err.message; } diff --git a/web/modules/custom/figli_compta_ledger/src/Controller/LedgerActionsController.php b/web/modules/custom/figli_compta_ledger/src/Controller/LedgerActionsController.php index 9c3c406..3047ed3 100644 --- a/web/modules/custom/figli_compta_ledger/src/Controller/LedgerActionsController.php +++ b/web/modules/custom/figli_compta_ledger/src/Controller/LedgerActionsController.php @@ -6,6 +6,7 @@ use Drupal\Core\Access\CsrfRequestHeaderAccessCheck; use Drupal\Core\Controller\ControllerBase; use Drupal\Core\Entity\EntityStorageException; use Drupal\node\NodeInterface; +use Drupal\taxonomy\Entity\Term; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; @@ -137,20 +138,24 @@ class LedgerActionsController extends ControllerBase { $fieldName = self::INLINE_EDITABLE_FIELDS[$field]; if ($field === 'client') { - // Only an existing "Client" term is accepted -- the front-end - // offers this as a datalist of known names, not free text, so a - // non-match almost certainly means a typo rather than a genuinely - // new client that should be created on the fly. if ($value === '') { $node->set('field_client', NULL); } else { $terms = $this->entityTypeManager()->getStorage('taxonomy_term') ->loadByProperties(['vid' => 'client', 'name' => $value]); - if (!$terms) { - return new JsonResponse(['error' => 'Client inconnu : "' . $value . '". Utilisez un nom existant dans la liste.'], 400); + if ($terms) { + $node->set('field_client', reset($terms)->id()); + } + else { + // No existing term matches -- create one rather than reject, + // same "autocreate" behavior as a standard Drupal entity + // reference autocomplete widget. The front-end's datalist only + // *suggests* known names, it doesn't restrict input to them. + $term = Term::create(['vid' => 'client', 'name' => $value]); + $term->save(); + $node->set('field_client', $term->id()); } - $node->set('field_client', reset($terms)->id()); } } else {