I've got a template.php with the folowing code to override the _links primary links theme:

function phptemplate_links($links, $attributes = array('class' => 'links')) {
  global $language;
  $output = '';
  if (count($links) > 0) {
    $output = '<ul'. drupal_attributes($attributes) .'>';

    $num_links = count($links);
    $i = 1;

    foreach ($links as $key => $link) {
      $class = $key;

      // Add first, last and active classes to the list of links to help out themers.
      if ($i == 1) {
        $class .= ' first';
      }
      if ($i == $num_links) {
        $class .= ' last';
      }
      if (isset($link['href']) && ($link['href'] == $_GET['q'] || ($link['href'] == '<front>' && drupal_is_front_page()))) {
        $class .= ' active';
      }
      $output .= '<li'. drupal_attributes(array('class' => $class)) .'>';

      if (isset($link['href'])) {
        $link['title'] = check_plain($link['title']) . '<span>&nbsp;</span>';
        $link['html'] = TRUE;      
        // Pass in $link as $options, they share the same keys.
        $output .= l($link['title'], $link['href'], $link);        
      }
      else if (!empty($link['title'])) {
        // Some links are actually not links, but we wrap these in <span> for adding title and class attributes
        if (empty($link['html'])) {
          $link['title'] = check_plain($link['title']);
        }
        $span_attributes = '';
        if (isset($link['attributes'])) {
          $span_attributes = drupal_attributes($link['attributes']);
        }
        $output .= '<span'. $span_attributes .'>'. $link['title'] .'</span>';
      }

      $i++;
      $output .= "</li>\n";
    }

    $output .= '</ul>';
  }
  return $output;
}

But it doesn't override the theme.inc.

If I change the theme.inc code to the same as above (using 'function theme_links') it works, but obviously I want the theme changes to be done in the template.php, not the theme.inc.

Any ideas?

Lee

Comments

anriettec’s picture

Same here, I am also trying to override theme_links(*) using phptemplate_links(*), but to no avail.

Does anyone know why this is not working?

mvaneck’s picture

Also tried with the

function phptemplate_links($links, $attributes = array('class' => 'links')) {
  global $language;

added in template.php - but no luck. Please help!

Regards,
Mike

Karlheinz’s picture

I'm no expert by any means... but did you try doing a theme override, rather than a template engine override?

That is, in template.php, change:
function phptemplate_links(...

to:
function YOURTEMPLATENAME_links(...

If that works, maybe there's a bug in the template engine override code in core...?

I've only overridden functions on the theme level, so that's really all the info I can give you.

-Karlheinz

tonicbox’s picture

Hi Karl-Heinz

Thanks for the tip. It didn't make any difference though. My modified links theme only works when it's in the theme.inc file.

The strange thing is that if I call it have the same code in both theme.inc and template.php I get an error saying they can't be the same, so it's obviously reading the template.php file. So why doesn't it override?! Very frustrating!

Any more ideas anyone?

Lee

Jeff Burnz’s picture

The Adams Family’s picture

I've cleared cache but to no avail.

Has anyone figured this out yet?

Karlheinz’s picture

You know, I'm looking at your code above, and I can't see any difference between it and the theme_links() function in theme.inc. What did you change?

What I'm saying is that perhaps it IS working, but it's not having the effect you want. Maybe you should put a big flashing "HEY PAL IT'S WORKING" sign in the code or something, as a debug.

One other possibility: If your template has both phptemplate_links() AND mytheme_links(), I would imagine that the mytheme_links() function would take precedence. This would also be an issue if e.g. you're writing a sub-theme. (That's why Drupal recommends the mytheme_ method.)

EDIT:
Explain what you mean by this:

The strange thing is that if I call it have the same code in both theme.inc and template.php I get an error saying they can't be the same

-Karlheinz

-Anti-’s picture

Did anyone figure this out yet?

It appears to me that *any* function that resides in *any* .inc file cannot be over-ridden in template.php.

I've tried three times to over-ride such functions, and each time it has failed for no apparent reason.
The only difference between these three and other functions that I have managed to over-ride, is that
these three are in .inc files.

Any ideas or suggestions are welcome.

Jeff Burnz’s picture

This is not the case dude, think of theme.inc's theme-able functions, which of course we can override in template.php

-Anti-’s picture

That might be the case, but from my perspective I've got a 100% failure rate for
functions in .inc files and a 100% success rate over-riding functions elsewhere.

Albeit, I don't know any php, so I'm just copying snippets into template.php
But there does seems to be something going wrong with over-riding .inc files.

Jeff Burnz’s picture

You need to be more specific; paste an example with details of what you are doing/want to achieve.

-Anti-’s picture

OK, here's one:

In /modules/wysiwyg/editors/timymce.inc there is this function:

function wysiwyg_tinymce_settings($editor, $config, $theme) {
  $settings = array(
'gecko_spellcheck' => TRUE,
    'button_tile_map' => TRUE, // @todo Add a setting for this.
    'document_base_url' => base_path(),
    'mode' => 'none',
    'plugins' => array(),
    'theme' => $theme,
    'width' => '100%',
    // Strict loading mode must be enabled; otherwise TinyMCE would use
    // document.write() in IE and Chrome.
    'strict_loading_mode' => TRUE,
    // TinyMCE's URL conversion magic breaks Drupal modules that use a special
    // syntax for paths. This makes 'relative_urls' obsolete.
    'convert_urls' => FALSE,
    // The default entity_encoding ('named') converts too many characters in
    // languages (like Greek). Since Drupal supports Unicode, we only convert
    // HTML control characters and invisible characters. TinyMCE always converts
    // XML default characters '&', '<', '>'.
    'entities' => '160,nbsp,173,shy,8194,ensp,8195,emsp,8201,thinsp,8204,zwnj,8205,zwj,8206,lrm,8207,rlm',
  );
  if (isset($config['apply_source_formatting'])) {
    $settings['apply_source_formatting'] = $config['apply_source_formatting'];
  }
  if (isset($config['convert_fonts_to_spans'])) {
    $settings['convert_fonts_to_spans'] = $config['convert_fonts_to_spans'];
  }
  if (isset($config['language'])) {
    $settings['language'] = $config['language'];
  }
  if (isset($config['paste_auto_cleanup_on_paste'])) {
    $settings['paste_auto_cleanup_on_paste'] = $config['paste_auto_cleanup_on_paste'];
  }
  if (isset($config['preformatted'])) {
    $settings['preformatted'] = $config['preformatted'];
  }
  if (isset($config['remove_linebreaks'])) {
    $settings['remove_linebreaks'] = $config['remove_linebreaks'];
  }
  if (isset($config['verify_html'])) {
    $settings['verify_html'] = $config['verify_html'];
  }

  if (!empty($config['css_classes'])) {
    $settings['theme_advanced_styles'] = implode(';', array_filter(explode("\n", str_replace("\r", '', $config['css_classes']))));
  }

  if (isset($config['css_setting'])) {
    if ($config['css_setting'] == 'theme') {
      $settings['content_css'] = implode(',', wysiwyg_get_css());
    }
    else if ($config['css_setting'] == 'self' && isset($config['css_path'])) {
      $settings['content_css'] = strtr($config['css_path'], array('%b' => base_path(), '%t' => path_to_theme()));
    }
  }

You can see I added: 'gecko_spellcheck' => TRUE at the top to turn on firefox spellchecker (which tinymce has off by default). This works BTW.

Aren't I correct in assuming that instead of having to hack the .inc file, I should be able to over-ride the function in template.php and add the spellcheck setting that way?

If not, can you explain why not?
If so, could you give me a clue how I should be doing it?

Thanks for your help.

Jeff Burnz’s picture

This is not a theme function, its a module layer function (being called and used in the module layer) the party is over by the time it gets to the theme layer.

The functions you can override in the theme layer all begin with theme_, such as theme_links, theme_item_list and so on.

I dont know anything about this module, I would think what you want sounds like a feature request for the module (perhaps this could be a setting in the module for TinyMCE?) - probably best to address this in the issue queue for the module.

Karlheinz’s picture

To be clear:

Whenever you write a theme override function (let's say "myawesometheme_foo"), Drupal looks for a function called "theme_foo" to override. If there isn't one, it considers it a new function and doesn't override anything.

So, the function you're overriding has to begin with "theme_". If it doesn't, you're out of luck. This is true of both core and contributed modules. You can hack the core (the .inc or .module file associated with the module), but that's not the "Drupal way" of doing things - go to the issue queue and write up a feature request instead.

Here's a list of all the core functions that can be overridden:
http://api.drupal.org/api/group/themeable/6

Also, see the theming handbook page about function overrides:
http://drupal.org/node/173880#function-override

Also, you really should name your function according to theme rather than engine: "mytheme_foo" = good, "phptemplate_foo" = bad. If you do the second, it will override that function for every single theme that uses that template engine, which is probably all of them.

Incidentally, unless you need to change a module's HTML output, you don't need to do a function override, you can just change the CSS.

-Karlheinz

michaels-musings.com’s picture

Okay, I was having the same problem, nothing I seemed to try would change "function pixture_reloaded_username($object)" to output what I had changed it too. I even had cache turned off to try and help, well, Drupal's idea of caching being "off" is odd, some other changes in my theme's template.php were showing up, but never the username changes. In desperation, I did it "all"

- Completely closed and cleared the testing browser's cache
- Re-uploaded all changed files
- Manually clear Drupal's cache: Home » Administer » Site configuration » Clear cached data
- Saved theme's configuration: Home » Administer » Site building » Themes » Configure » {the-theme} » "Save Configuration"

And, it worked. "Not verified" finally disappeared like I asked it too.

Computers, such a pain.

Michael

Karlheinz’s picture

What is your theme name? If it's not "pixture_reloaded," that won't work.

From another of your posts (which I found by searching for "pixture_reloaded_username"), I gathered that the function you're trying to override is theme_username(). If your theme is named "pixture", then the override function should be called "pixture_username()".

If the theme is indeed "pixture_reloaded," then it should work fine. Yes, you will need to clear all caches to see the results, including your browser's cache (usually you can just hit "reload" for this) and Drupal's cached data. If there's some CSS and/or JavaScript involved, it might also help to turn off optimization.

After this, you should visit Admin >Site Building > Themes. Clearing the cached data SHOULD rebuild the theme registry, but visiting this page will do it for sure.

I've also noticed a delay in updating this stuff with some webhosts - I think some might use their own file-caching system.

-Karlheinz

aosiname’s picture

...that cannot be overriden.

I am trying to overide function theme_cart_review_table($show_subtotal = TRUE) located in ubercart uc_cart_checkout_pane.inc file

So I have copied the function, and renamed it to phptemplate_cart_review_table($show_subtotal = TRUE) in template.php

(i have other overridden functions in there that work)

Made a few changes in the template.php version of the function (nothing major) just added another column into the table it returns

But the changes do not appear on my page and I have cleared the cache many times...even cleared my firefox browser cache hoping it may work...but nothing

Any ideas?

yub_yub’s picture

I am having a similar issue. I am trying to override theme_views_mini_pager to update the text of the forward/back navigation links. I've copied the function from theme.inc into template.php, and renamed the function rst_views_mini_pager. Before even attempting to make the (simple) edits I want, I just tried using this 'vanilla' stub, but it produces an undesired output - it's just spitting out the " " which indicates that there's nothing to show. (There is more content to show, if I use the function in theme.inc it works fine.) Here's the code I'm using. Any ideas on what I might be doing wrong?

The issue here was of course not related to being "unable" to override theme.inc. If anyone else finds this thread, the solution was to save template.php encoded in UTF-8, as found at #503062: mini pager override is broken

 function rst_views_mini_pager($tags = array(), $limit = 10, $element = 0, $parameters = array(), $quantity = 9) {
  global $pager_page_array, $pager_total;

  // Calculate various markers within this pager piece:
  // Middle is used to "center" pages around the current page.
  $pager_middle = ceil($quantity / 2);
  // current is the page we are currently paged to
  $pager_current = $pager_page_array[$element] + 1;
  // max is the maximum page number
  $pager_max = $pager_total[$element];
  // End of marker calculations.


  $li_previous = theme('pager_previous', (isset($tags[1]) ? $tags[1] : t('‹‹')), $limit, $element, 1, $parameters);
  if (empty($li_previous)) {
    $li_previous = "&nbsp;";
  }

  $li_next = theme('pager_next', (isset($tags[3]) ? $tags[3] : t('››')), $limit, $element, 1, $parameters);
  if (empty($li_next)) {
    $li_next = "&nbsp;";
  }

  if ($pager_total[$element] > 1) {
    $items[] = array(
      'class' => 'pager-previous',
      'data' => $li_previous,
    );

    $items[] = array(
      'class' => 'pager-current',
      'data' => t('@current of @max', array('@current' => $pager_current, '@max' => $pager_max)),
    );

    $items[] = array(
      'class' => 'pager-next',
      'data' => $li_next,
    );
    return theme('item_list', $items, NULL, 'ul', array('class' => 'pager'));
  }
}
mohamadaliakbari’s picture

I had same problem with theme_pager. I rebuild theme registry several times, but it didn't work.
finally I delete theme_registry:roshd_danesh row from cache table of database, I think it is a bug in cache functions in drupal

pat64j’s picture

I had similar issue with my template.php where any function i write in there will not run.
I suspected my theme name to be the cause so I run "dsm(variable_get('theme_default','none'));".
Then I copied and pasted what drupal printed for me as the theme name.
I realized drupal uses the file name for the theme's info file as the theme name instead of the name value I set in the info file.