get uid or nid from an alias_url in Drupal 7 - drupal

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;

Related

How get the language, which current user set in his profile?

How get the language in wordpress, which current user set in his profile?
Something like:
$language = get_current_user()->language;
if ($language == 'en') {
//gogogo;
}
If you want the user meta locale (which is set in user profile), you can try this:
$user = wp_get_current_user();
$user_locale = get_user_meta($user->ID, 'locale', true);
if($user_locale == 'en_EN') {
// gogogo;
}
Greetz, Bjorn

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

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;
}

Nested relations in Drupal

I have a D7 website where users can make content (obviously...). So every node has it's own author. Every author is a member of an organization. But he can be a member of more then one organization. So far the facts.
I would like to create a view where the content is filtered on Author. Very easy, set the relation of the view on "Content's Author" and select the current user as filter.
But what I would like is to filter on the author's organization. So in fact it's a nested relation. Filter the nodes on the current logged in user (that's easy), but how can I filter on the current logged in user's organization?
Ok, the panels didn't work out, so I wrote my own hook :-)
function mymodule_views_pre_view (&$view, &$display_id, &$args) {
// Only execute this script when the view 'fiches_my_thema' is called
if ('fiches_my_thema' == $view->name) {
// Get users thema
global $user;
$userRoles = $user->roles;
$user_themas = array();
// Filter roles so you end up with the "Thema's".
foreach ($userRoles as $key) {
if(strpos($key,'edacteur')) {
$key = str_replace('Redacteur - ','', $key);
$key = str_replace('Eindredacteur - ','', $key);
$user_themas[] = $key;
}
}
// Resolve tid
$terms = taxonomy_get_tree(5);
$allRoles = array();
$arguments = array();
// Assign the 'tid' to a variable
foreach ($terms as $key) {
$singleRoles = $key->name;
$allRoles[] = $singleRoles;
if(in_array($singleRoles, $user_themas)) {
$arguments[] = $key->tid;
}
}
// Only when the arguments are NOT empty, set the arguments
if(!empty($arguments)) {
$finalArguments = implode("+", $arguments);
$args[] = "$finalArguments";
$view->set_arguments($args);
}
}
}

Find nid from node's internal path in Drupal?

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");

Resources