0
votes

I'm at my wits end with Drupal 6 right now, and I'm hoping a second pair of eyes might be able to point out some syntax sublety I was missing earlier this morning.

Lets say I have a theme called my_theme.

This is a theme that subthemes from Ginkgo, a theme which which in turn subthemes from Rubik, whith in tun subthemes from Tao, a fairly common CSS reset theme.

I'm trying to understand how to declare my own theme functions, so I can clean up the mark up on a fee page I'm workign on for site.

Now my understanding of theming is along the lines of ' if register a theming function with hook_theme, passing it into an array, with the function's name, you'be able to call it in future with theme_function_name from within your theme.

For example, if have a module called my, I'd immplent the hook_theme like below, defining the name of the function, and defining which argument should be passed into it:

<?php

  function my_theme() {

    $items = array();
    $items['randomtext']   = array('arguments' => array('element' => NULL));
    $items['button_links'] = array('arguments' => array( '$links' => NULL, '$attributes' => NULL));

    return $items;
  };

Now I have the functions registered, I just need to implement them like this somewhat contrived example:

  function theme_randomtext($element) {
    $output = ' <h1>This is some very random text with' . ' this text concatenated: '  . $element . '</h1>';
    return $output;
  }

I can now use this new function by calling

  <php
    print theme('randomtext', 'some arbitrary words')

  ?>

And expect the following content to be returned:

  <h1>This is some very random text with this text concatenated: some arbitrary words</h1>
I'm getting nada.

And I don't know why. When I look at the theme registry, I see my function listed as my_randomtext - I've tried calling both of these options, in case I should have been adding the theme prefix:

  theme('my_randomtext', 'getting desparate');
  theme('randomtext', 'really losing my rag now');

Still no luck.

Why might these theme functions not be working? And what advantage does this give you over simply declaring a function like so in a theme?

  function manual_random_text($element) {
        $output = ' <h1>This is gives me everything I aleady need, 
                    'without relying on the weird themeing sytem. It's perfect for' . $element . '</h1>';
        return $output;

  }
1

1 Answers

0
votes

Ah, after a bit of distance from the problem, I see the issue.

For this to work, I need to add the prefix of the theme I declare the function in, to make it available to me for use.

So if my module name was my, the desired theme function name would be:

function my_random_text($element) {
        $output = ' <h1>, 
                    'This is finally outputting content. Now I can: ' . $element . '</h1>';
        return $output;
}

The thing is, if I'm creating an override to a theme function that doesn't already exist, like random_text, there are no functions for it to override anyway. The term theme override suggests there being a default theming function to override - in many themes, we're not making explicit overrides of functions, but creating alternatives to use in specific cases.

I'm now assuming that I'd need to define this in a module if I wanted to define a function as theme_function_name rather than themename_theme_function_name.

This page here about theming on Drupal.org was a useful quide in finding out some of this info.