drupal programmatically disable or remove node create node link(s) - drupal

In the context of organic groups, I am writing a module which will stop users who are not members of a group from adding group posts into that group.
My module currently sets the permissions necessary and detects whether a user has the permission.
So when a user(s) are looking at a group page, I want to disable/remove the standard link to create group posts.

Try this method.
function mymodule_menu_alter(&$items) {
global $user;
// Perform code for finding out users permissions.
// lets suppose we set true or false to $restricted after all
if ($restricted && isset($items['node/add/yourtype'])) {
$items['node/add/yourtype']['access arguments'] = FALSE;
// or unset($items['node/add/yourtype']) to remove item for user
}
}

If I understood right you don't want certain users to create a content type.
So the steps are:
1) Create a menu hook.
// Here we make sure if the user goes to for creating this node type
// we can use the appropriate call back function to stop it.
function yourmodoule_menu() {
$items = array();
$items['node/add/page'] = array(
'page arguments' => array('yourmodule_additional_actions'),
'access arguments' => array('administer create content')
);
}
2) Then make a permission hook to make sure only certain users have this permission.
// drupal will only allow access to to path 'node/add/page' with people
// who have access given by you.
function yourmodule_permission() {
return array(
'add content' => array(
'title' => t('Administer create conent'),
'description' => t('Perform administration tasks and create content')
)
)
}
3) Write your code for those users who have the permission.
// Only affter they have this permisson drupal will allow them access
// to the below function.
function yourmodule_additional_actions() {
// this code will only execute if the user has the permission
// "Administer create conent"
}

Related

Use VBO in Drupal to update custom user field

I want to perform bulk update of users with a Approved users, the table
field_user_status_value
-----------------------
entity_type, entity_id, field_user_status_value
The entity_id is the user id which does not exist in the table, below is the custom module I wrote to update the table:
function bulkapprove_action_info() {
return array(
'bulkapprove_action_callback_name' => array(
'type' => 'user', // Can be file, term, user, etc.
'label' => t('Approve User'),
'configurable' => FALSE, // Doesn't need config form
'behavior' => array('view_property'), // Uses view access rights ,
'pass rows' => TRUE,
'triggers' => array('any'), // Works always
),
);
}
function bulkapprove_action_callback_name($entity, $context)
{
db_update('field_data_field_user_status')->fields(array('field_user_status_value' => 'Approved'))->condition('entity_id', $context->entity_id)->execute();
}
But it is not inserting the values in this table
In Drupal you do not want to update the database fields directly unless you created the table. Drupal's internal APIs provide a collection of tools to ensure you update the values correctly and that all supporting modules get notified of changes as needed through the hook system.
In this case the callback gets the actual entity to run your action against (in this case the user object). You want to take action on that entity and then save the entity.
function bulkapprove_action_callback_name($entity, $context)
{
$entity->status = 1;
entity_save('user', $entity);
}

Drupal custom user registration form

I have built a custom registration form using module_form_alter hook. I have also added the required new fields to the database with db_add_field. Now I'm able to add the values to the table in user registration/ user profile edit and the values are also getting stored in the database.. But what I'm not able to do is get the values that are stored in the database in the user profile edit form is displayed. Is there a hook to load the values from database to form on form load? Or is there any other way?
function customUser_schema_alter(&$schema) {
// Add field to existing schema.
$schema['users']['fields']['detail'] = array(
'type' => 'varchar',
'length' => 100,
);
}
function customUser_install() {
$schema = drupal_get_schema('users');
db_add_field('users', 'detail', $schema['fields']['detail']);
}
function customUser_form_alter(&$form, &$form_state, $form_id) {
// check to see if the form is the user registration or user profile form
// if not then return and don’t do anything
if (!($form_id == 'user_register_form' || $form_id == 'user_profile_form')) {
return;
}
$form['account']['detail'] = array(
'#type' => 'textfield',
'#title' => t('Additional Detail'),
);
}
A proper answer needs more details. I can only assume what you did.
You added fields to the {users} table. You didn't update the database schema which made drupal_write_record not be aware of the new fields, that being the reason they are not populated.
You created a new table {my_table} with the fields.
In both cases you need hook_user_insert()
/**
* Implements hook_user_insert().
*/
function mymodule_user_insert(&$edit, $account, $category) {
// Here you add the code to update the entry in {users} table,
// or int your custom table.
// $edit has the values from the form, $account->uid has the
// uid of the newly created user.
}
Note: If my first assumption is true that's not the drupal way to do it. You should have done the 2nd way instead. And even in that case use the hook_schema to create your table in mymodule.install instead of doing db_add_field().
For drupal 7 you could have uses the profile module (core) or profile2 to achieve that.
Based on that code
Try to change to this inside the form alter.
$account = $form['#user'];
$form['account']['detail'] = array(
'#type' => 'textfield',
'#title' => t('Additional Detail'),
'#default_value' => $account->detail,
);

How to define custom access check function to see if a user may create a node in Drupal 7?

I have several things I want to check before a user may create a node. So, if the user visits node/add/proposal I want to check if he may do so, so I wrote a module:
function proposals_menu() {
$items['node/add/proposal'] = array(
'title' => t('Proposal'),
'access callback' => 'proposals_access',
);
return $items;
}
function proposals_access() {
$cond1;
$cond2;
...
return cond1 && cond2 && ....;
}
When I click on add content -> proposal I get a blank page. What am I missing?
To override existing menu items you need to use hook_menu_alter() instead of hook_menu(). e.g.
function proposals_menu_alter(&$items) {
$items['node/add/proposal']['access callback'] = 'some_function';
}
But there's also hook_node_access() which would be preferable to use for (as the name suggests) checking node access. e.g.
function proposals_node_access($node, $op, $account) {
$type = is_string($node) ? $node : $node->type;
if ($type == 'proposal' && $op == 'create') {
if ($allow_access) {
return NODE_ACCESS_ALLOW;
}
else {
return NODE_ACCESS_DENY;
}
}
return NODE_ACCESS_IGNORE;
}
Assuming you populate $allow_access with your access check. Be sure to use the $account object that's passed to the hook to verify the operation against that user object. Don't depend on the currently logged in user, which will not always be the same.
You get a blank page because you are not telling drupal how to render the path you are creating. For that you need to add a page callback to the item. As specified in the hook_menu documentation, this function will be called to display the page when the user visits the path.
...
$items['node/add/proposal'] = array(
...
'page callback' => 'proposals_display_function'
);
...

Bypass Node Delete Confirm Form in Drupal

Looking for the best way to allow users to delete nodes on a site without the need to use a confirm form. I have tried using a form_alter to direct people to a custom submit function, without success.
Anyone ever tried this?
Assuming drupal 7, the node/%/delete menu entry is wired directly to the node_delete_confirm form. You can modify it with with a hook_menu_alter, and change the function from drupal_get_form to a page callback of your own design that will just delete the node.
Example:
In your module file you'd need:
function mymodule_menu_alter(&$items) {
$items['node/%node/delete']['page callback'] = 'my_node_delete_function';
$items['node/%node/delete']['page arguments'] = array(1);
$items['node/%node/delete']['module'] = 'mymodule';
$items['node/%node/delete']['file'] = 'mymodule.pages.inc';
}
And in your mymodule.pages.inc file you'd need:
function my_node_delete_function($node) {
// Taken from node modules node_delete_confirm submit handler
node_delete($node->nid);
watchdog('content', '#type: deleted %title.', array('#type' => $node->type, '%title' => $node->title));
drupal_set_message(t('#type %title has been deleted.', array('#type' => node_type_get_name($node), '%title' => $node->title)));
// Do a drupal goto here to preserver the 'destination' parameter
drupal_goto();
}

Change owner on publish node in Drupal

Users on my site can add nodes of a custom type (let's call it "Player") but cannot publish them. Effectively they need moderating before posting. Once an admin / moderator has published them, I want the owner / publisher to be changed to an the relevant admin / moderator. This is so that the user is be unable to edit them and also so it is possible to track who approved them etc.
How do I go about this? I thought it might involve Actions / Rules / Workflow / Workflow-ng etc, but I've looked at each and can't seem to figure out how to make it work!
Another alternative is to write a short module that includes an 'approve' link using hook_link(). Point that link to a menu callback that changes the node's ownership from the current user to the user that clicked the 'Approve' link.
It could be nice, clean way of solving this, but requires a bit of Drupal knowhow. However, if you ask someone in the #drupal IRC channel on irc.freenode.net, they could show you how to get started, or even code it as a contributed module for you.
You can do it manually when you're editing the Player nodes. There's a group of two settings towards the end where you can change the node creator and creation time.
Alternatively, can you give the non-admin users permission to create nodes, but remove their permission to edit these nodes. Might work, but could be painful for these users.
Just to add some more info - BrainV helped me develop the following code for a custom module - called publishtrigger here. I wanted the approve button to publish the Player node and then assign it to the "contentadmin" user, which has ID 6 in my case...
<?php
/**
* Implementation of hook_perm().
*/
function publishtrigger_perm() {
return array('approve nodes');
}
/**
* Implementation of hook_menu().
*/
function publishtrigger_menu() {
$items['approve/%'] = array(
'title' => 'Approve',
'page callback' => 'publishtrigger_approve_node',
'page arguments' => array(1),
'access arguments' => array('approve nodes'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implementation of hook_link().
*/
function publishtrigger_link($type, $object, $teaser = FALSE) {
// Show this link at the bottom of nodes of the Player type which are not yet
// owned by contentadmin (UID 6).
if ($type == 'node' && $object->type == 'player') {
// Make sure user has permission to approve nodes.
if (user_access('approve nodes')) {
$links = array();
if ($object->uid != 6 || $object->status == 0) {
// Node is not owned by contentadmin (UID 6), and therefore not approved.
$links['approve_link'] = array(
'title' => 'Approve',
'href' => 'approve/' . $object->nid,
);
}
else {
// Node is already approved
$links['approve_link'] = array('title' => 'Already approved');
}
return $links;
}
}
}
/**
* When this code is run, adjust the owner of the indicated node to 'contentadmin',
* UID 6.
*
* #param $nid
* The node id of the node we want to change the owner of.
*/
function publishtrigger_approve_node($nid) {
// Load the node.
$node = node_load($nid);
// Set the UID to 6 (for contentadmin).
$node->uid = 6;
// Publish the node
$node->status = 1;
// Save the node again.
node_save($node);
// Go back to the node page
drupal_goto($node->path);
}

Resources