drupal global variable - drupal

where is the drupal global variable in ,it's say in developer/global.php.but in drupal install file,i can't find this file. what's the difference of the global variable and the avariable variables in page.tpl.php and node.tpl.php...... where is the declaration of the template avariable variables in.thank you

Variables for template files are declared in template preprocess functions. This page in the Drupal theming guide contains a flowchart describing the flow of Drupal's theme() function. For every template, the variables pass every preprocess function that matches the appropriate naming scheme.
For instance, for page.tpl.php, Drupal will first run template_preprocess() and template_preprocess_page(). Next, if some module contains the function somemodule_preprocess_page(), and/or if your custom contains yourtheme_preprocess_page(), those functions will be run as well. Every preprocess function can alter and add variables for the page.tpl.php template. When all preprocess functions have finished, the variables are passed to page.tpl.php.

There is a file called settings.php which may be what you're looking for.
Alternatively, if you'd like the owners of the site to be able to modify the variables without them having to change the source code, you can create a variable in the admin page of one of your modules, which can then be accessed at any point your application using drupal's variable_get() function.

Related

Add variable to each twig template

How can I transfer variable to each twig template?
I need transfer rates to each template in my application.
You do that by using global variables.
How to Inject Variables into all Templates (i.e. Global Variables) (I know, searching the docs is hard...)

Adding a custom themable variable in theme_preprocess_page

I have read the docs over and over trying to wrap my head around this seemingly simple task. Basically, I have a template with a 'skip navigation' div hard-coded in html.tpl.php that I do not want on front-page.
My idea was to set a $vars['skiplink'] variable in theme_preprocess_page. Since this variable contains a few lines of html markup, I was aiming for something as seen in garland theme:
function garland_preprocess_page(&$vars) {
// Move secondary tabs into a separate variable.
$vars['tabs2'] = array(
'#theme' => 'menu_local_tasks',
'#secondary' => $vars['tabs']['#secondary'],
);
<snip>
I would like to have the html in a themable function or even a template, but I cannot even get this snippet to work:
/**
* Override or insert variables into the page template.
*/
function morin_preprocess_page(&$vars) {
// add skiplink markup
$vars['skiplink'] = 'hello world';
}
This generates a notice:
Notice : Undefined variable: skiplink in include() (ligne 14 in /var/dev/morin/www/sites/all/themes/morin/templates/html.tpl.php).
Can anyone slap me with a clue? I would really like to understand how to do this with both methods, ( template & function ). I'm also wondering if this should be done in a module?
I realise there are probably 10 ways to skin this cat, so any insights on pros/cons of methods used are welcome.
Ok I figured out I was using the wrong preprocess function, setting $vars['skiplink'] in preprocess_html is the way to go for top-level variables.I still have yet to figure out in a clear way how to associate this variable to a template file.
The preprocess hooks follow this pattern:
<theme name>_preprocess_<template name>
So if you want to modify the variables for "html.tpl.php" you want to use this hook:
<theme name>_preprocess_html(&$variables)
You are using preprocess_page but you are inserting the variable in the html.tpl.php.
You should either insert it in the page.tpl.php or rename your preprocess function, to add the variable to the html.tpl.php
And remember to clear cache if you didn't have the preprocess function defined already.
Update:
You seem to be missing a key point. Preprocess functions (along with the actual theme call) is how you make variables accessible in templates. Different preprocess functions are called for different templates, (..._page for page.tpl.php etc.)
Do you still have problems after using the correct preprocess function and clearing cache?

drupal-----views(how to know the fields name)

how to know the field variable name when output by views. eg:now, i want to overwrite a field's output, the name which i added is field_hello. but i don't know what's the variable name of it? namely how to print the variable in views-view-field--field_hello.tpl.php
In the Views UI, under "Basic settings", you can click "Theme: Information" to get a list of what template files are currently being used and what file names can be used. You'll find all the default templates within your views/theme/ directory. If you copy one of those to create your custom template, e.g. views-view-field.tpl.php, you'll see they're heavily documented with all the variable names available. For field templates, you have $view, $field, $row, and $output. Depending on whether you want the pre-processed value or the processed value, you probably want $field or $output.
Using Theme Developer (formerly part of Devel), you can inspect a page and see what variables get passed to it and use them in your own templates.
If you talk about using a variable inside a views field: this is not possible due to security issues. Depending on the fields, there are some variables available and Vies tells you which one but you don't have easy access to ALL the variables that your template is able to process.
The simplest solution is to look inside the views module directory, views/theme/ all of these templates are the default view display and contain code that prints the varibles, arrays etc that your view will generate.

Find a list of modules that 'altered' a specific form in Drupal

Is there a trick, addon or patch that allows me to find out what modules altered a specfic form?
A list of all hook_form_alters is not too hard to achieve. But I want to list only those that actually changed my form.
Modules can alter a form trough a generic modulename_form_alter() and modulename_form_FORMID_alter() it would be nice if both are taken into consideration.
The drupal_prepare_form function calls all hook_form_alter functions. In this function, there is no storage for any modules that implement hook_form_alter. However, there is a container ($data) there that pulls all the alter functions then is applied with drupal_alter. Getting this data would require modifying this file (ref: line 543 in /includes/form.inc in Drupal 6.19).

How do I use theme preprocessor functions for my own templates?

I have several .tpl.php files for nodes, CCK fields, and Views theming. These template files have a lot of logic in them to move things around, strip links, create new links, etc. I understand that this is bad development and not "The Drupal Way".
If I understand correctly, "The Drupal Way" is to use preprocessor functions in your template.php file to manipulate variables and add new variables. A few questions about that:
Is there a naming convention for creating a preprocessor function for a specific theme? For example, if I have a CCK field template called content-field-field_transmission_make_model.tpl, how would I name the preprocessor function?
Can I use template preprocessor functions for node templates, CCK field templates, and Views templates? Do they have different methods of modifying template variables or adding new ones?
For a general overview, you should read up on manipulating variables within preprocess functions.
Concerning the naming convention, this is normally pretty simple, but there is a catch for your current example (see below):
A preprocess functions signature needs to be
[yourModuleName|yourThemeName]_preprocess_[themeFunctionName](&$variables)
so implementing one for the page template within a themes template.php file would result in
themeName_preprocess_page(&$variables)
Most of the time the name of the theme function will be the name of the *.tpl.php file, without the .tpl.php ending and with underscores instead of the hyphens. But there is a catch if the template file gets selected on the base of template suggestions, as the preprocess function can only be implemented for the base name, not for the additional suggestions! (The suggestions for alternate template files are added in preprocess functions themselves.)
Your current example is one of those cases, as content-field-field_transmission_make_model.tpl.php is such a suggestion, with the base name being content-field.tpl.php, and the corresponding theme function being content_field. So you would have to implement a preprocess function named yourThemeName_preprocess_content_field(&$variables), and within that inspect the available entries in the $variables array to check if you are actually called for the 'field_transmission_make_model', and not for a completely different CCK field, e.g.:
function yourThemeName_preprocess_content_field(&$variables) {
// Are we called for the right field?
if ('field_transmission_make_model' == $variables['field_name']) {
// Yes, add/manipulate entries within the variables array
$variables['new_entry'] = 'A useless new variable';
$variables['label'] = 'A useless change of the existing label variable';
}
}
(Note: Untested code, beware of typos)
After this, there should be a new variable $new_entry being available in your template file, and the content of the $label variable should have changed (all top level entries within the $variables array will be turned into separate variables for the template file, named after the array index).
As for your second question, the basic usage of preprocess functions is the same for all template files, but be aware:
Preprocess functions are only available for theme calls that use *.tpl.php files, not for theme functions
The content of the $variables array varies heavily, depending on what gets themed
Other modules might implement the preprocess functions as well, and they will be called one after another, so if you want to change something that gets added by another module, you can only do so if your implementation gets called after that (which will be no problem in your case, as implementations within a theme are called after all implementations within modules - just wanted to mention that there can be many implementations at once)
In order to figure out what our preprocessing function should be named, we need to know what template file or theme function some output comes from, and one great way to do this is by using the theme developer module.
Here is a video which explains it in detail - http://buildamodule.com/video/drupal-theming-essentials-template-files-theme-function-overrides-and-preprocessing-functions-how-to-use-simple-preprocessing-functions

Resources