0

I have the field_tags set to be a select box (not auto-complete). Inside hook_form_alter, how can I change the default value of the field_tags select box? So far, I have:

function autotag_custom_form_alter(&$form, &$form_state, $form_id){

    if($form_id == 'article_node_form'){
      //change default value here
    }

}

But I don't know how to access the field_tags element and then change the default value. Please help! Thanks.

8
  • I suggest you install the devel module. With it, you can print out your $form using dpm($form) and inspect the variable to see what is in it and figure out what you need to change. See guide and screenshots at ratatosk.net/drupal/tutorials/debugging-drupal.html
    – nmc
    Jul 23 '12 at 15:29
  • I did install it. I'm not that familiar with PHP, so I'm having trouble with it. I see in Devel that there's a 'field_tags' variable only when editing an article. But I don't see one when I create a new article (there's no Devel tab when creating a new article). Does that mean it's not possible to change the field_tags value here? If not, what function should I implement?
    – Nicole
    Jul 23 '12 at 15:45
  • You can add dpm($form); line to where you have //change default value here in your code above to print the form and inspect the form elements. Perhaps you should also mention why you're doing this programatically instead of setting defaults in the field setting through the interface.
    – nmc
    Jul 23 '12 at 15:52
  • When I do dpm($form), I see field_tags >> #attributes >> field-widget-options-select. How do I change the default option? I tried to install various modules, but none of them did what I needed. I need to assign the tag to the article automatically, based on the author's role. So I wanted to do this once the article is being created.
    – Nicole
    Jul 23 '12 at 15:53
  • Actually, I think I want something like this (but this doesn't work): $form['field_tags']['und']['#default_value'] = $form['field_tags']['und']['#options'][2];
    – Nicole
    Jul 23 '12 at 16:08
1

The field_tags element is likely a widget from the Field API. You can surely alter it from a form alter hook, but be aware that it would not be your average Form API element. The Field API adds post-processor, pre-rendered, etc. functions of its own. These functions may expect things you may want to alter.

You can however, change the default value of the field from the configuration interface of your content type (you need to enable the Field UI module for this). You should also be able to select a different widget (ie. a select box) from there.

2
  • I have Field UI enabled. I'm not sure how this can be configured to do what I want? I was able to create a barebones "module" (more of a hack) that sets the default value based on user role. Currently it just does this programmatically by hardcoding specific roles and specific tags. What I think I need is to create a Configuration page that allows me to choose a role and then pick the default tag that appears.
    – Nicole
    Jul 24 '12 at 22:30
  • You should update your question to explain that you need different default value depending on the current user's role. Jul 25 '12 at 21:15

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.