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.
This commit is contained in:
@@ -816,6 +816,13 @@
|
|||||||
const result = await updateLigneField(item.nid, field, newValue);
|
const result = await updateLigneField(item.nid, field, newValue);
|
||||||
const row = this.rows.find((r) => r.id === item.id);
|
const row = this.rows.find((r) => r.id === item.id);
|
||||||
if (row) row[field] = result.value;
|
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) {
|
} catch (err) {
|
||||||
this.typeUpdateError = err.message;
|
this.typeUpdateError = err.message;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ use Drupal\Core\Access\CsrfRequestHeaderAccessCheck;
|
|||||||
use Drupal\Core\Controller\ControllerBase;
|
use Drupal\Core\Controller\ControllerBase;
|
||||||
use Drupal\Core\Entity\EntityStorageException;
|
use Drupal\Core\Entity\EntityStorageException;
|
||||||
use Drupal\node\NodeInterface;
|
use Drupal\node\NodeInterface;
|
||||||
|
use Drupal\taxonomy\Entity\Term;
|
||||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
|
||||||
@@ -137,20 +138,24 @@ class LedgerActionsController extends ControllerBase {
|
|||||||
$fieldName = self::INLINE_EDITABLE_FIELDS[$field];
|
$fieldName = self::INLINE_EDITABLE_FIELDS[$field];
|
||||||
|
|
||||||
if ($field === 'client') {
|
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 === '') {
|
if ($value === '') {
|
||||||
$node->set('field_client', NULL);
|
$node->set('field_client', NULL);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$terms = $this->entityTypeManager()->getStorage('taxonomy_term')
|
$terms = $this->entityTypeManager()->getStorage('taxonomy_term')
|
||||||
->loadByProperties(['vid' => 'client', 'name' => $value]);
|
->loadByProperties(['vid' => 'client', 'name' => $value]);
|
||||||
if (!$terms) {
|
if ($terms) {
|
||||||
return new JsonResponse(['error' => 'Client inconnu : "' . $value . '". Utilisez un nom existant dans la liste.'], 400);
|
$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 {
|
else {
|
||||||
|
|||||||
Reference in New Issue
Block a user