6

I would like to have two feature modules for users to enable. One basic and one advanced. For example, the basic feature creates a blog content type, with only title and body fields. The advanced module, once enabled adds additional fields to that original content type, such as tagging or imagefield. Is this possible or will one override the other? Or will my site blow up? or?

  • 1
    Are these users are in one role? I don't think you can create two features containing single content type, that will make your feature inconsistent(my two cents). How about using permission? But you need to have a different roles I guess. – ninjascorner Jun 7 '12 at 4:24
  • It's a feature module that can be installed by site admins, devs or builders. It has nothing to do with drupal's roles and permissions. I just need to know if one feature can override or add to another feature when enabled? – sicko Jun 7 '12 at 13:07
3

It's definitely possible, I had to do something similar yesterday and I'm happy to say features can handle it perfectly.

The trick is that your 'advanced' feature will not contain the content type at all, but simply a list of the extra fields that are attached to the content type defined by the original rule.

When you install/enable the second feature module, the extra fields will be added to the existing content type as if by magic. I was pleasantly surprised at how well this works.

There is an example of a well established Drupal distribution using this same method (it's where I got the idea if I'm being honest); OpenPublish contains the openpublish_article feature, which defines a content type, and the openpublish_atricle_extra_media feature, whose sole job is to add a couple of extra fields to the article.

Just as a side note, it would probably be a good idea to make your main feature module a dependancy of the second, so the content type is definitely defined by the time the second feature is trying to add the extra fields to it.

  • 1
    I too can vouch for this method, though I'd say it's more than a "good idea" for your simpler feature to be a dependency of the advanced one - it's essential. – Garrett Albright Jun 7 '12 at 12:42
  • Fantastic. Thanks Clive. It's nice to hear that these things have already been thought out in Drupal. – sicko Jun 7 '12 at 13:10
  • How does the second feature know to attach itself the original content type created by the first feature, if the content type is not included in the second? – sicko Jun 7 '12 at 16:06
  • Because in this case you're exporting an instance of a field, not the field definition itself; since the field instance knows what entity type and bundle it's supposed to be attached to, if features finds them available in the system it can happily just add them automatically – Clive Jun 7 '12 at 16:09
  • Clive, you so s-m-r-t! :) – sicko Jun 7 '12 at 18:20
0

You can easily do this by using hook_form_alter


function hook_form_FORM_ID_alter(&$form, &$form_state, $form_id) {
  if($form_id == 'YOUR_FORM_ID') {
    // Write your code here with conditions.
    global $user;   
    if($user->role == 'somerole') {
      // Here You can add form fields whatever you want for somerole.
    }
    else {
      // Here You can add form fields whatever you want for other roles.
    }
  }
}

But for fields which you want to add for a specific role should not be required field.

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.