Drupal 7: how to add template file to /node/add/content-type - drupal

In Drupal 7.
I want to themming /node/add/content-type by template file as page--node--add--content-type.tpl.php.
Please help me.

You need to use
page--node--add--content_type.tpl.php
instead of
page--node--add--content-type.tpl.php
underscore(_) instead of dash(-)

You will need to add the template as a suggestion in a preprocess function or you can create a custom theme function for the form, that will use the desired template. This is much similar to how it was done in Drupal 6, only you can do form_alters in your theme now.

you can add this function to your template.php:
you can print_r() your form and see what are the fields.
function yourThemeName_form_yourFormID_alter(&$form, &$form_state, $form_id) {
// Modification for the form with the given form ID goes here. For example, if
// FORM_ID is "user_register_form" this code would run only on the user
// registration form.
print_r($form);
}

You are adding tpl file for node form. For this you have to define tpl file name in template.php under your theme.
First create your_theme_name_theme()
Print content on tpl form.
Example: In below example contact_site_form is the form id of content type.
your_theme_name_theme() {
$items['contact_site_form'] = array(
'render element' => 'form',
'path' => drupal_get_path('theme', 'your_theme_name') . '/templates',
'template' => 'contact-site-form',
);
}
now create a file contact-site-form.tpl.php in your path location as specify above.
<?php echo render($form['name']); ?>
<?php echo render($form['mail']); ?>
.
.
.
<?php print drupal_render_children($form); ?>
You can render each element separately in given method.

Related

Customize drupal search result page

I have created a customized theme called "test", and I am trying to display the search results.
I added the function test_preprocess_search_results() to the template.php (copied the code from drupal page "function template_preprocess_search_results", then copy the search-result.tpl.php from search module to the "test"'s template folder.
function test_preprocess_search_results(&$variables) {
$variables['search_results'] = '';
if (!empty($variables['module'])) {
$variables['module'] = check_plain($variables['module']);
}
foreach ($variables['results'] as $result) {
$variables['search_results'] .= theme('search_result', array('result' => $result, 'module' => $variables['module']));
}
$variables['pager'] = theme('pager', array('tags' => NULL));
$variables['theme_hook_suggestions'][] = 'search_results__' . $variables['module'];
}
I am new in drupal, my concern is how can I make the search result display in some certain div which claimed in my page.tpl.php file? Do something like <?php print render($page['search_result']); ?> in page.tpl.php div ? I am not sure how does the default theme know where to display the search result? Can anyone help thanks
ps: after what I did, and refresh the cache, nothing show up
For search results you should use the search-results.tpl.php (all results list) and search-result.tpl.php (single list item) templates. Copy them from ROOT/modules/search folder into your theme folder and override them as you prefer.

WordPress, pass variable from page.php to header.php

I try to create a theme in WordPress, and allow the user for some Page Templates, to load either a Slide-show in the header or to display the title.
Lets say, I have a template name called, Portfolio and another page template called Portfolio with Slide-show In Head.
Can I from within the portfolio.php and portfolio-with-slide.php to send variables in the header.php in order to decide what to display, or have I to create a second header for the second option and load the one need it into the template file with get_header('title') and get_header('slide')
What is the best approach ?
I personally use the second option - create a second header for the second option and load the one need it into the template file with get_header('title') and get_header('slide').
This is the best approach in terms of code maintainability.
A proper solution is to write a filter to replace the title:
function this_is_the_title_now( $title ) {
// can return un-altered $title or can use fancy logic here
return( "This is the new title." );
}
add_filter( 'the_title', 'this_is_the_title_now', 10, 2 );
This can be put into functions.php of your theme, or in page-whatever.php.
since wordpress 5.5 get_header has $args parameter ready to use:
https://developer.wordpress.org/reference/functions/get_header/
You can just put your arguments into get_header like this:
get_header( 'yourheadername', [ 'header_arg' => 'XYZ' ] ); (if you're using customized header.php file, in this case would it be: header-yourheadername.php)
or for default header.php file:
get_header( null, [ 'header_arg' => 'XYZ' ] );
then inside your header file you can use:
<?php echo $args['header_arg']; ?>
inside if or whatever you want :-)
You can use set_query_var to send variables to your header.php file, or any template part file.
For example, in your Portfolio Slideshow template (portfolio-with-slide.php) file:
//portfolio-with-slide.php
set_query_var('includeSlideshow', true);
Then in your header.php file (or a template part file):
//Header.php
$slideshowHeader = get_query_var('includeSlideShow');
//If variable exists and is boolean true
if($slideshowHeader !== null && $slideshowHeader === true) {
//code to include slideshow in header
}

How to add warning text in Drupal comment form

I want a warning message displayed in the comment form when people try to add comments:
"Please write comments in correct
grammatical English, otherwise they
will not published"
How can I do it?
Here is how you can do it by using hook_form_alter in your own module:
function mymodule_form_alter(&$form, &$form_state, $form_id) {
switch ($form_id) {
case "comment_form":
$form['#prefix'] .= "<div><p>Show some text before the comment form.</p></div>";
break;
}
}
You can alter the comment form so that your guidelines are added to it. There are a handful of ways to alter forms in Drupal. You can do it in your theme's template.php file (which I prefer for simple changes) or in a custom module. This article describes both methods, in Drupal 5 and 6, however not for the form you're interested in. However, the method used is the same that leads to the solution below. This is how you can make the change via template.php:
The following PHP code can be added to your theme's template.php file:
function YOURTHEME_theme() {
return array(
'comment_form' => array(
'arguments' => array('form' => NULL),
),
);
}
function YOURTHEME_comment_form($form) {
$output = '';
$output .= '<div class="comment-help">' . t('Please write comments in correct grammatical English, otherwise they will not published.') . '</div>';
$output .= drupal_render($form);
return $output;
}
Replace YOURTHEME with the name of your theme. If you already have a YOURTHEME_theme function you will need to add the 'comment_form' key to the array it is already returning. I doubt you do, but it's worth mentioning just in case.
A note: you should not be editing any of the themes in /themes, but you should have made a new theme or copied and renamed any of those themes into /sites/default/themes or /sites/all/themes.
The above code is based on code from this page.
Once you are inside a hook_form_alter function you can use the Development module (http://drupal.org/project/devel) dpm() function in place of var_dump to help view and isolate which properties to change in the big form arrays. I find this is a must-have when trying to figure out changes to an existing form. It puts all the elements of the form array into clickable rows.
In Drupal 7 go to
admin/structure/types/manage/mycontenttype/comment/fields/comment_body
There you can add your text. It will be shown below the field as usual. If you want the warning displayed above the field, you'd have to go the form_alter way.

Programmatic Views in Drupal 7

I'm trying to create two views.
View-1 is a list of nodes.
View-2 is an image gallery associated with each node.
I basically want to pass the node title from View-1 to a programmatic View-2, so that each row in View-1 will load View-2(with a result set filtered by the title of View-1!).
I'm confused about the approach. Should this happen in a custom module, preprocess functions, or some combination thereof?
I run into this a lot - wanting to pass an argument from a primary view to a secondary view that displays with each result.
I realize that the question is a bit general, but I'm curious how folks with more experience would approach this problem.
I've done this before on D6 where basically I just create a couple template tpl.php files for my View-1.
Inside my View-1 template for Display Output (views-view--default.tpl.php in D7 now)
I would simply programmatically find the value passed or returned by View-1 for this row.
In your case on each row you would check to see which node is returned by View-1 and then I'd add code in my View-1 template to programmatically load View-2 based on the current View-1 row (ie. node in your case.)
Make sense? 5 months late on the response but I was looking for a refresher and seeing if there's a better way to do this now in D7.
UPDATE:Just did this on my new D7 install. As an example I'll explain how it relates to my Ubercart implementation.
Ubercart, when installed, has it's main "home" shop page located at mysite.com/catalog
This page, when loaded, calls a View created by Ubercart called uc_catalog_terms. It is a taxonomy based view and all it does is grab all your Catalog taxonomy categories and render them.
E.g
As a clothing store, when you navigate to mysite.com/catalog, all you'll see at this page is a grid structure like:
Sweaters Shirts Jeans
My requirement was that I needed to show the shop catalog categories/terms on this page, but ALSO show 3 random products (images) from that category/term below it each catalog category.
E.g
Sweaters
Random Sweater #1 - Random Sweater #2 - Random Sweater #3
Jeans
Random Jean #1 - Random Jean #2 - Random Jean #3
How is this accomplished?
I created my own brand new custom view (no page or lock, just default) which grabs 3 random product images based on a taxonomy term ID argument and renders 3 linked product images. I'll call this custom view random_catalog_items. If 15 is the term ID for Sweaters, when this view is called with the argument 15 it will only render 3 random linked sweater product images.
I now went back to uc_catalog_terms view and created a views-view-fields--uc-catalog-terms.tpl.php (Row Style Output) template file.
THE DEFAULT VIEW VERSION OF THIS FILE (BEFORE MODIFICATION) IS:
<?php foreach ($fields as $id => $field): ?>
<?php if (!empty($field->separator)): ?>
<?php print $field->separator; ?>
<?php endif; ?>
<?php print $field->wrapper_prefix; ?>
<?php print $field->label_html; ?>
<?php print $field->content; ?>
<?php print $field->wrapper_suffix; ?>
<?php endforeach; ?>
THE MODIFIED VERSION BECOMES:
<?php foreach ($fields as $id => $field): ?>
<?php if (!empty($field->separator)): ?>
<?php print $field->separator; ?>
<?php endif; ?>
<?php print $field->wrapper_prefix; ?>
<?php print $field->label_html; ?>
<?php
$title = str_replace('/','-',strtolower($field->raw));
print '<img src="'.drupal_get_path('theme','my_theme').'/images/catalog/'.$title.'-header.png" />';
print '<hr style="width: 100%; background: #000; height: 2px; margin-top: 3px;"/>';
// get the taxonomy term ID
$tid = $row->tid;
// render the 3 random items
if ($random_products = views_get_view('random_catalog_items' )) {
print $random_products->execute_display('default', array($tid));
}
?>
<?php print $field->wrapper_suffix; ?>
<?php endforeach; ?>
So as you can see inside the first View, for every row that is rendered I get the current taxonomy term ID being shown through the available row result object - $row->tid and then I simply call my created view for each row, passing along this Term ID as the argument for it. I leave a lot of the default code in there but inside my view configurations the LABELS and such are set to HIDDEN so they don't even render anyway.
In your case it should be very easily adaptable to just pass a Node NID instead of a Taxonomy Term ID.
VOILA IT ALL WORKS! View within a View! Hope this helps :)
It helps to have the Devel module loaded since then inside these View templates you can debug and see what variables are available to you via something like print krumo($row).
Personally I would avoid views here alltogether.
A simple module using a hook_menu to define the menu-items and two simple menu-callback-functions dealing with the required parameters.
The alternative would be to make all the custom parameters and custom query-filtering and tables known to views.
My (pseronal) rule of thumb is:
If you are certain you will re-use the code several times in future projects a view-addon is worth the investment.
If you will use the views-interface for more then one-time-setup (the general use) e.g. define or change views in an editors/webmasters workflow this makes sense.
The basics of this is really simple and most probably a lot less coding and development then writing the views extensions.
/** Implementation of hook_menu().
*/
function gallery_menu() {
$items = array();
$items['gallery'] = array(
'title' => 'Gallery',
'page callback' => '_gallery_list',
'access arguments' => array('access content'),
);
$items['gallery/%gallery'] = array(
'title' => 'For dynamic titles, see title_callback documentation',
'page callback' => '_gallery_view',
'access arguments' => array('access content'),
);
return $items;
}
/** Load a gallery from database. Name follows %parameter_load hook.
*/
function gallery_load($id) {
return db_query("SELECT * FROM {galleries} WHERE id = %d", $id);
}
/** Render a list of galleries.
*/
function _gallery_list() {
$html = "";
$galleries = pager_query("SELECT * FROM {galleries}", 10);
foreach($galleries as $gallery) {
$html .= check_plain($gallery->title); //You would actually build vars here and push them to theme layer instead.
}
$html .= theme("pager");
return $html;
}
/** Load a gallery from database. Name follows %parameter_load hook.
*/
function gallery_load($id) {
return db_query("SELECT * FROM {galleries} WHERE id = %d", $id);
}
/** Render a list of galleries.
*/
function _gallery_view($gallery) {
$html = "";
$images = pager_query("SELECT * FROM {images} WHERE gallery_id = %d", 10, $gallery->id);
foreach($images as $image) {
$html .= check_plain($image->title); //You would actually build vars here and push them to theme layer instead.
}
$html .= theme("pager");
return $html;
}
Obviously, as stated in the comments, you would additionally create a few theme functions to handle the rendering, to 1) avoid hardcoded spagetty-HTML all over your module and b) allow the frontenders to stay in their theme when creating the HTML.
This sounds like a good opportunity to use an ajax callback. You could have your primary view on a part of the page just like normal and a secondary view in a custom block or something. when focus lands on the primary item (in the form of a button click or hover or something) you can use an ajax callback to replace the content of your custom block with the secondary view using your argument.
are you using drupal 6 or 7 for this? my understanding is that they do this is different ways.

Register your theme functions in Drupal

I am new to Drupal and I am working on creating my own theme for our custom module. Can any one please help me understand how to register our theme functions or share any ideas or links that explains the process from scratch.
Register a theme function means implementing hook_theme in your module.
For example, if your module is called "example", then you need to have a function called example_theme in the example.module file. The theme function must return an array or you'll end up with the famous white screen of death.
In example.module:
<?php
// $Id$
// Implements hook_theme
function example_theme(){
return array(
'mydata' => array(
// Optionally, you can make the theme use a template file:
// this line references the file "mydatafile.tpl.php" in the same folder as the module or in the folder of the active theme
'template' => 'mydatafile',
// these variables will appear in the template as $var1 and $var2
'arguments' => array(
'var1' => null,
'var2' => null,
),
),
'myotherdata' => array(
// these variables will appear in the functions as the first and second arguments
'arguments' => array(
'var1' => null,
'var2' => null,
),
)
);
}
// If you don't want to use a template file, use a function called "theme_THEID" to create the HTML.
function theme_myotherdata($var1, $var2){
return "<div>var1= $var1 and var2= $var2</div>";
}
In mydatafile.tpl.php:
<div>mydatafile.tpl.php was called</div>
<ol>
<li>var1: <?php echo $var1; ?></li>
<li>var2: <?php echo $var2; ?></li>
</ol>
You can then later call the theme function manually if needed:
$html = theme('mydata', 'hello world', 123);
$html = theme('myotherdata', 'hello world', 123);
In which case "mydatafile.tpl.php" and "theme_myotherdata" will receive the value "hello world" in $var1 and the value 123 in $var2.
There are many more options, like changing the name of the function, using patterns instead of a fixed name, being able to have the function in another php file or such, check out the link.
Here are a couple more ressources about theming:
The theme guide
Overriding themable output
The Devel module
The Theme developer module (requires the Devel module)
a longuer example that also creates a form/page to use the theme function
By the way, you will need to rebuild the theme registry cache if you add the functions in the .module file after it has been installed, in which case you can do so by clearing the cache (one way is using the button at the bottom of the the Performances page).
NOTE: Slightly different syntax with Drupal 7 (ex. 'arguments' changes to 'variables')
http://www.davidcalculli.com/blog/2012/01/drupal-7-custom-template-only-displaying-the-first-character

Resources