1

I try to alter taxonomy edit form.

This is my hook:

function MYMODULE_form_taxonomy_form_term_alter(&$form, &$form_state, $form_id) {
    // hide the desciption field and relations group field
    $form['description']['#type'] = 'hidden';
    $form['relations']['#type'] = 'hidden';

    // set the default value
    $form['description']['#value'] = ' ';
    $form['relations']['weight']['#value'] = 0;
    $form['relations']['parent']['#value'] = array('value' => '13');

}

But When I submit, It throw me an error:

Warning: Illegal string offset 'value' in taxonomy_form_term_submit_build_taxonomy_term() (line 862 in /Users/tangmonk/MyGit/Drupal/daxuebao/modules/taxonomy/taxonomy.admin.inc).

1 Answer 1

1

The problem causing the error is that you are setting your hidden description field value is a string, but the taxonomy module is expecting a text area type array with a 'value' and a 'format', which you cannot replicate with a hidden field.

Unfortunately the term description field is a special property of the term, not a proper field, so you can't remove the field (the taxonomy module relies on it being there).

If you don't want people to see it and you always want it to have an empty value a good option is to restrict it from the user like this:

$form['description']['#access'] = FALSE;

but that doesn't allow you to change the value if you want to.

Another option is to use 'value' instead of 'hidden' as the type, like this:

/**
 * Implements hook_form_FORM_ID_alter().
 */
function MYMODULE_form_taxonomy_form_term_alter(&$form, &$form_state, $form_id) {
  // Hide the desciption field and relations group field
  $form['description'] = array(
    '#type' => 'value',
    '#value' => array(
      'value' => '',
      'format' => 'filtered_html',
    ),
  );
  // Unset the existing relations fieldset.
  unset($form['relations']);
  // Set hidden values for all the sub-fields of the relations fieldset.
  $form['weight'] = array(
    '#type' => 'value',
    '#value' => 0,
  );
  $form['parent'] = array(
    '#type' => 'value',
    '#value' => 13,
  );
}

Note the changes to the relations fieldset too:

Since you only want to pass hidden values from the children of the fieldset just remove the fieldset altogether and then set the hidden values (be careful if using other add on modules that add fields to the 'relations' fieldset).

Because the fieldset wasn't using #tree the values passed through in $form_state will just be ['weight'] and ['parent'], not ['relations']['weight'] and ['relations']['parent'] so you can leave the ['relations'] part out when adding the values back in.

3
  • thanks for your reply, '#access' and '#hidden' are the same effect(hide field for user), what's the deep different of '#access' and '#hidden'?
    – Moon soon
    Apr 23, 2014 at 13:08
  • Hidden created a html input field of type hidden, which changes the way the value is passed through when the form is submitted (description is normally an array of value & format, which hidden fields can't replicate). Using #access means the field is not added to the form at all.
    – rooby
    Apr 23, 2014 at 13:11
  • See developer.mozilla.org/en-US/docs/Web/HTML/Element/… - hidden is a type of field, which is how the drupal hidden type works.
    – rooby
    Apr 23, 2014 at 13:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.