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");
Related
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;
}
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;
}
}
}
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 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;
?>