1

I need to manage a few different types of objects, let's say "car", "pickup", and "van". These are similar enough (for my purposes) that I can define a single content type and associated code for "vehicle" and use it for all of them, along with a field that specifies the exact type of the object. This allows me to do things like have one page that shows all the cars, one page that shows all the pickups, and so on. I have this code running; everything is fine.

I now want to add an OPTIONAL tag field to these items, using the taxonomy / term_reference stuff in Drupal. The catch is that I want to have a separate tag taxonomy for each of my different kinds of vehicles, so that "car" tags don't show up in the truck's autocomplete suggestions. (Trust me -- in the context of my app, this makes sense.) The question is how to do it; the complicating factor is that the vocabulary for a tag field has to be defined when the field is added to the content type, and, remember, my content type is generic across all the types of vehicles.

So:

  • Ugly possibility #1: Define a single tag field -- field_vehicle_tags? -- in the content type and write some magical code in a form_alter or theme or after_build hook or somesuch that, when building the edit form for the node, checks to see what kind of vehicle is being created (this is known upon entry to the form) and bashes the appropriate vocabulary into the field's autocomplete process. I don't know that this is actually possible, and I suspect the opportunities for screwing myself are endless.

  • Ugly possibility #2: Define one tag field for each kind of vehicle -- field_car_tags, field_pickup_tags, etc -- each of which is connected to its own taxonomy. Then, use the theming hook to throw away (= not render as part of the form) all the tag fields except for the one that matches the kind of thing being edited. This is stupid and wasteful in all sorts of ways, but I'm pretty sure it would work.

Any thoughts or alternatives? I'm currently leaning towards #2, but if there's a better alternative, I'd love to hear about it.

2

You can create a Content Type named Vehicle and make inherits his fields to your Cars, Van with the help of the Bundle Inherit module. Then you can create a Taxonomy for each kind of Vehicle then you will have.

Vehicle (color, number_of_seats, etc)
 - Car (field_car_tags)
 - Van (field_van_tags)

With this you don't need to write lines of code and you can reuse the fields of the Vehicle Content Type.

  • @JimMiller a pleasure help you, let me know the result ;-) – Adrian Cid Almaguer Jan 26 '16 at 6:19
  • @JimMiller you got some advance? – Adrian Cid Almaguer Jan 29 '16 at 10:45
  • Not so far -- a few other things have gotten in the way. But soon, I hope... – Jim Miller Jan 29 '16 at 17:19
  • 1
    @JimMiller If you solve your problem please post the answer – Adrian Cid Almaguer Jan 29 '16 at 17:30
  • @JimMiller the problem is solved? – Adrian Cid Almaguer Feb 10 '16 at 7:13

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.