Problem/Motivation

The Allow new terms widget setting applies to every role, so there is no way to say that one role may add terms while another may only pick from the ones that exist.

Steps to reproduce

  1. Add a term reference field with the Autocomplete Deluxe widget and turn Allow new terms on.
  2. Give a role the right to create the content, but not the right to create terms in that vocabulary.
  3. A user in that role is still offered the new term, and it is created on save.

Proposed resolution

No new permission of our own. The widget asks the access handler of the referenced entity type whether the current user may create an entity in the autocreate bundle, which for taxonomy is the core create terms in vocabulary permission, and which any module that alters entity access can answer as well. New terms are offered only when the setting is on, an autocreate bundle is configured and that check passes.

A user without the create terms permission is not offered the new term

Remaining tasks

  • ✅ File an issue
  • ✅ Addition/Change/Update/Fix
  • ✅ Testing to ensure no regression
  • ➖ Automated unit testing coverage
  • ➖ Automated functional testing coverage
  • ➖ UX/UI designer responsibilities
  • ➖ Readability
  • ➖ Accessibility
  • ➖ Performance
  • ✅ Security
  • ➖ Developer Documentation
  • ➖ User Guide Documentation
  • ➖ Reviewed by human
  • ➖ Code review by maintainers
  • ➖ Full testing and approval
  • ➖ Credit contributors
  • ➖ Review with the product owner
  • ➖ Release notes snippet
  • ❌ Release

User interface changes

  • A user without the create permission of the referenced entity type is no longer offered new terms.

API changes

  • The widget takes the entity type manager as one more constructor argument, which defaults to the service when it is not passed.

Data model changes

  • N/A

Release notes snippet

  • Autocomplete Deluxe now offers new terms only to a user who is allowed to create them, following the create permission of the referenced entity type.
Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

nkoporec created an issue. See original summary.

rajab natshah’s picture

Version: 8.x-1.x-dev » 2.0.x-dev
Assigned: Unassigned » mohammed j. razem
rajab natshah’s picture

Assigned: mohammed j. razem » Unassigned
bassplaya’s picture

Is this available for Drupal 7 as well?
If you have it, please provide.
Much obliged.

mohammad-fayoumi’s picture

Status: Needs review » Reviewed & tested by the community

Thanks, @nkoporec the patch works fine.

rajab natshah’s picture

Status: Reviewed & tested by the community » Needs work

The patch needs to be updated to latest 2.0.x branch

rajab natshah’s picture

daveiano’s picture

I think that this is the wrong direction. We should not introduce new permissions, we should use the available core permissions, e.g. for taxonomy:

create terms in [vocabulary]

So the widget should check which is the target bundle and type and then if the user has the correct permission to create new entities of this specific tape + bundle.

tostinni made their first commit to this issue’s fork.

tostinni’s picture

Status: Needs work » Needs review

Following comment from @daveiano adding the test if the user can add new terms to the current vocabulary.

rajab natshah’s picture

Title: Create a permission that allows users to create new terms. » Add a new permission that allows users to create terms in (bundle) terms
cyberwolf’s picture

StatusFileSize
new2.01 KB

Attaching a patch that properly uses the entity access handler (so the existing permissions in core, or altered by modules in the proper way).

cyberwolf’s picture

StatusFileSize
new2.01 KB

Updated patch that properly uses the field's target type instead of a hardcoded 'taxonomy_term'.

rajab natshah’s picture

Version: 2.0.x-dev » 2.1.x-dev
Assigned: Unassigned » rajab natshah
Issue summary: View changes
StatusFileSize
new42.64 KB

Thank you, Nejc for reporting and for the first patch, David for pointing at the core permissions, Nicolas for the earlier merge request, and cyberwolf for the patch that uses the entity access handler.

Went with the access handler rather than a permission of our own, so the core create terms in vocabulary permission decides, and any module that alters entity access is respected. The new merge request also carries the entity type manager into the widget instead of calling the service statically.

Tested on Drupal 11.4 with an unlimited term reference field and a role that may create the content but not create terms in that vocabulary: the widget stops offering the new term and nothing is created. Granting that one permission to the same role brings the behaviour back, and the node saves with the new term.

A user without the create terms permission is not offered the new term

To review, test, then merge.

rajab natshah’s picture

MR !34 needed rebasing before it could safely go in, and that is now done. Pipeline is green.

Why it needed it. The branch was cut on 28 August and 2.1.x has moved fourteen commits since. Its diff against the branch tip was deleting seven feature files, reverting the stylesheet build from PostCSS back to gulp and SASS, and removing settings including show entity ID, the parent hierarchy and the selection handler override. Merging it would have quietly undone #3181219, #3263472, #3100411, #3226157, #3314294, #3307367, #3056545, #3535541, #3176316, #3298376, #3152123, #3268207, #3534094 and #3061562.

CI was green throughout, because the branch was perfectly consistent with itself. It was just eight days behind. Worth remembering that a green pipeline says nothing about what a merge would remove.

What I did. Rebased onto current 2.1.x. Six conflicts, all the same shape: the branch tip injects the selection plugin manager and this work injects the entity type manager, and both are wanted, so both are kept. On the second one I took your approach of requiring the injected service rather than my earlier fallback, since yours is cleaner.

The diff is now four files and ninety five lines: the access check, its feature file, the tagger role and the cucumber user. Nothing else.

Checked on Drupal 11.4.5. As a user who may create articles but has no create terms permission, the widget stops offering to add a term and the list simply reads "No results found". Existing terms are still selectable. As an administrator the offer is still there.

That last part is also exactly what #3465063 and #3568757 report, so this closes all three.

Prepared with AI assistance (Claude), reviewed and driven by the maintainer.

  • rajab natshah committed b08ed199 on 2.1.x
    feat: #3112680 Offer new terms only to a user who may create them
    
rajab natshah’s picture

Assigned: rajab natshah » Unassigned
Status: Needs review » Fixed

Merged into 2.1.x as b08ed19. Thank you tostinni for the original patch and for the long wait on this one.

The widget now asks the access handler of the referenced entity type whether this user may create one, before it offers to add anything new. Where the answer is no, the suggestion list simply reports no results instead of promising a value the save would refuse.

Rather than adding a permission of its own, it follows the core permissions already there, so "create terms in [vocabulary]" and anything a contrib module does to entity access are both respected without configuring the same thing twice.

This also settles #3465063 and #3568757, which were the same fault seen from the content side and the taxonomy side.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.