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

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

Related

how to implement hook_theme drupal 8?

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

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.

Render node without theme from a module (no theme editing)

I have a module that is being used to create only a few page nodes (done in .install). That's working fine. The problem is that these nodes contain xml, json, jsonp content, so I want to be able to render them without the theme, no header, no footer, no styling, just node->content.
This module is going to be shared with several other Drupal sites so I can't do this with theme development, I don't want anyone to have to create or modify templates.
Is there a way to do this using a hook from within the module, the .module? Basically detect the node title or node alias (or something) and then prevent the theme from rendering and only render the content. I'll know the titles and aliases of the nodes because I'm creating them in the .install.
I would also like to modify the headers to correctly to say tell whats being returned is xml, json, etc.
Thanks in advance.
Normally, I'd go about this another way. I'd define the content through hook_menu() menu router items rather than as node content, as it's rarely intended to be directly user-editable. If there is a lot of processing, you can separate it from the .module and include it as a file for each item.
/**
* Implementation of hook_menu().
*/
function MODULE_menu() {
$items = array();
$items['example/json'] = array(
'title' => 'JSON example',
'page callback' => '_MODULE_json',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['example/xml'] = array(
'title' => 'XML example',
'page callback' => '_MODULE_xml',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* JSON example.
*/
function _MODULE_json($string = '') {
$data = array();
$data['something'] = 0;
$data['anotherthing'] = 1;
drupal_json($data);
}
/**
* XML example. No idea if this actually produces valid XML,
* but you get the idea.
*/
function _MODULE_xml($string = '') {
$data = array();
$data['different'] = 2;
$data['evenmore'] = 3;
// Build XML
$output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
$output .= "<data>\n";
$output .= format_xml_elements($data);
$output .= "</data>\n";
// We are returning XML, so tell the browser.
drupal_set_header('Content-Type: application/xml');
echo $output;
}

simple example form in drupal 7 with everything configured correctly shows "Page not found"..Why..?

I have installed drupal 7 and have been trying to create a custom form. The below code which am trying has been taken from http://drupal.org/node/717722 and I have not made any changes except for .info file.
here is the my_module.info
name = My module
description = Module for form api tutorial
core = 7.x
Below is the my_module.module
<?php
/**
* This function defines the URL to the page created etc.
* See http://api.drupal.org/api/function/hook_menu/6
*/
function my_module_menu() {
$items = array();
$items['my_module/form'] = array(
'title' => t('My form'),
'page callback' => 'my_module_form',
'access arguments' => array('access content'),
'description' => t('My form'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* This function gets called in the browser address bar for:
* "http://yourhost/my_module/form" or
* "http://yourhost/?q=my_module/form". It will generate
* a page with this form on it.
*/
function my_module_form() {
// This form calls the form builder function via the
// drupal_get_form() function which takes the name of this form builder
// function as an argument. It returns the results to display the form.
return drupal_get_form('my_module_my_form');
}
/**
* This function is called the "form builder". It builds the form.
* Notice, it takes one argument, the $form_state
*/
function my_module_my_form($form_state) {
// This is the first form element. It's a textfield with a label, "Name"
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
);
return $form;
}
?>
I have placed these two files in a *my_module* folder and placed it in sites/all/modules
After that, I enabled the module from the modules page without any errors or warnings.
Now, when I try to access this for using the url, localhost/d7/?q=my_module/form
I get a "Page not found " error..!! Why..?? What am I missing..?
Its not only for this module but also for this examples for developers module http://drupal.org/project/examples. It shows the same error.
You should write:
$items['my_module']
Where my_module is module name.
And you need to create page-my_module_my_form.tpl.php file at
sites/all/theme/your_theme/template/page-my_module_my_form.tpl.php
and in this file add code like this:
<?php
if (isset($form['submission_info']) || isset($form['navigation'])) {
print drupal_render($form['navigation']);
print drupal_render($form['submission_info']);
}
print drupal_render($form['submitted']);
?>
<?php print drupal_render_children($form); ?>
and try to run with
localhost/d7/my_module
I hope this will be useful to you
I know this is late, but I do believe that you need to have the $form variable passed into your form, like so : function my_module_my_form($form_state, $form)... That way you actually have a form variable to house your form data.

Drupal 7 - Confirm email address

I have added some extra fields to the standard 'create account' page; notably a 'confirm email' field.
How do hook into the validation so that I can add some custom validation rules of my own (e.g. to check the two emails match)?
I have found hook_user_presave, but am unsure on how to code it or where I should put it.
Any and all help appreciated.
I would advise installing the LoginToboggan module, it actually has the option for that exact functionality out of the box and has a bunch of other useful options as well.
If you want to do it yourself though you'd probably be better off implementing hook_form_FORM_ID_alter() and adding a validation function directly to the registration form:
function mymodule_form_user_register_form_alter(&$form, &$form_state, $form_id) {
$form['#validate'][] = 'mymodule_user_register_form_validate';
}
function mymodule_user_register_form_validate(&$form, &$form_state) {
if ($form_state['values']['first_email'] != $form_state['values']['second_email']) {
form_set_error('second_email', 'The email addresses much match.');
}
}
Make sure you clear Drupal's cache once you've implemented the form alter function so Drupal registers it correctly.
Hope that helps.
Here is the example solution for Drupal 7:
/**
* Implements hook_menu().
* Note: You can define your own menu callback optionally.
*/
function foo_menu() {
$items['foo-signup'] = array(
'title' => 'Create new account',
'page callback' => 'drupal_get_form',
'page arguments' => array('user_register_form'),
'access callback' => 'user_register_access',
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function foo_form_user_register_form_alter(&$form, &$form_state, $form_id) {
$form['account']['mail_confirm'] = array(
'#type' => 'textfield',
'#title' => t('Confirm e-mail address'),
'#maxlength' => EMAIL_MAX_LENGTH,
'#description' => t('Please confirm your e-mail address.'),
'#required' => TRUE,
);
$form['#validate'][] = 'foo_user_register_form_validate';
}
/**
* Implements validation callback.
*/
function foo_user_register_form_validate(&$form, &$form_state) {
if ($form_state['values']['mail'] != $form_state['values']['mail_confirm']) {
form_set_error('mail_confirm', 'The email addresses must match.');
}
}

Resources