Question on #theme attribute in D6 FAPI

View: New views
10 Messages — Rating Filter:   Alert me  

Question on #theme attribute in D6 FAPI

by Syscrusher-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Good evening!

I have a form that works fine in Drupal 5 and uses the #theme attribute
to invoke a user-defined function. I'm trying to update the module to
Drupal 6, and am getting no errors, but the #theme function is no longer
being invoked (I placed trace statements there, and am using
devel.module for diagnostic printing using dprint_r()).

I checked the upgrade-your-module documentation (which is excellent,
btw), but there's no mention of a change to how #theme works. Have I
overlooked a change that's documented elsewhere?

Here are the code snippets. In the code that generates the $form array,
I have this:

        $form['links_related']['#theme'] = 'links_related_form';

and the theme function is defined thusly:

        function theme_links_related_form($form)

I have a trace print() statement right inside that function as the first
line, so I know the function isn't being called.

By the way, the form itself generates and executes correctly, with all
fields working. The only problem is that the theming isn't being called,
so I'm getting plain vanilla output rather than the table that is
supposed to neatly align the columns (my theme function calls Drupal's
core theme('table', ....) function to do this).

Again, this worked in D5, so I'm looking for what needs to change for
D6.

Thanks for any suggestions.

Scott
--
Syscrusher <syscrusher@...>


Re: Question on #theme attribute in D6 FAPI

by Darrel O'Pry-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Did you add the #theme function to hook_theme? #theme should work the same. I don't believe there were any changes to how it is handled in drupal_render.

.darrel.

On Wed, Jul 9, 2008 at 7:32 PM, Syscrusher <syscrusher@...> wrote:
Good evening!

I have a form that works fine in Drupal 5 and uses the #theme attribute
to invoke a user-defined function. I'm trying to update the module to
Drupal 6, and am getting no errors, but the #theme function is no longer
being invoked (I placed trace statements there, and am using
devel.module for diagnostic printing using dprint_r()).

I checked the upgrade-your-module documentation (which is excellent,
btw), but there's no mention of a change to how #theme works. Have I
overlooked a change that's documented elsewhere?

Here are the code snippets. In the code that generates the $form array,
I have this:

       $form['links_related']['#theme'] = 'links_related_form';

and the theme function is defined thusly:

       function theme_links_related_form($form)

I have a trace print() statement right inside that function as the first
line, so I know the function isn't being called.

By the way, the form itself generates and executes correctly, with all
fields working. The only problem is that the theming isn't being called,
so I'm getting plain vanilla output rather than the table that is
supposed to neatly align the columns (my theme function calls Drupal's
core theme('table', ....) function to do this).

Again, this worked in D5, so I'm looking for what needs to change for
D6.

Thanks for any suggestions.

Scott
--
Syscrusher <syscrusher@...>



Re: Question on #theme attribute in D6 FAPI

by Syscrusher-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Wed, 2008-07-09 at 19:48 -0400, Darrel O'Pry wrote:
> Did you add the #theme function to hook_theme? #theme should work the
> same. I don't believe there were any changes to how it is handled in
> drupal_render.

Thanks for the quick reply, but now I'm confused. With regard to
registering my theme function, no, I didn't realize I needed to do that.
Is this necessary for the FAPI to call a theme function that is
referenced in the $form array? IOW, just declaring the function in the
$form array doesn't imply that it should be called?

I went to the hook_theme documentation, but I'm unclear on how those
registration parameters apply to FAPI clients; the documentation seems
to speak from the viewpoint of the theme writer. I'm not writing a
theme, just needing to wrap form elements into a table from the
drupal_render() call. I embed class and ID attributes in the table which
the theme can modify for aesthetics -- IOW, my module only applies the
document structure elements without regard to their cosmetics at display
time.

Am I taking the wrong approach to this?

Scott

--
Syscrusher <syscrusher@...>


Re: Question on #theme attribute in D6 FAPI

by Earl Miles :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Syscrusher wrote:

> On Wed, 2008-07-09 at 19:48 -0400, Darrel O'Pry wrote:
>> Did you add the #theme function to hook_theme? #theme should work the
>> same. I don't believe there were any changes to how it is handled in
>> drupal_render.
>
> Thanks for the quick reply, but now I'm confused. With regard to
> registering my theme function, no, I didn't realize I needed to do that.
> Is this necessary for the FAPI to call a theme function that is
> referenced in the $form array? IOW, just declaring the function in the
> $form array doesn't imply that it should be called?
>
> I went to the hook_theme documentation, but I'm unclear on how those
> registration parameters apply to FAPI clients; the documentation seems
> to speak from the viewpoint of the theme writer. I'm not writing a
> theme, just needing to wrap form elements into a table from the
> drupal_render() call. I embed class and ID attributes in the table which
> the theme can modify for aesthetics -- IOW, my module only applies the
> document structure elements without regard to their cosmetics at display
> time.
>
> Am I taking the wrong approach to this?

FAPI doesn't talk to the theme layer, and because there is no form
registration, it can't do that for you. This is something I'd like to
see changed in the future, since it is actually not at all difficult to
figure out what forms are available if we just add a tiny bit of code.

All form themes have exactly one argument, so:

   'arguments' => array('element' => NULL),


Re: Question on #theme attribute in D6 FAPI

by Earl Miles :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Syscrusher wrote:
> I went to the hook_theme documentation, but I'm unclear on how those
> registration parameters apply to FAPI clients; the documentation seems
> to speak from the viewpoint of the theme writer. I'm not writing a
> theme, just needing to wrap form elements into a table from the
> drupal_render() call. I embed class and ID attributes in the table which
> the theme can modify for aesthetics -- IOW, my module only applies the
> document structure elements without regard to their cosmetics at display
> time.

See: http://drupal.org/node/165706 -- that's in the module developer's
guide.

And the api docs for hook_theme, of course.

Re: Question on #theme attribute in D6 FAPI

by Syscrusher-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Wed, 2008-07-09 at 18:26 -0700, Earl Miles wrote:
> See: http://drupal.org/node/165706 -- that's in the module developer's
> guide.

Ah. That helps, some. I need to go look at the code in forum.module to
see how they did it, but this is starting to clear the murk in my brain.
Thanks!

>
> And the api docs for hook_theme, of course.

Been there, done that, and that's the page that confused me to prompt
the question on the list. :-)

I think node 165706 plus the forum sample code will probably get me
squared away. It's obvious that the step I missed is to register the
theme call, now I just need to walk through how that process works in
some existing code. I'm one of those people who learns best from
examples rather than theoreticals. :-)

Thanks for the referrals. I hadn't seen that page in the module dev
guide.

Scott

--
Syscrusher <syscrusher@...>


Re: Question on #theme attribute in D6 FAPI

by Darrel O'Pry-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message



On Wed, Jul 9, 2008 at 9:22 PM, Syscrusher <syscrusher@...> wrote:
On Wed, 2008-07-09 at 19:48 -0400, Darrel O'Pry wrote:
> Did you add the #theme function to hook_theme? #theme should work the
> same. I don't believe there were any changes to how it is handled in
> drupal_render.

Thanks for the quick reply, but now I'm confused. With regard to
registering my theme function, no, I didn't realize I needed to do that.
Is this necessary for the FAPI to call a theme function that is
referenced in the $form array? IOW, just declaring the function in the
$form array doesn't imply that it should be called?

I went to the hook_theme documentation, but I'm unclear on how those
registration parameters apply to FAPI clients; the documentation seems
to speak from the viewpoint of the theme writer. I'm not writing a
theme, just needing to wrap form elements into a table from the
drupal_render() call. I embed class and ID attributes in the table which
the theme can modify for aesthetics -- IOW, my module only applies the
document structure elements without regard to their cosmetics at display
time.

Am I taking the wrong approach to this?

FAPI and the theme layer are not the same... A theme function must be declared in hook_theme() in order for theme() to call it. FAPI simply calls  theme() with the $element.



Re: Question on #theme attribute in D6 FAPI

by Syscrusher-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Thu, 2008-07-10 at 02:30 -0400, Darrel O'Pry wrote:
> FAPI and the theme layer are not the same... A theme function must be
> declared in hook_theme() in order for theme() to call it. FAPI simply
> calls  theme() with the $element.

Ahhhhhh....that helps a LOT. I realized they were separate layers, but I
was under the impression that if I declared

  $form[$element]['#theme'] = 'foo'

that the FAPI's drupal_render() would directly call

  $html_fragment = them_foo($form[$element])

I understand now that FAPI is using theme(.....) internally rather than
making the direct call. For some reason I just never made the connection
that the FAPI was relying on the actual theme layer rather than just
looking for the function directly.

Are the naming conventions for the functions still the same now? IOW,
should my theming function begin with "theme_my_module_...." or just
with "my_module_.....", as a D6 convention? And is the naming convention
the same for theme functions intended to be called by FAPI as it would
be if the same function were actually part of a theme package?

I want to be sure that by creating module "foo" I am not conflicting
with theme "foo" if someone else were to create that (unlikely, but
better to be safe than sorry, and I want to be a good Drupal citizen).

Thanks again for all the help on this, from all who have replied.

Scott
>
--
Syscrusher <syscrusher@...>


Re: Question on #theme attribute in D6 FAPI

by Earnie :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Quoting Syscrusher <syscrusher@...>:

>
> Are the naming conventions for the functions still the same now? IOW,
> should my theming function begin with "theme_my_module_...." or just
> with "my_module_.....", as a D6 convention? And is the naming convention
> the same for theme functions intended to be called by FAPI as it would
> be if the same function were actually part of a theme package?
>

Maybe you should check the documentation:
http://api.drupal.org/api/function/hook_theme/6

Earnie -- http://for-my-kids.com/
-- http://give-me-an-offer.com/


Re: [SOLVED] Question on #theme attribute in D6 FAPI

by Syscrusher-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Thu, 2008-07-10 at 05:32 -0400, Syscrusher wrote:
> On Thu, 2008-07-10 at 02:30 -0400, Darrel O'Pry wrote:
> > FAPI and the theme layer are not the same... A theme function must
> be
> > declared in hook_theme() in order for theme() to call it. FAPI
> simply
> > calls  theme() with the $element.
>
> Ahhhhhh....that helps a LOT.

And indeed it did. I have the function call working perfectly now. For
the benefit of others new to D6 and facing the form-theming issue, my
code footprint looks like this:

In the form generation:

$form['my_module']['#theme'] = 'my_module_form';

Implementing hook_theme():

function my_module_theme() {
  return array(
    'my_module_form' => array(
      'arguments' => array('element' => NULL),
    ),
  );
}

Finally, the actual theme function signature:

function theme_my_module_form($form)

What I was missing was the hook_theme() registration and the fact that
the string passed as the #theme value does *not* begin with 'theme_',
rather, that is added by the core as it invokes the theme hook.

Thanks to all who replied.

Scott
--
Syscrusher <syscrusher@...>

LightInTheBox - Buy quality products at wholesale price