Drupal 6 - how to set up a VIEWS block? - drupal

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.

Related

How to: Unpublish Nodes by Author when Author is assigned Role

I'm using Drupal 7 + Rules. I would like to create a rule that unpublishes all nodes authored by a user when they have been given a particular role.
EVENT - After updating an existing user account
CONDITION - User has role(s): SelectedRole
ACTION - ???
BONUS: If this could be limited to nodes of a certain type, that would be even better.
If there is a better way outside of Rules to do this, I'm open to other ideas.
Thanks so much!
You can create a custom ruleset to loop through the nodes or a Views Bulk Operation action.
An easiest option is to add a custom PHP function on your rule (PHP > Execute custom PHP code). Of course you have to enable the php filter core module if you didn't already.
In the PHP action you have to get all the nids of the published nodes the current user and loop through them to unpublish them. I will use the EntityFieldQuery API class but you can use the database functions also.
// Get updated user id
$uid = $account -> uid;
// Get all nodes from user that are of NODE_TYPE and are published
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'NODE_TYPE')
->propertyCondition('status', 1)
->propertyCondition('uid', $uid);
$result = $query->execute();
$nids = array_keys($result['node']);
// Load all nodes in one go for better performance.
$nodes = node_load_multiple($nids);
foreach ($nodes as $node) {
// set status property to 0 (unpublished)
$node->status = 0;
// re-save the node
node_save($node);
}
I would also suggest to add one more Condition for the user before the one you are using: User Has Roles: (NOT) SelectedRole so that the action do not run everytime the user profile is updated.
References:
Use Rules to Unpublish content based on Author's role
Publish unpublished node programmatically
Find nodes created by user (more programmatically)

Drupal - Restricting access to users

I need to be able to add pages and then restrict these by different types of user: Anonymous, Partial and Full.
When registering, a user can enter all details to get a full registration or only half their details e.g don't need to enter address, telephone etc to gain a partial registration type.
Then when adding a page I need to be able to select whether anonymous, partial and/or full have access to it. If they don't then it still needs to show the summary of the page as a teaser but they won't have access to the main content until they have registered and signed in.
I have installed the Simple Access plugin which allows me to create groups but unsure how to implement the registration form so that if the user only enters the required fields they will become a Partial user, otherwise they will become a Full user. Any suggestions?
You can use the Rules module. Create a triggered rule that runs on new user creation, then check the fields and finally assign the user a role accordingly.
I've ended up using a hook to implement whether a user is a normal authorised user or a full user. Can someone please check the hook below is correct? Im new to Drupal so not sure if any other tables are affected when adding/deleting roles.
function module_user_update(&$edit, $account, $category) {
$dob = field_get_items('user', $account, 'field_dob');
$address1 = field_get_items('user', $account, 'field_address1');
$address2 = field_get_items('user', $account, 'field_address2');
$address3 = field_get_items('user', $account, 'field_address3');
$city = field_get_items('user', $account, 'field_city');
$postcode = field_get_items('user', $account, 'field_postcode');
$county = field_get_items('user', $account, 'field_county');
$telephone = field_get_items('user', $account, 'field_telephone');
if(empty($dob[0]['value']) || empty($address1[0]['value']) || empty($address2[0]['value']) || empty($address3[0]['value']) || empty($city[0]['value']) || empty($postcode[0]['value']) || empty($county[0][$
{
$userid = $account->uid;
//remove full role from db so user is only an authorised user
db_query("DELETE FROM {users_roles} WHERE uid = '".$userid."' && rid = '5'");
} else {
$userid = $account->uid;
//delete full role if it already exists so it doesnt go in twice
db_query("DELETE FROM {users_roles} WHERE uid = '".$userid."' && rid = '5'");
//insert full role
db_query("INSERT INTO {users_roles} (rid, uid) VALUES ('5',$userid)");
}
}
You would do well to look into some of the beginning Drupal tutorials. Roles, rules, triggers and CCK (and content_permissions) -- those are the modules/concepts you'll be looking at learning about.
They will arm you with what you need. CCK will allow for you to create specific content types, content_permissions (included in CCK) will allow for you to set visibility based on a user's role, roles will allow for you to create a new group of users, and as #Laxman13 said, rules will allow for you to set up rules to do what needs to be done (i.e. add this user to X role), and triggers will provide you with the functionality to do that.

Change node author automatically

Anonymous user is able to post nodes. After posting node, user is redirected to registration. After registration, the previously submitted node should be linked with newly registered user.
I played with rules and entities but I was not able to get it work properly. Any ideas?
I would write a custom module (but that's me). The module needs to implement hook_node_insert and save the nid into SESSION. Then on hook_user_insert it can do the change. Untested code:
function foo_node_insert($node) {
$_SESSION['mynodes'][] = $node->nid;
}
function foo_user_insert($edit, $account) {
if (!empty($_SESSION['mynodes'])) {
foreach ($_SESSION['mynodes'] as $nid) {
$node = node_load($nid);
$node->uid = $account->uid;
// This saves the revision as the current user uid but that's just what we wanted.
node_save($node);
}
}
}
Edit: don't forget unset($_SESSION['mynodes']);
Save the node data until after registration and post it then.
There's the Anonymous Node Create module.
The module allows anonymous users to create nodes. But 'anonymous' is questionable in this module. This module alters the node form for anonymous users by adding two field groups at the end before the save button.
The first field group has fields that allow users to create a new account. This new account is then the author of the new node created.

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

Show block on nodes the user can edit?

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

Resources