How to display custom html block in Drupal using theme? - drupal

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.

Related

Drupal - using views_embed_view with an external Drupal site

I have two Drupal sites hosted on different servers. In the main they need to operate separately however site1 has one specific content type which I'd like to show in a list on site2.
I can't simply use feeds to import as the original has to remain and if edited, the changes be instantly reflected on both sites. Site2 has no requirement to edit the content - only show it.
The content is already being presented in a list on site1. The list was created using views.
My intention was to call the view on site2 using the following code in a custom module.
function site2_menu() {
$items = array();
$items['content-from-site1'] = array(
'title' => 'Content from Site1',
'page callback' => 'site_two_list',
'access arguments' => array('access content'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
function site2_list() {
db_set_active('site1');
$content = views_embed_view('articles', 'default');
db_set_active('default');
return $content;
}
site1 DB is defined in the settings.php file.
However this isn't returning any data. Using the same approach on Site1 (without switching DBs) works fine.
Was I being over-optimistic in hoping this approach would work or am I missing something obvious?
If this isn't likely to work, what would the alternative be? I can do my own SQL query, but I'd prefer to use views for built in arguments, pagination, templates, etc.
Thanks.
I've not been able to use views_embed_view, however I have been able to use views_get_view to retrieve everything I need and iterate over the results myself. It's a very close second.
function site2_menu() {
$items = array();
$items['content-from-site1'] = array(
'title' => 'Content from Site1',
'page callback' => 'theme_site2_list',
'access arguments' => array('access content'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
function site2_theme() {
return array (
'site2_list_page' => array(
'arguments' => array('content' => NULL),
'template' => 'templates/site2-list-page'
)
);
}
function theme_site2_list() {
db_set_active('site1');
$view = views_get_view('articles');
$view->base_database = "site1";
$view->init_display();
$view->pre_execute();
$view->execute();
db_set_active('default');
foreach ($view->result as $key => $data) {
$content[$key]['nid'] => $data->nid;
$content[$key]['title'] => $data->node_title;
$content[$key]['body'] => $data->field_body;
$content[$key]['image'] => $data->field_field_image;
}
return theme('site2_list_page', array('content' => $content));
}
Then in site2-list-page.tpl.php I can use the $content array to do what I need. It's not quite as clean as a simple views_embed_view, but it's a close second and allows the content from one site to be pulled to another fairly easily.

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

Only partial theming of custom form

I've constructed a custom module to create a form. Now I'm stuck on the theming. I already have a CSS stylesheet for the form, since my company is part of the government and they have a preset branding. So I wanted to change the HTML used by the default form theme functions of Drupal thus implementing the correct style.
But only the form-tag of the form gets rendered. The fieldset and elements are not rendered. When the theme functions are removed the default theming kicks in and the form renders normally (but of course without the requested theming).
What I have tried so far:
Added a hook_theme function to add theme functions
function publicatieaanvraagformulier_theme() {
return array(
'publicatieaanvraagformulier_form' => array(
'arguments' => array("element" => NULL)
),
'publicatieaanvraagformulier_fieldset' => array(
'arguments' => array("element" => NULL)
),
'publicatieaanvraagformulier_form_element' => array(
'arguments' => array(
"element" => NULL,
"value" => NULL
)
)
);
}
Added ['#theme'] to the form-element, fieldset-element and the form-elements
$form['#theme'] = "publicatieaanvraagformulier_form";
$form['groep'] = array(
'#title' => t("Please fill in your details"),
'#type' => "fieldset",
'#theme' => "publicatieaanvraagformulier_fieldset"
);
$form['groep']['organisatie'] = array(
'#title' => t("Organization"),
'#type' => "textfield",
'#attributes' => array("class" => "text"),
'#theme' => "publicatieaanvraagformulier_form_element"
);
Added the actual theme function based on the default ones in form.inc
function theme_publicatieaanvraagformulier_form($element) {
function theme_publicatieaanvraagformulier_fieldset($element)
function theme_publicatieaanvraagformulier_form_element($element, $value)
I haven't included the code of these functions because even with the default themefunctions code, they don't work. Therefor I assume they are not the source of the problem.
The form is called
//Get the form
$form = drupal_get_form('publicatieaanvraagformulier');
//Add messages
$errors = form_get_errors();
if (!empty($errors)) {
$output .= theme("status_messages","error");
}
//Show form
$output .= $form;
return $output;
I haven't found similar 'complicated' examples of theming a form, but have pieced together the former from books and online searches.
Hopefully someone has an answer to this problem (point out the mistake I made).
Greetings
Jeroen

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;
}

returning untemplated output in drupal from menu callback function

I have a drupal module with a function that returns an attachment text/plain,
function mymodule_menu() {
$items = array();
$items[MY_PATH] = array(
'title' => 'some page',
'page callback' => 'myfunction',
'type' => MENU_CALLBACK,
);
}
function myfunction()
{
drupal_set_header('Content-Type: text/plain');
return "some text";
}
But it returns the page in the page.tpl.php template, however I want it untemplated, how do I over-ride the theme to make it return plain text?
Thanks,
Tom
This will return plain text
function myfunction() {
drupal_set_header('Content-Type: text/plain');
print "some text";
exit(0);
}
Alternatively you can use the 'delivery callback' setting in your menu callback definition. Now your page callback function will be run through a custom function that just prints and exits, rather than calling drupal_deliver_html_page(), which is what outputs all the typical theme markup, etc.
function mymodule_menu() {
$items = array();
$items['MY_PATH'] = array(
'title' => 'some page',
'page callback' => 'myfunction',
'type' => MENU_CALLBACK,
'delivery callback' => 'mymodule_deliver_page',
);
return $items;
}
function mymodule_deliver_page($page_callback_result) {
print $page_callback_result;
exit(0);
}
The best and simplest solution is to just have your callback print your html and return nothing.
For example,
// Hooks menu to insert new url for drupal
function MYMODULE_menu() {
$items = array();
$items['member-stats.php'] = array(
'page callback' => '_MYMODULE_menu_callback',
'access callback' => TRUE,
);
return $items;
}
// Callback prints and doesn't return anything
function _MYMODULE_menu_callback() {
print "Hello, world";
}
if you'd create a template like html--barebones.tpl.php, containing just
<?php
drupal_set_header('Content-Type: text/plain');
print $barebones;
?>
you could hook that template to YOURTHEME_preprocess_html(), like so:
function YOURTHEME_preprocess_html(&$variables) {
if (array_key_exists('barebones',$_REQUEST)) {
$variables['barebones'] = $variables['page']['foo']['bar'];
$variables['theme_hook_suggestions'][] = 'html__barebones';
}
}
now, if you call your page with the additional query ?barebones, like drupal/foo/bar?barebones, it will return the barebones version.
theres a tricky bit in getting your result back. var_dump($variables['page']) to see where drupal left your text. Its been tucked inside the render array surrounded by all kind of info used to render the text, which you are not using. Making me wonder if it wouldnt be more efficient to just print it and exit() inside myfunction :-)
Your module can define template files (reference):
<?php
function mymodul_preprocess_page(&$variables) {
foreach ($variables['template_files'] as $file) {
$template_files[] = $file;
if ($file == 'page-node') {
$template_files[] = 'page-'. $variables['node']->type;
}
}
$variables['template_files'] = $template_files;
}
?>
By creating a new .tpl.php file for the page in question. E.g.
page-module.tpl.php
page-module.tpl.php would only need to be a simple page, e.g.
<?php
print $content;
?>

Resources