Drupal Page Template based on url alias - drupal

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

Related

Drupal HOOK_views_pre_render not working for simple change of $views->result

well I haven't been able to find the problem with my little custom module for a couple of days, I'm hoping some clever and kind person might spot my issue ;)
So I am creating a simple module that runs on a certain view type, this view contains images, the images have alt tags, I want to amend these alt tags by using the url alias and sticking this on the end of the alt tag..
I have successfully found the data in the $view array and I have looped through all image instances and done as said above, I know it has worked because I have printed them out onto the page.
However it is not updating the view on the actual page, the alt tags remain the same... Please excuse my ignorance here as I am learning PHP and multidimensional keyed arrays are taking some time to sink in.
So what is the variable called for the alt tag, an object? I think that's wrong but anyway.. This variable which is ..
$view->result[0]->_field_data['nid']['entity']->field_image_front_menu['und'][0]['alt']
... is being changed, everything seems fine inside the function and I have tried
return $view
but to no avail.
Here is my code in entirety
<?php
function image_alt_tag_alter_views_pre_render(&$view) {
if ($view->name == "we_print_for_menu") {
$path = arg(0) . "/" . arg(1);
$alias = drupal_get_path_alias($path);
// loops through index in array to change each alt tag
for ($i = 0; $i < count($view->result); ++$i) {
$view->result[$i]->_field_data['nid']['entity']->field_image_front_menu['und'][0]['alt'] = $view->result[$i]->_field_data['nid']['entity']->field_image_front_menu['und'][0]['alt'] . $alias;
}
return $view;
?>
<?php
function image_alt_tag_alter_views_post_execute(&$view) {
global $user;
if ($view->name == "we_print_for_menu") {
path = arg(0) . "/" . arg(1);
$alias = drupal_get_path_alias($path);
dsm($view);
for ($i = 0; $i < count($view->result); ++$i) {
$amended_alt = $view->result[$i]->_field_data['nid']['entity']->field_image_front_menu['und'][0]['alt'] . " " . $alias;
$view->result[$i]->_field_data['nid']['entity']->field_image_front_menu['und'][0]['alt'] = $amended_alt;
}
}
}

get uid from url_alias in Drupal 7

my url looks like this
http://localhost/incollect/spencer-gallery
i would like to obtain uid from this page to compare it with
$user->uid
Help what can i do I want to obtain uid from that node
$acc = user_load(arg(1)); //This is based on the url path
if ($user->uid == $acc->uid){
...some code
}
//Lookup system URL by alias and turn the system url into string array
$path = drupal_lookup_path('source', request_path());
$pathArr = explode("/", $path);
//Load node object if the page is of a node
if($pathArr[0] === 'node' && isset($pathArr[1])) {
$node = node_load($pathArr[1]);
//You can get any properties from the loaded node now, uncomment the line below to see it
//drupal_set_message("<pre>" . print_r($node, true) . "</pre>");
$nid = $node->nid;
$uid = $node->uid;
}

How to use nodeapi in drupal?

guys i have this node type -> movie. this movie has casts in it. so now i was instructed to display the number of cast in a movie. how do i achieve this by using nodeapi? please someone guide mo through this. im very new to drupal coding here. here's what ive made so far:
function count_cast_nodeapi(&$node, $op) {
switch ($op) {
case 'update index':
if ($node->type == 'movie') {
$text = '';
$q = db_query('SELECT count(field_movie_cast_nid) FROM content_field_movie_cast ' .
'WHERE nid = %d', $node->nid);
if ($r = db_fetch_object($q)) {
$text = $r->count;
}
drupal_set_message($text);
}
}
}
but i have no idea where to put this code. where to save it. do i make a module of this? and is this code even correct?
also i have only copied this code. so i dont know what that 'update index' means. and who or what function will set that parameter $op
I can suggest you follow solution:
Create new integer CCK field for content type "movie" which will store the number of casts. Name it movie_cast_number.
This field will be updating in hook_nodeapi (similar to your example). For that you need to create custom module "count_cast". Place it to sites/all/modules directory.
In hook_nodeapi need to use $op "insert" and "update". $op generating by module node and means which operation is performed with node. Insert means new node has been created and update - existing node has been updated.
If you need custom output for this content type then you need to create node-movie.tpl.php in your theme directory.
Code which update the number of casts can looks follow way:
function count_cast_nodeapi(&$node, $op) {
switch ($op) {
case 'update':
case 'insert':
if ($node->type == 'movie') {
$result = db_result(db_query('
SELECT count(field_movie_cast_nid) cnt
FROM {content_field_movie_cast}
WHERE nid = %d
', $node->nid));
$node->field_movie_cast_number[0]['value'] = $result->cnt;
}
}
}

Naming Drupal 7 template files based on url

If I have a page whose url alias is "/api/user/create", how can I name a template file based on the url such as "page-api-user-create.tpl.php".
You need to name it:
page--api--user--create.tpl.php
(note double dashes in the file name).
See http://drupal.org/node/1089656 for more info.
You can set up a page level template file to reflect the url, as was explained by Maciej Zgazdaj.
To do the same with a node level template file you have to add this to your template.php file:
function YOURTHEME_preprocess_node(&$variables) {
$url = str_replace("-", "", $variables['node_url']);
$urlParts = explode("/", $url);
unset($urlParts[0]);
if($urlParts[1] !== false) {
$out = array();
$sug = "node";
foreach($urlParts as $val) {
$sug .= "__".$val;
$out[] = $sug;
}
$variables['theme_hook_suggestions'] =
array_merge($variables['theme_hook_suggestions'], $out);
}
}
Replace YOURTHEME with .. well your theme. Check the offered suggestions with devel_themer.

drupal module, check if node type

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
}

Resources