1

I need a specific vocabulary (A) that has unique terms. I don't want to do validation at form level because I want to be able to add term from everywhere.

I successful used hook_entity_bundle_field_info_alter() and addConstraint() to be sure one specific field is unique on another vocabulary (B). (Will this be deprecated? See https://www.drupal.org/node/2346347.)

For the vocabulary (A), there is no specific field, so the hook_entity_bundle_field_info_alter() is not working here, as this field name is not a "true" field.

I use hook_entity_presave(), but I'm not able to do a proper validation because this hook is called too late in the code, except if I use Exception, but that is not a good way.

How can I achieve what I am trying to do?

0

That hook wont' go away and its fine to use it. That warning was added a while ago, it's not possible to change it anymore in 8.x AFAIK. We hoped to do that before the stable release.

Name is a true field but it is a base field. You should be able to get the base field definitions from the entity type manager if it is not there, clone it and add your constraint.

  • Great news ! I read some things about what you said (cloning, etc) but can you be more precise ? Or maybe an example ? I don't know what hook to use / or what class to use / surdefine Thanks – rroblik Feb 8 '16 at 10:00
  • Look at Comment::bundleFieldDefinitions(), you have to load the base field definitions yourself but otherwise it's similar. – Berdir Feb 8 '16 at 10:44
  • I looked about that... I want a specific vocabulary taxonomy term (vocabulary created trough UI) be unique. So where should I clone $base_field_definitions to addConstraint on it ? I tried to extends Drupal\taxonomy\Entity\Term class in my custom module but... Seems to have no effect :/ – rroblik Feb 9 '16 at 9:05
  • As I said, don't clone, that was just an example. Use the API of the entity field manager to get the base field definitions and then clone the field from those and put it in the bundle definitions. If the vocubulary name needs to be dynamic, you need to store some configuration somewhere about the name, for custom code, you could just hardcode it. – Berdir Feb 9 '16 at 9:29
  • Duh ... I "see" what you say but... Drupal 8 make me feel like a dumb developper ! I don't understand where (in what way, hook, class, ...) and how use the Entity API :x (I can hardcode it to begin !). – rroblik Feb 9 '16 at 10:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.