7

We can theme the label of any field in tpl or even simply by changing the manage display option but wondering how to do the same in form (while creating or editing a content) ?

I'm looking for the proper way to alter/hide the label especially with field collection fields when the content is created or edited.

20

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.

  • Thanks. But in case of field collection, its not having any of those attributes attached with – pown Oct 29 '13 at 9:30
  • It surely does, but the field collection is a little bit complicated depending on how you configure the field collection field. If you use the 'Embedded' widget, you should be able to find the fields in your form array. – Елин Й. Oct 29 '13 at 9:35
  • its embedded widget only, but still nothing happening. I'm not trying to do anything with field collection but with field collection fields is whre I want to change the label – pown Oct 29 '13 at 9:36
1

This will help you.

/**
 * Implements hook_field_widget_form_alter().
 */
function YOURMODULE_field_widget_form_alter(&$element, &$form_state, $context) {
  if (isset($element['#entity_type']) && $element['#entity_type'] == 'field_collection_item' && $element['#bundle'] == 'YOUR FIELD COLLECTION') {
    $element['value']['#title_display'] = 'invisible';
  }
}
1

The field_display_label module solves this by adding a separate display label.

This way, the original field label appears exclusively on the create/edit form, while the display label (if set) appears exclusively on the view page. So by setting the original field label to nothing, you can remove the label in the create/edit form only.

0

Using hook_form_FORM_ID_alter() trying to act on the node edit form, this worked for me:

function MYMODULE_form_NODE_TYPE_node_form_alter(&$form, &$form_state, $form_id) {
  $form['field_MY_FIELD']['und']['#title_display'] = 'invisible';
}

(Substitute your values for values in ALL CAPS.)

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.