6

I'm using a custom theme but Seven as administration theme.

How can I override the theme function 'theme_node_preview'?

Notice that theme_node_preview is called in the node form, and it's shown in an overlay. So it's part of the admnistration theme.

  • I tried to used the generic phptemplate_ prefix but this way of overriding theme functions was removed for Drupal 7.
  • I tried seven_node_preview from a module and also from the template.tpl.php of my custom theme, but didn't work.
  • I don't want to create a subtheme of Seven only for this.
  • Just curious, why the aversion towards creating a subtheme? – Chris Pliakas Jul 19 '11 at 12:18
  • Because I want to encapsulate all the modifications in the module that make changes in the node display for maintainability. Or even in the "public" theme will be fine. But not in the Seven theme. Although Drupal seems not follow this logic, theme_node_preview should be related to "how it will look the node when is viewed in the the public theme". Not "how it looks in the administration theme" – corbacho Jul 19 '11 at 12:32
  • 1
    This is a quirk of Drupal 7. If the user has the "View the administration theme" permission, they will see the admin theme when editing nodes. Otherwise they will see the public theme, as I think is your desire. If you implement hook_admin_paths_alter(&$paths), you can set $paths['node/*/edit'] = FALSE; in which case the public theme will always be used for editing nodes regardless of the "View the administration theme" permission. – Chris Pliakas Jul 19 '11 at 16:27
5

I know you explicitly stated that you don't want to create a subtheme of Seven, however short of hacking the core Seven theme or messing with the theme registry (which I explain below against my better judgement) this is the simplest way to accomplish what you are trying to do. The amount of overhead to do this is incredibly small and requires only three files. Simply create a folder named something like "mytheme" and add an empty "mytheme.css" file in addition to a "mytheme.info" file with the following code:

name = My Theme
description = Custom subtheme of Seven.
core = 7.x
base theme = seven
stylesheets[all][] = mytheme.css

Then add a template.php file and override the theme hook similar to below:

function mytheme_node_preview($variables) {
  // Do custom stuff here.
}

All done. This is the "Drupal way" of doing theme hook overrides.

Again, it is against my better judgement to post the code below. If you absolutely have your mind set on not creating a subtheme, you can implement hook_theme_registry_alter(). Did I mention that this solution is hacky and the hook is pure evil? :-) In a custom module named "mymodule", do the following:

function mymodule_theme_registry_alter(&$registry) {
  $path = drupal_get_path('module', 'mymodule');
  $registry['node_preview']['file'] = 'mymodule.module';
  $registry['node_preview']['theme path'] = $path;
  $registry['node_preview']['function'] = 'mymodule_node_preview';
  $registry['node_preview']['includes'] = array(
    $path . '/mymodule.module'
  );
}

function mymodule_node_preview($variables) {
  // Do whatever you need to do here.
}

Use the snippet above at your own risk.

  • Thanks Chris for this extended answer, maybe is useful for others who find this Question, but I said clearly I don't want to create a subtheme. And that snippet looks evil as you said :) . I vote you up, but I will wait for a better answer. – corbacho Jul 18 '11 at 21:35
  • Thanks for the response. I can't think of any other methods to integrate with the theme system other than the 3 methods above, would be curious to see if there are any other creative ways to attack this. – Chris Pliakas Jul 19 '11 at 1:29
  • Thanks Chris! The registry alter approach is the evil, kludgy way. I went with a custom sub-theme approach and everything "just works" as it should. – jwal Sep 16 '11 at 21:22

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.