drupal module, check if node type - drupal

As a more specific take on this question:
drupal jQuery 1.4 on specific pages
How do I check, inside a module, whether or not a node is a certain type to be able to do certain things to the node.
Thanks
The context:
I'm trying to adapt this code so that rather than working on 'my_page' it works on a node type.
function MYMODULE_preprocess_page(&$variables, $arg = 'my_page', $delta=0) {
// I needed a one hit wonder. Can be altered to use function arguments
// to increase it's flexibility.
if(arg($delta) == $arg) {
$scripts = drupal_add_js();
$css = drupal_add_css();
// Only do this for pages that have JavaScript on them.
if (!empty($variables['scripts'])) {
$path = drupal_get_path('module', 'admin_menu');
unset($scripts['module'][$path . '/admin_menu.js']);
$variables['scripts'] = drupal_get_js('header', $scripts);
}
// Similar process for CSS but there are 2 Css realted variables.
// $variables['css'] and $variables['styles'] are both used.
if (!empty($variables['css'])) {
$path = drupal_get_path('module', 'admin_menu');
unset($css['all']['module'][$path . '/admin_menu.css']);
unset($css['all']['module'][$path . '/admin_menu.color.css']);
$variables['styles'] = drupal_get_css($css);
}
}
}
Thanks.

Inside of a module, you can do this:
if (arg(0) == 'node' && is_numeric(arg(1)) && arg(2) != 'edit') {
if (!($node)) {
$node = node_load(arg(1));
}
if ($node->type == 'page') {
// some code here
}
}
That will load a node object given the current node page (if not available). Since I don't know the context of code you are working with, this is kind of a rough example, but you can always see properties of a node by doing node_load(node_id). But, depending on the Drupal API function, it may already be loaded for you.
For example, hook_nodeapi.
http://api.drupal.org/api/function/hook_nodeapi
You could do:
function mymodule_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
switch ($op) {
case 'view':
// some code here
}
}

Try this:-
function MyModule_preprocess_node(&$vars) {
if ($vars['type'] == 'this_type') {
// do some stuff
}
}

Try this:-
$node = node_load(arg(1));
$node =$node->type;
if($node == 'node_type'){
//do something
}

Related

Add class to drupal body

How can I add term id of all terms related to a node, to that node body class in drupal site?
For example, A node named stackoverflow is tagged with four terms
term1
term2
term3
term4
term5
I want to add these classed to node body class...
article-term-(term1tid)
article-term-(term2tid)
article-term-(term3tid)
article-term-(term4tid)
article-term-(term5tid)
These are pages I want to change their class names:
عکس نوزاد
عکس نوزاد
کاردستی
سوپ ساده
داستان برای کودک
کاردستی
leymannx code is really complete and fine.
But it does not contains all terms of a node.
I wrote this code and i wish it will be useful for you.
function YOURTHEME_preprocess_html(&$variables) {
if (arg(0) == 'node' && is_numeric(arg(1))) {
$node = node_load(arg(1));
$results = stackoverflow_taxonomy_node_get_terms($node);
if (is_array($results)) {
foreach ($results as $item) {
$variables['classes_array'][] = "article-term-" . $item->tid;
}
}
}
}
There is a function named ""stackoverflow_taxonomy_node_get_terms"" that returns all terms attached to a node.
function stackoverflow_taxonomy_node_get_terms($node, $key = 'tid'){
static $terms;
if (!isset($terms[$node->vid][$key])) {
$query = db_select('taxonomy_index', 'r');
$t_alias = $query->join('taxonomy_term_data', 't', 'r.tid = t.tid');
$v_alias = $query->join('taxonomy_vocabulary', 'v', 't.vid = v.vid');
$query->fields($t_alias);
$query->condition("r.nid", $node->nid);
$result = $query->execute();
$terms[$node->vid][$key] = array();
foreach ($result as $term) {
$terms[$node->vid][$key][$term->$key] = $term;
}
}
return $terms[$node->vid][$key];
}
i wish this code could be the best.
write all of this codes in template.php file in your theme.
if you want just some nodes have class name, add replace this part of code.
> if (arg(0) == 'node' && is_numeric(arg(1)) && ( arg(1)==X || arg(1)==Y
> ) ) {
As #P1ratRuleZZZ already pointed out template_preprocess_html (implemented from your sub-theme's template.php file) is the function to add body classes.
Thing is, that within this function, you need to load the actual node object first, then get the values of that term reference field, to finally add them as classes to the body tag.
Replace MYTHEME and field_MYFIELD with your names.
/**
* Implements template_preprocess_html().
*/
function MYTHEME_preprocess_html(&$variables) {
// Get the node.
if ($node = menu_get_object()) {
// Check node type.
if ($node->type === 'article') {
// Get taxonomy terms.
$terms = field_get_items('node', $node, 'field_MYFIELD');
foreach ($terms as $term) {
// Add body class.
$variables['classes_array'][] = 'article-term-' . $term['tid'];
}
}
}
}
Try to use template_preprocess_html()
this is in your theme's template.php file:
YOURTHEMENAME_preprocess_html(&$variables) {
$term_id = arg(1); // For example, or use some &drupal_static() to store a value while preprocessing from module
$variables['classes_array'][] = 'article-term-' . $term_id;
}
So as you can see it you shoud change template_ to your themename_ first

hook_alter uc_addresses submit function in Drupal/Ubercart

I am trying to hook into uc_addresses submit function, but it has become very confusing very fast. Just to note this is Ubercart running on Drupal 6. So I've isolated the code in uc_addresses.module that I'm interested in hooking in to:
function uc_addresses_get_address_form_submit($form, &$form_state) {
global $user;
$address_user = $form['stored_values']['#value']['user'];
$address = $form['stored_values']['#value']['address'];
$view = $form['stored_values']['#value']['view'];
if ($form_state['clicked_button']['#value'] == t('Delete address')) {
cache_clear_all();
$form_state['redirect'] =
array('user/'. $address_user->uid .'/addresses/' . $address->aid . '/delete');
}
else {
if (!$address) {
$address = new stdClass();
$address->uid = $address_user->uid;
}
$valid = TRUE;
foreach (element_children($form_state['values']['panes']) as $pane_id) {
$func = _address_pane_data($pane_id, 'callback');
$isvalid = $func('process', $address, $form_state['values']['panes'][$pane_id]);
if ($isvalid === FALSE) {
$_SESSION['expanded_panes'][] = $key;
$valid = FALSE;
}
}
if ($view == 'edit') { // Update database
_uc_addresses_db_update_address($address);
}
elseif ($view == 'new' || $view == 'add') { // Insert into datebase
_uc_addresses_db_add_address($address);
}
$form_state['redirect'] = array('user/'. $address_user->uid .'/addresses');
}
}
The goal is to copy a portion of the submitted form values in the database. Which in itself may be an issue because I need to make sure my hook occurs after the values have been written to the table. So my question is what should my hook function look like if I want it to occur after this form has been submitted?
Okay so foolish me didn't realize that there is a huge section in the uc_addresses documention about hooks. Link to the documentation. In this specific case where you want to hook into form submissions I would recommend using hook_uc_addresses_address_insert() and hook_uc_addresses_address_update().

Drupal Page Template based on url alias

I want to create an drupal page Template depending on the url alias.
Her my current situation:
I create a page named test, the url alias is test, too.
The page template, based on this docu - http://drupal.org/node/1089656 is: page--test.tpl.php.
I cleaned the drupal them cache, but there is still the default page template shown for this page.
What could be the error?
page--test.tpl.php doesn't work because Drupal is using the real path of page--node--#.tpl.php. To get Drupal to recognize aliased paths, you have to add the aliased path as part of the theme suggestions like so:
function MYMODULE_preprocess_page(&$vars, $hook) {
// only do this for page-type nodes and only if Path module exists
if (module_exists('path') && isset($vars['node']) && $vars['node']->type == 'page') {
// look up the alias from the url_alias table
$source = 'node/' .$vars['node']->nid;
$alias = db_query("SELECT alias FROM {url_alias} WHERE source = '$source'")->fetchField();
if ($alias != '') {
// build a suggestion for every possibility
$parts = explode('/', $alias);
$suggestion = '';
foreach ($parts as $part) {
if ($suggestion == '') {
// first suggestion gets prefaced with 'page--'
$suggestion .= "page--$part";
} else {
// subsequent suggestions get appended
$suggestion .= "__$part";
}
// add the suggestion to the array
$vars['theme_hook_suggestions'][] = $suggestion;
}
}
}
}
Source: http://groups.drupal.org/node/130944#comment-425189

Drupal PHP block visibility rules

In a Drupal block's Page Visibility Settings I'd like to prevent a certain block from showing if the second value in the path is a number. This does not seem to be working for me. Cheers.
Show block ONLY when arguments are:
domain.com/video/one (arg 0 is 'video' and arg 1 is present and NOT a number)
Don't show:
domain.com/video
domain.com/video/1
<?php
if (arg(0) == 'video' && is_nan(arg(1)) && empty(arg(2))) {
return TRUE;
}
else {
return FALSE;
}
?>
I'm assuming this is in a hook_block/hook_block_view function? You could try a different approach:
if (preg_match('/^video\/[0-9]+$/', $_GET['q'])) {
// Path has matched, don't show the block. Are you sure you should be returning TRUE here?
return TRUE;
}
else {
// Path has matched, go ahead and show the block
return FALSE;
}
You can simply use the following code:
<?php
$arg1 = arg(1);
$arg2 = arg(2);
// Check arg(1) is not empty, or is_numeric() returns TRUE for NULL.
return (arg(0) == 'video' && !empty($arg1) && !is_numeric($arg1) && empty($arg2));
?>
As KingCrunch already said, is_nan() doesn't return TRUE when its argument is a number.
The code you reported contains another error too: empty() can be used only with variables, as reported in the PHP documentation.
empty() only checks variables as anything else will result in a parse error. In other words, the following will not work: empty(trim($name)).
The code I reported shows the block for paths like "video/video1"; if you want to show the block also for paths such as "video/video1/edit", then the following code should be used.
<?php
$arg1 = arg(1);
return (arg(0) == 'video' && !empty($arg1) && !is_numeric($arg1));
?>
Using arg() doesn't work if the path you are looking for is a path alias. Suppose that "video/video1" is a path alias for "node/10"; in that case arg(0) will return "node," and arg(1) will return "10." The same is true for $_GET['q'] that will be equal to "node/10."
This happens because Drupal, during its bootstrap, initialize $_GET['q'] with the following code:
// Drupal 6.
if (!empty($_GET['q'])) {
$_GET['q'] = drupal_get_normal_path(trim($_GET['q'], '/'));
}
else {
$_GET['q'] = drupal_get_normal_path(variable_get('site_frontpage', 'node'));
}
// Drupal 7.
if (!empty($_GET['q'])) {
$_GET['q'] = drupal_get_normal_path($_GET['q']);
}
else {
$_GET['q'] = drupal_get_normal_path(variable_get('site_frontpage', 'node'));
}
If you what you are checking is a path alias, then you should use the following code:
// Drupal 6.
$arg = explode('/', drupal_get_path_alias($_GET['q']);
return (arg[0] == 'video' && !empty($arg[1]) && !is_numeric(arg[1]) && empty($arg[2]));
// Drupal 7.
$arg = explode('/', drupal_get_path_alias();
return (arg[0] == 'video' && !empty($arg[1]) && !is_numeric(arg[1]) && empty($arg[2]));
Dont know, what your arguments looks like, but I assume you mixed up two kinds of types. is_nan() only works with numbers. If you want to test, if a value is a number,
var_dump(is_numeric(arg(1));
is_nan() tests, if a "numeric" value is a concrete value or "not a number" like "infinite" or the result of "0/0" or such.

Drupal: Node Submission Time

How do I print into a block the node submission time.
something like... print $node->submitted_time?
$node->created is what you're looking for.
add block -> choose php filter, add code:
if ((arg(0) == 'node') && is_numeric(arg(1)) ) {
$node = node_load(arg(1));
if ($node) {
print format_date($node->created);
}
}

Resources