-1

I have built a module that adds a new custom type. I want to control the theme of that node type, and use a custom theme page for that node type. I can't find any documentation about this. How can customize the theme/page of that node type?

EDIT: I need to completely rewrite the entire page that shows the node content, discarding the default theme and handling with a custom code what must be shown and how.

EDIT: Based on the suggestion of cyclone, I've added this code:

module name: my_module

node type name: mynode

I have a file called mynode.tpl.php in my module folder with an echo "hello world" to test it

I have added this code to my_module.module:

function my_module_preprocess_node(&$variables) {
  // check so the node is your custom type
  if ($variables['node']->type == 'mynode') {
    // add a template suggestion, the example file here will be
    // called your-custom-template.tpl.php
    $variables['theme_hook_suggestions'][] = 'mynode';
  }
}

but when I load a node, it is still used the default theme.

  • 1
    Do you only want to add css styles for detail page of particular content type? Its not related to site theme? Not enough info – skorzh Apr 12 '16 at 18:43
  • @skorzh I added more details, I hope it is enough, otherwise let me know – Terix Apr 12 '16 at 19:16
  • @Terix - I updated my answer. You must make sure so the module is enabled and also you'll need to flush the cache. – Cyclonecode Apr 13 '16 at 21:53
3

If you need to change site theme, you can try to use some contrib modules, like:
- ThemeKey
- Switchtheme

Or change theme programmatically in custom code with hook_custom_theme():

/**
* Implements hook_custom_theme().
*/
function mymodule_custom_theme() {
  if ($some_condition_is_true) {
    return 'my_theme';
  }
}
| improve this answer | |
  • I cannot use anything else than my module and my code. I have to code the ability to replace my node theme, I will try the programmatic way. – Terix Apr 13 '16 at 8:36
2

If you need to theme only node page, would recommend you to use Panels module, with Panels you could override default node content page of particular type, you can chose layout there and add custom code. But Panels pretty complex module and you have to read Panels documentation to work well with it.
Shortly what should you do:
- Enable Panels and Page manager modules.
- Go to Pages: /admin/structure/pages.
- Enable Node template page and edit it/.
- Add new variant of type Panels.
- Chose layout.
- Click Continue then Create variant.
- Go to Selections rules tab, add node type as criteria and chose your content type - so your page will work only on your content type.
- Go back to Content tab and add content panes to regions.
I would recommend to read/watch some tutorials before working with Panels.

| improve this answer | |
  • I cannot use anything else than my module and my code. I have to code the ability to replace my node theme – Terix Apr 13 '16 at 7:40
2

I'm not sure I understand the question. You can use template_preprocess_node() to add your own template:

function <module>_preprocess_node(&$variables) {
  // check so the node is your custom type
  if ($variables['node']->type == 'your-custom-type') {
    // add a template suggestion, the example file here will be
    // called your-custom-template.tpl.php
    $variables['theme_hook_suggestions'][] = 'you-custom-template';
  }
}

The above code should be placed in your .module file and of course you will need to change <module> in the above example to the actual name of your own module.

You can also follow the above link to see some examples on how to implement this in your custom module.

Notice that you also need to flush the cache whenever you add something new into your module file in order for the changes to take effect.

| improve this answer | |
  • What is that I could clarify to you? I'll try to explain better: My drupal has a default theme. My custom type needs a specific custom, different, theme than the default one, that is shown only for my custom type node, and that completely overrides and rewrites the whole html code page from <html> to </html> – Terix Apr 13 '16 at 7:45
  • where I am supposed to add this code? To the .module file? I've done it but it does not work. How could I debug it? – Terix Apr 13 '16 at 8:06
  • 1
    @Terix You need to replace <module> with your module name, 'your-custom-type' with the machine name of your content type, and 'your-custom-template' with your template name. Also, if you are editing an enabled module, you need to first disable it, then edit its code. – kiamlaluno Apr 13 '16 at 8:17
  • @kiamlaluno I've added the code I wrote as update to the question. Does it have to go to the .install file or the . module file? – Terix Apr 13 '16 at 9:06
  • 1
    @Terix It goes in the .module file. – kiamlaluno Apr 13 '16 at 9:11
0

I was able to customize my node theme with this code. So far is the best result I got. I hope I will be able to reach a full page theme customization somehow.

/**
 * Implements hook_theme().
 */
function my_module_theme($existing, $type, $theme, $path) {
  $theme = array();
  $path = drupal_get_path('module', 'my_module');
  debug($path,'path',TRUE);
  drupal_set_message('path: '.$path);
  dvm($path,'path'); 
  $theme['node__mynode'] = array(
    'render element' => 'content',
    'base hook' => 'node',
    'template' => 'node--mynode',
    'path' => $path  . '/templates',
   );
  return $theme;
}
| improve this answer | |

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.