4

I'm using Twitter Bootstrap and I want to expand the length of all the title fields in the content forms by adding 'span6' to the class attribute for the title field's input element.

I've added the following function to my template.php file:

function ccom_field__title($variables) {
  $output = '';

  // Render the label, if it's not hidden.
  if (!$variables['label_hidden']) {
    $output .= '<div class="field-label"' . $variables['title_attributes'] . '>' . $variables['label'] . ':&nbsp;</div>';
  }

  // Render the items.
  $output .= '<div class="field-items"' . $variables['content_attributes'] . '>';
  foreach ($variables['items'] as $delta => $item) {
    $classes = 'field-item ' . ($delta % 2 ? 'odd' : 'even');
    $output .= '<div class="' . $classes . '"' . $variables['item_attributes'][$delta] . '>' . drupal_render($item) . '</div>';
  }
  $output .= '</div>';

  // Render the top-level DIV.
  $output = '<div class="' . $variables['classes'] . '"' . $variables['attributes'] . '>' . $output . '</div>';

  return $output;
}

Right now all I've done to it is change the name of the function according to the functions preceding comment. I'm not sure what I need to do, because I need to add the class to the input element directly, and not its parent div element.

Using JavaScript (including jQuery) is not an option.

TL;DR

How does one add a class to a specific field's input element?

  • 1
    The node title isn't normally a field (it's what's known as a 'property' of the entity) and as such isn't themed by the field module. Do you have a field that you've added specifically called field_title that you want to add the class to, or do you mean the normal title? – Clive Apr 19 '12 at 23:11
  • I meant the normal title. I assumed title was valid because in the theme_field comments there is an example for changing the body field, which I guess is an actual field and not a property? – Lester Peabody Apr 19 '12 at 23:29
  • 1
    Yeah the body is a field in Drupal 7 but the title isn't. It'd probably be easier to do this in hook_form_alter(), I'll pop an answer in – Clive Apr 19 '12 at 23:30
  • This is a little after the fact but you can use the "Title" module (drupal.org/project/title) to replace the traditional title as a field and then it's fully themeable as such. I use this on my sites and it's great. It's also great for multi-lingual sites using Entity Reference. You can also output it in custom node tpl files (hint get the variable via Contemnplate module). One gotcha is if you are using Omega, then it get's a little tricky so you might need the Delta Module to help matters. – Danny Englander Apr 29 '12 at 14:30
3

The content title is a property rather than a field, so overriding theme_field() isn't the way to do this.

Instead you might want to use a hook_form_FORM_ID_alter() (which can also be used in your theme's template.php file):

function ccom_form_node_form_alter(&$form, &$form_state, $form_id) {
  $form['title']['#attributes']['class'][] = 'span6';
}
  • Thanks Clive I'll give this a shot tomorrow morning when I get into the office :) You've come to my rescue on more than 1 occasion now. Cheers. – Lester Peabody Apr 19 '12 at 23:57

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.