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;
}
Related
I would like to obtain uid in a page, i tried to use
global $node;
$nid = $node->nid;
global $user;
$userid = $user->uid
but it doesn't work my url is something like this
http://localhost/incollect/spencer-gallery
spencer-gallery is node alias
what can i do to get uid or nid in spencer-gallery page?
For the nid you can use the code below:
if (arg(0) == 'node' && is_numeric(arg(1))) {
$nid = arg(1);
}
To follow on from Kevin's answer once you have the $nid you can user:
$node = node_load($nid);
And then the user id of the nodes author is accessible by:
$uid = $node->uid;
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
I have a path like this:
/news/2011/05/26/some-story-path
How do I find the nid for the node that the path points to?
Edit
The path above is simply a string value that I have on another page. I need to obtain information about the node that the path links to.
You could use menu_get_object:
$node = menu_get_object();
See http://api.drupal.org/api/drupal/includes--menu.inc/function/menu_get_object/6
edit
I think you can specify your path like this
menu_get_object($type = 'node', $position = 1, $path = '/news/2011/05/26/some-story-path');
In Drupal 6...
If you know the url alias, you can retrieve the internal system path:
$nid = str_replace("node/","",drupal_lookup_path("source","my/drupal/path"));
From php, when viewing/editing a node, you can also retrieve it like so:
function get_current_nid () {
if (arg(0) == 'node' && is_numeric(arg(1)) { return arg(1); }
return null;
}
$nid = get_current_nid();
drupal_set_message("The current node id is: $nid");
I added a new field "mood" (image) to the page content type. Is there any way to access the image stored in this field in the page.tpl.php?
Should be
$node = node_load($nid);
$node->field_mood[$node->language][0]['value'];
There is a new "field_get_items()" function in drupal 7. The $node variable should already be defined in page.tpl so the first line may not be required.
This will get the field in the appropriate language. There is also an optional parameter to specify the desired language if needed.
$node = node_load($nid);
$values = field_get_items('node', $node, 'mood');
if ($values != FALSE) {
$val = $values[0]['value'];
}
else {
// no result
}
reference: http://api.drupal.org/api/drupal/modules--field--field.module/function/field_get_items/7
What block visibility PHP snippet would show a block only on node pages that the loged-in user can edit? The user may not own the node. In my case, I want to show the Content Complete block to people who can actually populate missing fields.
check for node_access("update", $node) (more on http://api.drupal.org/api/function/node_access/6)
//first check whether it is a node page
if(arg(0) == 'node' && is_numeric(arg(1))){
//load $node object
$node = node_load(arg(1))
//check for node update access
if (node_access("update", $node)){
return TRUE;
}
}
Following is barraponto's solution rewritten for noobs like me and to support multiple conditions.
<?php
$match = FALSE;
// Show block only if user has edit privilges for the node page
// first check whether it is a node page
if(arg(0) == 'node' && is_numeric(arg(1))){
//load $node object
$node = node_load(arg(1));
//check for node update access
if (node_access("update", $node)){
$match = TRUE;
}
}
return $match;
?>