Confused about theme function calls - drupal

I've created a content type that has a CCK text field.
When I select the text field using the Drupal Themer widget it tells me the last function called was
theme_text_formatter_default() , which I found in the CCK text.module
It also tells me that it's parents were;
content-field.tpl.php < theme_markup < theme_markup < node.tpl.php < page.tpl.php
So I assumed that somewhere in the content-field.tpl.php was the function call to theme('text_formatter_default',$element) but it wasn't in there. Just print $item['view'] used to display the content.
I searched all the project files for theme('text_formatter_default',$element) and it doesn't exist. I know it's being called by the theme function as I override it in my template.php and it used my overridden function, which would only happen if was using the theme_hook$. Wouldn't it?
So how is it being called? It's not that I need to override it. I'm just learning how drupal works and thought I had it sussed until this. Something must be calling it.
Also, the function theme_text_formatter_default exists in the theme registry and it's overridable (if that's a word) as I did so in my template.php and it displayed. It's all quite confusing.
Any help would be much appreciated

It's CCK that calls the theming function.
When you create a CCK field you select a widget. The widget corresponds to a theming function that is called. That's the short explaination.
To understand the entire mechanics will be a bit difficult as creating a cck field is a complex subject lacking good documentation.
To really understand what, how and why, you would need to understand how the CCK module works internally. Probably to the point where you could write patches for it. Something very few people could help you with.
Edit:
I don't know the CCK module in depth, I have only created my own field formatters. Anyways, I looked though the db and found that the table content_node_field_instance holds info about each CCK field, one of the columns is widget which it seems, is where CCK stores which theming function to call. It knows what function to call because if the naming convention that is used and through the implementation of hook_field_formatter_info, which is what other modules uses when they want to tell CCK about how to theme a field.

The function is defined in text.module. the theme function that matches the entry text_formatter_default returned from the implementation of hook_theme()istheme_text_formatter_default()`.
/**
* Theme function for 'default' text field formatter.
*/
function theme_text_formatter_default($element) {
return ($allowed =_text_allowed_values($element)) ? $allowed : $element['#item']['safe'];
}

Related

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?

using module_invoke_all to submit a form question

here is more detailed explanation:
i am using the ubercart module with the file download feature module (the uc_file module ).
i have created a product class (which is a new content-type as far as the drupal system) and add a cck file field to it.
what i want to achive is the following behavior:
once a user saves a new node of my product class, i want the uploaded file to be added as a file download feature to the product class automatically.
i know i can hack the function uc_file_feature_form_submit($form, &$form_state), do what it does in my module code, but i rather ivoke it since i'll have easier life with future changes to the uc_file module (since i am calling it's function, i dont care if it will change in the future).
so, to invoke the uc_file_feature_form_submit function i need to build fake $form, &$form_state parameters, i know i can print_r those arrays, and build it from there, the thing is that there are a lot of data in those arrays that is not mandatory, i was wondering what are those mandatory fields that i have to build on my own.
thank you...
Short answer: Look at the submit function you are trying to invoke. The form values that it's using are the ones you need.
Long answer . . . need more info before I can give a better answer.
You can use drupal_execute() to programatically execute a form. I am not sure if it works with files though.

Add extra field in story content type using hook without CCK

I want to add extra field in story content type using hook, I don't want to use CCK, because am trying something different.
Please tell some suggestion with hook method.
If you do not use CCK, you will have to create your database table and code to add the form field, validate the form field, capture the data and save it in your field. I know cck can be a monster, but it does all this for you. I'd be happy to give you more info on all of this, but it is quite lengthy
There are lots of reasons that you may want to do this without CCK or Fields, and the best example is found at the node_example module in the examples project which can be found at: http://drupalcode.org/project/examples.git/tree/refs/heads/6.x-1.x:/node_example. You can also view the documentation on api.drupal.org.
The short version is that you're going to have to define your own node type using hook_node_info() and then define all the hooks for _load(), _insert(), _update(), _delete(), _access(), _validate(), and _view() in addition to defining your schema in your hook_schema and managing your tables on your own.
Sadly there is no good example for Drupal 7 as the node_example module for 7 was converted to use fields instead of the hooks listed above, which are still fully documented on api.drupal.org (they do now typically act on an array of nodes instead of a single node, but are otherwise identical).

Setting up Drupal node reference autosuggest to search on two separate fields

The simple versoin of my question: I need a CCK Node reference field to search on two different fields in a node. What's the best way to do this?
Some Background
I run a calendar for a physical therapy program. Each lecture has a reading list. Readings are their own content type and the lecture has a autosuggest node reference field that currently only searches the reading's title. I need it also to search on an additional cck field, the readings author.
I attempted to do this a custom view, but I need a very simple title OR author search, and we're stuck with AND for a little bit longer. Is there a workaround? Do I need to create a small module to do this? If so, wheres a good place to start to learn how to do that? (I've made custom modules before but none that involve interfacing with views/cck). I'm not really sure where to go with this right now.
Thanks for the help!
There isn't really a neat way to accomplish this, because searching only in the title is a "feature" of the Node Reference module (which is part of CCK). However, you can create a custom View to provide the results for the autocomplete, and use hook_views_query_alter() to change the query that the View execute. The view you create should be selected in the configuration page of the field.
Below is a sample implementation that changes the query to search both the title and the body of nodes. You'll probably need to customize it a little bit to get exactly what you want.
function mymodule_views_query_alter(&$view, &$query) {
if ($view->name == 'my_custom_view' && $view->current_display == 'content_references_1') {
// Remove the original title constraint
unset($query->where[0]['clauses'][2]);
// Duplicate the argument (keyword to search for), so
// it is passed to both the title and the other field
$query->where[0]['args'][] = $query->where[0]['args'][1];
// Add the custom where clause
$view->query->add_where(0, "(node.title LIKE '%%%s%%' OR node_revisions.body LIKE '%%%s%%')");
}
}

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