Drupal - How to alter/hide the label of the input element in form (create/edit)?

You can do this with hook_form_alter() or hook_form_FORM_ID_alter() in your custom module or also in your theme's template.php file.

function YOURMODULE_form_alter(&$form, &$form_state, $form_id) {
  // Check the form_id
  if ($form_id == 'YOUR_FORM_ID') {
    // To hide the label
    $form['your_form_element']['#title_display'] = 'invisible';
    // To alter the label
    $form['your_form_element']['#title'] = t('Your new title');
  }
}

If you use hook_form_FORM_ID_alter() instead, you don't need to check the form_id.

You can find more information on the Form API Reference for the #title and #title_display attributes, and also all the other form elements and their attributes.

In case of Field Collection, it can be a bit complicated. Below is an example code:

function YOURMODULE_form_YOUR_FORM_ID_alter(&$form, &$form_state, $form_id) {
  $your_field_collection = element_children($form['your_field_collection'][LANGUAGE_NONE];
  foreach ($your_field_collection as $key => $value) {
    if (is_numeric($value)) {
      $form['your_field_collection'][LANGUAGE_NONE][$val]['your_field_collection_field']['#title'] = t('Your new title');
      $form['your_field_collection'][LANGUAGE_NONE][$val]['your_field_collection_field']['#title_display'] = 'invisible';
    }
  }
}

Explanation of the above code:

First I assigned the field collection itself to a variable, to make it a bit readable. I used LANGUAGE_NONE for simplicity but a better practice would be to use $form['your_field_collection']['#language'], admittedly it makes the whole code more complicated to read.

Second, I used a foreach, since most of the time we use a field collection if we want to have multiple values of some collection of fields. So the foreach loop would take care of all instances of the field collection.

Third hack, is a dirty trick I learned from someone on Drupal.org, maybe there would be a better approach, but it just works so I didn't dig into. I check if the $value is numeric, to exclude all other meta attributes and loop only through the elements.

Then, finally, in each element I alter the field in the field collection.

Tags:

Entities

7