10

I simply want to be able to remove the colon from my rendered field labels, for example:

Hotel name: to Hotel name

I copied field.tpl.php and removed the colon there, but no matter what Drupal refuses to accept it and is pulling the colon from elsewhere.

How can I do this with a template.php function?

  • did you clear the cache? – AjitS May 31 '12 at 7:13
  • yea, several times. no go – blue928 May 31 '12 at 7:21
  • possible duplicate of Remove colons from Drupal form fields/labels – Clive May 31 '12 at 11:25
  • @blue928 See the duplicate post, you need to override theme_form_element() to do this – Clive May 31 '12 at 11:26
  • That node / fix above simply removed the colons from the form when someone is editing / inputting content. I need to remove the labels after they are rendered in the node. .. so, it would not be a duplicate of the above. I actually implemented that solution to get that result. – blue928 May 31 '12 at 11:40
15

The answer is definitely to use field.tpl.php. If you look at the source for that file you can see the offending lines:

<?php if (!$label_hidden): ?>
  <div class="field-label"<?php print $title_attributes; ?>>
    <?php print $label ?>:&nbsp;
  </div>
<?php endif; ?>

(spacing and indentation mine for clarity).

Removing the colon is as simple as replacing the above lines with:

<?php if (!$label_hidden): ?>
  <div class="field-label"<?php print $title_attributes; ?>>
    <?php print $label ?>&nbsp;
  </div>
<?php endif; ?>

I've just tested that on a blank Drupal installation and it works perfectly; no more colons after field labels.

If you're already doing the same thing, but not getting the correct results, then I can think of one or two things that could be going awry:

  • You've put your custom version of field.tpl.php into a module folder, rather than in your site's active theme folder (it needs to be in the theme folder).
  • Another module/element in the system is also overriding field.tpl.php, in a more robust manner than you currently are. Seeing if this is the case will involve the always-tedious disabling of each enabled module to determine where the problem is coming from.

You won't be able to make the change with a template preprocess function, as the colon is hard-coded into the template file.

  • Ah, there was indeed another module preventing the template file from working correctly. Thanks for point out some helpful debugging directions. – blue928 Jun 3 '12 at 4:45
9

I just created a module which does exactly this, removes colons (on a per field basis) from field labels on output: Colectomy

  • Can confirm. This module works excellently and is a simple, elegant solution to the issue. – Mark Jul 15 '15 at 16:30
2

If it is a View with fields, just click on the field and uncheck "Place a colon after the label" under "Create label".

2

Display suite provides the same functionality as well:

If you enable display suite extras there's an option to remove colons before labels:

The path for that is: /admin/structure/ds/list/extras/

enter image description here

  • 1
    This does a LOT more than remove the colon. If you have applied custom CSS this will kill that. – Mark Jul 15 '15 at 15:40
0

In my case it worked by overriding theme_field() (field.module) by copying the entire function to my template.php file and changing the line that reads

$output .= '<div class="field-label"' . $variables['title_attributes'] . '>' . $variables['label'] . '&nbsp;</div>';

Clear caches before trying.

0

I couldn't find an answer to this question for Drupal 8 anywhere. With Drupal 8 the colon is added with a CSS file (field.css) in the Classy core theme if you're using it as your base theme. All you have to do is override it in your own CSS file in your theme.

So from the original field.css file:

.field--label-inline .field__label::after {
    content: ':';
}

In my theme CSS file to remove the colon:

.field--label-inline .field__label::after {
    content: '';
}

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.