1

I was wondering how would you guys approach a situation where your Drupal7 site required a couple of custom content type, yet most of the fields would be shared across them. What I mean resembles a typical object-oriented case of a super class and its subclasses.

I want it so that I can make the admin-side content filling easier for the owner of the site. I also assume that for me, as the author of the form it would be a great time-saver if I could base one on another.

Is it possible? Does it make sense? Or would you rather make one type with all possible fields merged onto one form, then tweak the suggestion template so that irrelevant parts are /shown/hidden upon changing a dropdown menu that specifies the subclass?

Anyone been through a similar problem?

1
1

Or would you rather make one type with all possible fields merged onto one form, then tweak the suggestion template so that irrelevant parts are /shown/hidden upon changing a dropdown menu that specifies the subclass?

If you're going the single-type route, here are two tips: Drupal Forms API supports conditions, check the docs; it might be useful to take a look at the Prepopulate module.

2
  • Prepopulate and conditions don't really answer data structure inheritance. Not quite a -1 because you could hack your way into mimicking some of the goal. – Grayside Apr 10 '12 at 16:43
  • Did I make myself clearer now? It's not about data inheritance between the different content types, it's about using a single content-type and hiding fields. Of course, it will also need some #validation handlers to ignore unused field values. And maybe Display Suite to handle different node views. – Capi Etheriel Apr 12 '12 at 1:29
0

There's no way to use actual object-oriented inheritance. However, you can manage the field configurations in such a way that you attach them to multiple content types based on some flag--typically a content type-specific variable.

Using the Features module's Field export hooks (namely, hook_field_default_fields() and hook_field_default_fields_alter()), you can programmatically define fields for multiple types. In fact, once this issue lands, you can have a central definition for a field, then separate instance definitions to attach and configure that field for each content type.

For form changes in general, you could capture multiple node types in the same form alter logic to create common tweaks to certain nodes. In both cases, you might end up using code such as

if (variable_get($node->type . '_is_a_shape', FALSE)) {
  // Apply changes to form/data structure.
}

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.