4

I have a field that accepts a date range using the Date module. This is the theme function for outputting the date (found in sites/all/modules/contrib/date/date.theme):

/**
 * Returns HTML for a date element formatted as a range.
 */
function theme_date_display_range($variables) {
  $date1 = $variables['date1'];
  $date2 = $variables['date2'];
  $timezone = $variables['timezone'];
  $attributes_start = $variables['attributes_start'];
  $attributes_end = $variables['attributes_end'];

  // Wrap the result with the attributes.
  return t('!start-date to !end-date', array(
    '!start-date' => '<span class="date-display-start"' . drupal_attributes($attributes_start) . '>' . $date1 . '</span>',
    '!end-date' => '<span class="date-display-end"' . drupal_attributes($attributes_end) . '>' . $date2 . $timezone . '</span>',
  ));
}

I somehow need to override that and replace the 'to' in the return with a hyphen... I know, it's nitpicky but it underlines a more important point - I honestly have no way to alter this. I've tried overriding it in my template.php but that doesn't take. I literally just copy and pasted this code but replaced theme in the function with the actual name for my theme.

So yeah... how do I alter this theme function "the Drupal way"?

  • 1
    did you try clearing your cache? – Mike Miles Jun 21 '13 at 12:16
3

With the help of hook_theme_registry_alter you can override(actually by passing) any theme function.

Usually module's will define their theme functions in hook_theme(), however using hook_theme_registry_alter you can alter those information.

For your case below code will achieve the desired functionality,

/**
 * Implements hook_theme_registry_alter().
 */
function <my_module>_theme_registry_alter(&$theme_registry) {
  if (isset($theme_registry['date_display_range'])) {
    $theme_registry['date_display_range']['function'] = 'theme_custom_date_display_range';
  }
}

/**
 * Returns HTML for a date element formatted as a range.
 */
function theme_custom_date_display_range($variables) {
  $date1 = $variables['date1'];
  $date2 = $variables['date2'];
  $timezone = $variables['timezone'];
  $attributes_start = $variables['attributes_start'];
  $attributes_end = $variables['attributes_end'];

  // Wrap the result with the attributes.
  return t('!start-date - !end-date', array(
    '!start-date' => '<span class="date-display-start"' . drupal_attributes($attributes_start) . '>' . $date1 . '</span>',
    '!end-date' => '<span class="date-display-end"' . drupal_attributes($attributes_end) . '>' . $date2 . $timezone . '</span>',
  ));
}

So whenever you call theme('date_display_range', $variables), theme_custom_date_display_range() function will be invoked instead of theme_date_display_range().

Don't forget to clear the caches after adding hook_theme_registry_alter

0

If all you want is to change "to" to "-", then hook_theme_alter is overkill.

You can simply use the translation layer:

At least 2 options:

  1. in settings.php:

    $conf['locale_custom_strings_en'][''] = array('!start-date to !end-date' => '!start-date - !end-date');

  2. use stringoverrides module, which provides a GUI for same.

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.