Drupal user access callback selective response - drupal

Please bear with this Drupal API novice whilst I explain some background stuff!
I have been experimenting with the code below to create 2 separate responses when my users click on a custom node creation link. By default, a page opens that allows users to go through the usual steps in creating a node.
What my module does is check if the user has specific permissions and either allow them to proceed in creating the node or throw up an access denied page.
function mymodule_menu_alter(&$items) {
$items["node/add/page/%"]['access callback'] = 'mymodule_access_callback';
}
function mymodule_access_callback(){
if( user_access('open sesame') ){
drupal_set_message("successfully intecepting new node creation");
return true;
}
return false;
}
The node/add/page is blocked successfully but it does so in both cases. The if statement determines if the user has a certain permission and within it I added return true which resulted in the following error:
Fatal error: require_once() [function.require]: Failed opening
required '/node.pages.inc' (include_path='.:') in
/var/www/vhosts/mysite.co.uk/httpdocs/includes/menu.inc on line 347
As a novice, I am not sure what I need to do in order to avoid the access denied page for the right users.

Try this:
function mymodule_menu_alter(&$items) {
$items["node/add/page/%"]['access callback'] = 'mymodule_access_callback';
$items["node/add/page/%"]['file'] = drupal_get_path('module', 'node') . '/node.pages.inc';
}
EDIT
Try changing the second line above to this:
$items["node/add/page"]['file'] = drupal_get_path('module', 'node') . '/node.pages.inc';
It's an attempt to explicitly set the file path for the parent item of the path you're defining.
Also this might be a stupid thing to say but every time you make a change to hook_menu_alter() make sure you clear Drupal's caches so the changes are picked up.

Related

DriveApp.getFolderById :No item with the given ID could be found, or you do not have permission to access it

sorry. stuck again. I have a driveapp fileid and folderid. all correct i am sure about that. trying to just move fileid into the folderid (server-side script):
function moveFiles(sourceFileId, targetFolderId, role) {
var file = DriveApp.getFileById(sourceFileId);
file.getParents().next().removeFile(file);
DriveApp.getFolderById(targetFolderId).addFile(file);
return "1";
}
The line that crashes is the DriveApp.getFolderById() line, with the error message above (No item with the given ID could be found, or you do not have permission to access it)
these are files and folders that i own with my account. i cant see that it would be a permissions issue
My mistake...
I was using the getUrl() not the getId()... My bad

Adding users and attributes programatically in Concrete5 version 8

Hi I have recently followed some documentation to create new users from a csv file programatically. According to the Concrete5 docs/api there was method called getByID( $uID ) but this has since be deprecated!
I am creating a new user like so:
$userRegistration = Core::make('user/registration');
$file = fopen("test-users.csv","r");
while (($data = fgetcsv($file)) !== FALSE) {
echo "email address " . $data[0];
$userRegistration->create([
'uName' => $data[0],
'uPassword' => $data[1],
'uEmail' => $data[2],
'uIsValidated' => true,
]);
}
However if I want to add a value to an existing non-core attribute for instance lets call it user_county then how would I change this after programatically adding the user? I may need to do this for several user attributes as well so the values would need to come from the CSV and automatically be looped through to apply the correct value to the corresponding attribute whether it is blank or filled.
The create() method will return a UserInfo object (Concrete\Core\User\UserInfo) after successfully adding a new user. With the returned UserInfo object, you can use the setAttribute() method to set your custom user attribute.
Make sure that you have created the customer user attribute first and check that it is available before setting it, otherwise setting it will throw an error. I believe you can do this using Concrete\Core\Attribute\Key\UserKey::getByHandle('my_user_attribute') and seeing if it returns an object.
The create() method is in the RegistrationService class:
https://github.com/concrete5/concrete5/blob/develop/concrete/src/User/RegistrationService.php#L51-L140

How can I remove a message shown from a different module?

In Drupal 7, I could use the following code.
if ($_SESSION['messages']['status'][0] == t('Registration successful. You are now logged in.')) {
unset($_SESSION['messages']['status']);
}
What is the equivalent code for Drupal 8?
First of all, in Drupal 8, messages are stored in the same $_SESSION['messages'] variable as before. However, using it directly is not a good way, as there exist drupal_set_message and drupal_get_messages functions, which you may freely use.
Then, status messages are shown using status-messages theme. This means that you can write preprocess function for it and make your alteration there:
function mymodule_preprocess_status_messages(&$variables) {
$status_messages = $variables['message_list']['status'];
// Search for your message in $status_messages array and remove it.
}
The main difference with Drupal 7, however, is that now status messages are not always strings, they may be objects of Markup class. They are wrappers around strings and may be cast to underlying string using magic method __toString. This means that they can be compared with and as strings:
function mymodule_preprocess_status_messages(&$variables) {
if(isset($variables['message_list']['status'])){
$status_messages = $variables['message_list']['status'];
foreach($status_messages as $delta => $message) {
if ($message instanceof \Drupal\Component\Render\MarkupInterface) {
if ((string) $message == (string) t("Searched text")) {
unset($status_messages[$delta]);
break;
}
}
}
}
}
Upon reading the related change record, I have discovered \Drupal::messenger()->deleteAll(). I hope this is useful to someone. UPDATE: You should NOT do this, as it removes all subsequent messages as well. Instead, do unset(['_symfony_flashes']['status'][0]).
You can install the module Disable Messages and filter out messages by pattern in the configuration of the module.
For this particular case, you can filter out the message using the following pattern in the module configuration
Registration successful.*
Although the question is asked around Drupal 8 which is no longer supported, the module works for Drupal 7, 8, 9.
You can solve your problem in more than one way.
First way:
You can make minor change in core user module.
Go on:
\core\modules\user\src\RegisterForm.php
In that file you have line you can change:
drupal_set_message($this->t('Registration successful. You are now logged in.'));
NOTE: This is easiest way but in this case way you will edit Drupal core module and that is generally bad practice. In further development you could have problems like overwrite your changes when you do update.
Second way:
You can disable end user message using module. Disable message module have option you need. In module configuration you have text box where you can filter out messages shown to the end users.
Third way:
Messages in Drupal 8 are stored in a session variable and displayed in the page template via the $messages theme variable. When you want to modify the variables that are passed to the template before it's invoked you should use preprocess function. In your case here you can just search string in session variable and alert/remove it before it's displayed.
function yourmodule_preprocess_status_messages(&$variables) {
$message = 'Registration successful. You are now logged in.';
if (isset($_SESSION['messages'])) {
foreach ($_SESSION['messages'] as $type => $messages) {
if ($type == 'status') {
$key = array_search($message, $messages);
if ($key !== FALSE) {
unset($_SESSION['messages'][$type][$key]);
}
}
}
}
}
(Note:Untested code, beware of typos)
Hope this helps!

drupal7 blocked user show content not anonymous

Does anyone know if it is possible in Drupal 7 to show a piece of content (a Page) to a blocked user that an anonymous user can not access?
If so how to you go about doing it?
Many thanks.
Create a new content type (or a node) for blocked users.
Then you will need to code a custom module for that. Inside this module you'll need to implement hook_node_access, and the code would be similar to this
function [YOUR_MODULE]_node_access($node, $op, $account)
{
if($op == "view" && $node->type == "YOUR_CONTENT_TYPE" && $account->status != 0)
{
return NODE_ACCESS_DENY;
}
}
You can then use these nodes inside a block/view or any way you like.
Kindly note that I haven't tested the code, tell me if you have any problems getting it to work.
Hope this helps... Muhammad.

How can I modify the Book Copy module in Drupal 6 to forward the user to a different starting tab once a copy is made?

Example call to copy a book using the Book Copy module in Drupal 6 (assuming a node exists with book id of 142):
www.examplesite.com/book_copy/copy/142
When the above site is called and node 142 is copied it then notifies the user that the book was copied, but starts the user out on the Outline tab for the book copy. I think it would be more intuitive to start the user out on the Edit tab for the book copy so the user can immediately start to edit the information for the book. The outline is less important than setting up the actual initial details for the book which are in the Edit tab.
Does anyone know how I could modify the module to forward the user to the Edit tab? I looked through the code and it's just not clicking. I'm having problems interpreting exactly how this Book Module is working under the hood. Any suggestions will be greatly appreciated. Thanks!
I have no experience with the book_copy module, but looking at the last lines of the book_copy_copy_book() function:
$book = node_load(array('nid' => $newbid));
$book->bookcopydata = array();
$book->bookcopydata['message'] = t('Successfully cloned "%message", now viewing copy.', array('%message' => $message));
if (_book_outline_access($book)) {
$book->bookcopydata['url'] = 'node/'. $newbid .'/outline';
}
else {
$book->bookcopydata['url'] = 'node/'. $newbid;
}
// The function signature is: hook_book_copy_goto_alter(&$data);
drupal_alter("book_copy_goto", $book);
drupal_set_message($book->bookcopydata['message']);
drupal_goto($book->bookcopydata['url']); // requires user has 'administer book outline' or can access personal books
the user would automatically end on the new book page if he did not have the 'administer book outlines' right. If you want the user to always end on the new book edit page, you could just replace the whole if
if (_book_outline_access($book)) { ... }
clause with the assignment from its else part with an adjusted url:
$book->bookcopydata['url'] = 'node/'. $newbid . '/edit';
However, changing a modules code directly is not recommended (update clashes), especially if the module provides a hook to achieve the change you need 'from outside'. So the 'right' way in your situation would be to implement the offered hook_book_copy_goto_alter(&$data) in a custom module in order to adjust the redirection URL to your liking:
function yourModule_book_copy_goto_alter(&$new_book) {
$new_book->bookcopydata['url'] = 'node/'. $new_book->nid . '/edit';
}

Resources