0

I have a taxonomy reference field and in the node creation form there will be only one value allowed and I guess I use a radio button and I want it to be enabled by default.

I thought I can do it with form_alter but on inspection in krumo I see the default values are set to the concrete term id.

If I would want to set that id (which I do not know) it could look like:

 form['field_abteilung'][LANGUAGE_NONE]['#default_value'] = 12345;

For various users and their roles there will be other another allowed option in this form and I just want to do sth like

 form['field_abteilung'][LANGUAGE_NONE].... My-single-radio-button = checked;

Update

I saw I can find out the single option by looking into

$form['field_sektion'][LANGUAGE_NONE]['#options'][2] 

I tried it but get an error about missing index 'field_sektion' which confuses me.

update 2 It takes some time, I maybe should have mentioned that the field in the end should be the access control field of https://drupal.org/project/workbench_access , that module also alters the form; I disabled the field's access function first and now need to rebuild all

  • 1
    A radio group is used to indicate more than one choice. From a UI/UX perspective you should probably reconsider your approach. Standard fare would be to convert the field to a 'value' type, which is hidden from display on the front end – Clive Aug 28 '13 at 10:45
  • I agree , could you tell me how to make it a value type (it is a term reference...) – groovehunter Aug 28 '13 at 10:51
  • You'd probably get away with something as simple as $form['field_sektion'][LANGUAGE_NONE]['#type'] = 'value'; as long as you have a #default_value set. Give that a try and if it causes any problems just update the question with the code and I'm sure you'll get it fixed – Clive Aug 28 '13 at 10:54
0

the way I did it now, is following - First I tried to use a hook provided by workbench access itself which supposed to be for that use case, look here https://drupal.org/node/2019473

But it would not work for me, so I did : this results in a select box with only one possibility. Not the best UX style maybe and later I will try to hide the element, maybe with ds module.

function workbench_access_crud_by_state_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id != 'hardware_antrag_node_form') {
    return;
  }
  //sdpm($form);
  $option_arr = $form['field_tu'][LANGUAGE_NONE]['#options'];
  // remove 'none'-option
  array_shift($option_arr);

  // look for field for workbench_access and replace options
  $wb_access_field = $form['workbench_access_fields']['#value'][0];
  $form[$wb_access_field][LANGUAGE_NONE]['#options'] = $option_arr;
}

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.