how to implement hook_theme drupal 8? - drupal

Im new to drupal,I need to render a form so i have to implement hook theme, my confusion is Under which directory I should create hook theme file in drupal 8?
// my_module.module
function custom_module_theme($existing, $type, $theme, $path) {
return array(
'customize_form' => array(
'variables' => array(
'Custom_Form' => NULL
),
'render element' => 'form'
),
);
}
where I have to put above file in drupal 8??
Thanks in advance.

In your .module file
File location - module/custom/MODULENAME/MODULENAME.module
/**
* #file
* Twig template for render content
*/
function MODULENAME_theme($existing, $type, $theme, $path) {
return [
'theme_name_template' => [
'variables' => ['flag' => NULL],
],
];
}
To Use theme function use below code
return ['#theme' => 'theme_name_template', '#flag' => 1];

If i got it right you want the folder to place your module, right? You have to put your module in a folder under
/modules/custom/your_module_folder or /sites/all/modules/your_module_folder

Related

Drupal 6 how do page-customnamehere.tpl.php work

I'm working with a drupal 6.26 install. I can see file names like page-2011-custom-landing-page.tpl.php in the theme directory I'm using.
From what I understand, I should be able to see this template at http://www.mydomain.com/2011-custom-landing-page however I just get a 'page not found' message at that address. What's going on?
If you see a file name as 'page-2011-custom-landing-page.tpl.php' in you theme folder it means that there is a template file named 'page-2011-custom-landing-page.tpl.php' using for a page. That page may be defined in one of your custom module.
Like this:
<?php
/**
* Implements hook_menu().
*/
function custommodulename_menu() {
$items['pathname'] = array(
'title' => 'title',
'page callback' => 'custommodulename_pagename',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implements hook_theme().
* Adds our theme specificiations to the Theme Registry.
*/
function custommodulename_theme($existing, $type, $theme, $path) {
$items = array();
$items['custommodulename_pagename_page'] = array(
'render element' => 'form',
'template' => 'page-2011-custom-landing-page', //name of file(template) to be created,here create page-2011-custom-landing-page.tpl.php in the custom module folder
);
return $items;
}
/**
* Callback function(menu)
*/
function custommodulename_pagename(){
return theme('custommodulename_pagename_page');
}
?>
page-2011-custom-landing-page is not a url, it is a template name. You can see the content inside the template my accessing the menu callback that using that template. (here it is : http://yoursite.com/pathname)
Reference : http://www.developerdoc.com/answer/add-template-menu-call-back

How to display custom html block in Drupal using theme?

Having some module, defined some url in hook_menu() and need to display there some theme (modules/mymodule/templates/mytheme.tpl.php).
How do I show mytheme.tpl.php content on needed url?
function mymodule_menu(){
$item = array();
$item['somemenu'] = array(
'page callback' => 'somemenu_display',
);
return $item;
}
function somemenu_display(){
return WHAT_IS_THIS_FUNCTION('modules/mymodule/templates/mytheme.tpl.php');
}
And it will be good to display only these contents, without and header/footer.
The function is Theme()
return theme('some_theme_function_template', array('aValues' => $someArray));
You then need to use the theme hook like this:
function my_module_name_theme() {
return array(
'some_theme_function_template' => array(
'template' => 'mytheme',
),
);
}
It now searches for mytheme.tpl.php in the root of your module.

Theming Module Output

What I am trying to do is generate some raw output within a module.
I would like to pass an array of data through to a template file, and then use that data to populate the code from the template. The template is represented by a file in my theme folder.
I have a hook set up for a certain URL (/itunes):
$items['itunes'] = array(
'page callback' => 'itunespromo_buildpage',
'type' => MENU_SUGGESTED_ITEM,
'access arguments' => array('access content'),
);
..inside itunespromo_buildpage...
function itunespromo_buildpage() {
//grab some data to pass through to template file, put into $promo_data
$details = theme('itunes_page', array(
'promo_data' => $promo_data,
));
return $details;
}
Here is the hook_theme():
function itunespromo_theme() {
return array(
'itunes_page' => array(
'template' => 'itunes_page',
),
);
}
Inside my theme's template.php:
function geddystyle_itunes_page($vars) {
return print_r($vars['promo_data'], true);
}
Right now, $promo_data is being passed through fine, and it is print_r'd on to the result page. However, I'd like to then take this $promo_data variable and use it in my itunes_page.tpl.php template file.
I'm kind of certain I'm close here. Am I supposed to call some sort of render function and pass the $promo_data variable to it from function itunespromo_theme()?
I believe you just need to update your hook_theme() to provide the ability to send variables to your template file.
Something like this should do the trick:
function itunespromo_theme($existing, $type, $theme, $path) {
return array(
'itunes_page' => array(
'variables' => array(
'promo_data' => NULL,
),
'template' => 'itunes_page',
)
);
}
Also, instead of calling the theme() function directly what you want to be doing is actually constructing a renderable array and letting Drupal call the theme() function. What you should be doing is calling drupal_render which in turn calls theme() for you. Look at this piece of advice here for a little more clarity:
http://drupal.org/node/1351674#comment-5288046
In your case you would change your function itunespromo_buildpage to look something like this:
function itunespromo_buildpage() {
//grab some data to pass through to template file, put into $promo_data
$output = array(
'#theme' => 'itunes_page',
'#promo_data' => $promo_data //call $promo_data from the tpl.php page to access the variable
);
$details = drupal_render($output);
return $details;
}

Suggesting different templates when theming a node form

function posts_theme($existing, $type, $theme, $path) {
return array(
'post_node_form' => array(
'arguments' => array('form' => NULL),
'template' => VARIABLE,
)
);
}
This is the way of suggesting a template to render the 'post_node_form' in Drupal 6. BUT I want to get the node editing form from 2 different paths:
via AJAX through drupal_get_form('post_node_form')
via default node/add/post
If I replace "VARIABLE" depending on the path (or whatever other condition), it will not work because it seems? the name of the template is cached and you need to flush caches to refresh it.
Any solution of suggesting different form templates?
NOTE. This is not the case of node template, (then you can put the template suggestions in the preprocess hooks). It's about node FORM.
Add this function/or modify if exists into template.php of your theme:
function phptemplate_preprocess_page(&$vars) {
// ...
$node = menu_get_object();
if ($node->type == 'post') {
$vars['template_files'][] = VARIABLE;
}
// ...
}
Ok, I answer my own question:
The key of the solution is the hook preprocess_NAME_OF_MY_FORM , that is executed every page load and can be in your module or your theme.
So in my case, I wrote in my "posts" module:
/**
* Implementation of hook_theme().
*/
function posts_theme($existing, $type, $theme, $path) {
return array(
'post_node_form' => array(
'arguments' => array('form' => NULL),
'template' => 'post-form-custom',
)
);
}
function posts_preprocess_post_node_form(&$vars) {
// I check the path to know if node_form is retrieve through normal way or ajax way.
if (check_plain(arg(0)) == 'node'){
$vars['template_files'][] = 'post-form-default';
}
}
I had in my module folder the files post-form-custom.tpl.php and post-form-default.tpl.php

Can I override a theme function with a .tpl file?

How would I go around overriding a theme function with a .tpl file? I know how to override a .tpl file with a theme function but not the other way round. I can't seem to find anywhere that tells me so, so maybe it's not possible or not good practice.
For example if there was a theme function defined in a module called super_results and registered with the theme registry, like the example below, how would I go around overriding it with super_results.tpl.php.
'super_results' => array(
'arguments' => array('title' => NULL, 'results' => NULL, 'votes' => NULL),
),
function modulename_super_results($title, $results,$votes){ output HTML }
The simplest solution would probably be creating a new theming function that uses a template. Something like that should work, disclaimer code is untested.
function my_theme_theme() {
return array(
'overide' => array(
'template' => 'elm-super_results',
'arguments' => array('title' => NULL, 'results' => NULL, 'votes' => NULL),
),
);
}
function my_theme_super_results($title, $results, $votes) {
return theme('overide', $title, $results, $votes);
}

Resources