1

Some of my node types use fields from extra modules (such as Open Hours or Get Locations). How do I change the way those modules render their fields inside the form? I mean Get Location for example renders fieldset with fields in it. I'd like to change it at all. Also I'd like to change the order of fields and so on. Is there anyway to make it using something like field--field-name.tpl.php for output fields? Sorry if this is a common question but I couldn't find any complete guidance on this.

1

The form elements for fields (or "Field Widgets" as Drupal calls them) are defined using hook_field_widget_form(), e.g.

function hook_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  $element += array(
    '#type' => $instance['widget']['type'],
    '#default_value' => isset($items[$delta]) ? $items[$delta] : '',
  );
  return array('value' => $element);
}

They use the form API, and don't have associated templates/themes unless one is explicitly set on a contained element.

You can use hook_field_widget_form_alter() to manipulate the elements; the simplest example (from the docs) would add a class to a particular element:

function hook_field_widget_form_alter(&$element, &$form_state, $context) {
  // Add a css class to widget form elements for all fields of type mytype.
  if ($context['field']['type'] == 'mytype') {
    // Be sure not to overwrite existing attributes.
    $element['#attributes']['class'][] = 'myclass';
  }
}

But obviously you're only limited by what the form API can do. So if you wanted to provide a specific theme for an element:

function MYMODULE_field_widget_form_alter(&$element, &$form_state, $context) {
  if ($context['field']['type'] == 'mytype') {
    $element['#theme'] = 'MYMODULE_some_theme';
  }
}

Then it would just be a matter of implementing hook_theme() and creating the template file to do what you need.

Just for posterity (and because I accidentally answered the question with this first time round); the following relates to formatting the output of a field when its parent entity is displayed:

As listed in Drupal 7 Template (Theme Hook) Suggestions, and originally defined in template_preprocess_field(), these are the built-in patterns you can use to override specific field templates:

  • field--field-name--content-type.tpl.php
  • field--content-type.tpl.php
  • field--field-name.tpl.php
  • field--field-type.tpl.php

You could also add your own using a template preprocess hook:

function MYTHEME_preprocess_field(&$vars) {
  // This will let you use field--foo.tpl.php as the template name for any fields that pass the condition.
  if (some_condition()) {
    $vars['theme_hook_suggestions'][] = 'field__foo';
  }
}

As far as changing the order of field output goes, that's usually done through the UI in the "Manage Display" forms available for the various view modes for each entity type.

4
  • mm. Seems like I couldn't explain clear enough what do I need. I know how to theme a results using field--name.tpl.php. I need to change the way those fields appear in new node form (on a node creation). – Andriy May 31 '14 at 13:14
  • No you explained it perfectly, I mis-understood. Sorry about that, just writing another answer... – Clive May 31 '14 at 13:18
  • Added a bit more... – Clive May 31 '14 at 13:24
  • Thanks Clive. Let me bore you a bit more if you don't mind.) I can't get the last (about MYMODULE_field_widget_form_alter). Can you show some example step by step, please? Let's say I need to alter Geo Location module form elements. So I put a geolocation_field_widget_form_alter() into my temlpate.php, right? then I put $element['#theme'] = 'MYMODULE_some_theme' there. then what? how do I name my tpl file? How can I see a list of all variables available to use in the template? – Andriy May 31 '14 at 14:38

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.