Show block on nodes the user can edit? - drupal

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

Related

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

Drupal 6 - how to set up a VIEWS block?

I can't figure out how to set up a block view for this in Drupal 6:
Users submit a picture. If it is approved, I upload it to the site.
There is a node that shows the details of the picture and the author information. I want to have a block that says, MORE BY THIS AUTHOR. This block would list more images that this author has submitted. How can I do this?
The URL is: mysite/content/name-of-image so I don't know how to create a view that shows all the images by this author since the user name is not in the URL. Can someone tell me how to do this?
Thank you.
Add a user:uid argument to the view.
choose "provide deafult argument" option for this argument.
choose "php code" as default argument type.
enter following php code:
if (arg(0) == 'node' && is_numeric(arg(1))) {
$node = node_load(arg(1));
$uid = $node->uid;
}
return $uid;
in your case you want the uid from a cck field of the node, so the php code should be this:
if (arg(0) == 'node' && is_numeric(arg(1))) {
$node = node_load(arg(1));
//if the field contains a user id
$uid = $node->field_authorid[0][value];
//if the field contains a username
$user = user_load(array('name' => check_plain($node->field_authorname[0][value])));
$uid = $user->uid;
}
return $uid;
Now you'll have the author user id from the node in url.
Now you can add fields to the view from nodes created by this author.
NOTE: the view preview won't show any results, so you'll have to save this view and test it outside the views builder.

Drupal 7: Access custom node field in page.tpl.php

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

Drupal - Hide block with PHP code

I've installed the module - http://drupal.org/project/formblock (Enables the presentation of node creation forms in blocks)
I've enabled it for a particular content type and then exposed that block, to show when an Organic Group node is being viewed.
What I would like to do is hide that block if the current logged in user, is not the author of the Organic Group node being viewed. i.o.w I only want the organic group author to see that block.
thanks in advance :)
You can use 'PHP block visibility settings' to achieve what you want here. Using PHP you can query the database, and check whether the logged in user is the same user that created the node in the organic group.
There is an example already on drupal.org that I have adapted (you will probably need to customise this further) -
<?php
// check og module exists
if (module_exists('og')){
// check user is logged in
global $user;
if ($user->uid) {
// check we've got a group, rights to view the group,
// and of type "group_type" - change this to whichever group you want to restrict the block to
// or remove the condition entirely
if (($group = og_get_group_context()) && node_access('view', $group) && ($group->type == 'group_type') ) {
// check current user is a team admin as they should get access
if (og_is_node_admin($group)) {
return TRUE;
}
// check to see if the current user is the node author
if (arg(0) == 'node' && is_numeric(arg(1))) {
$nid = arg(1);
$node = node_load(array('nid' => $nid));
if ($node->uid == $user->uid) {
return TRUE;
}
}
}
}
}
return FALSE;
?>

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