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 {