drupal7 blocked user show content not anonymous - drupal

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.

Related

how to read additional parameters in alfresco 5.1.1- aikau faceted search

Custom Search UI will be populated when user selects Complex asset in the Advance search screen drop down(apart from Folders,Contents) where 12 fields will be displayed .So when user clicks search button ,need to read those values and redirect to the alfresco repo files(org/alfresco/slingshot/search/search.get.js).We have already customized these files(search.get.js,search.lib.js) existed in the repository to suit out logic and working fine in 4.2.2;As we are migrating to 511,so we need to change this logic in customized faceted-search.get.js to read these values.How to write this logic in customized facted-search.get.js?
It's not actually possible to read those URL hash attributes in the faceted-search.get.js file because the JavaScript controller of the WebScript does not have access to that part of the URL (it only has information about the URL and the request parameters, not the hash parameters).
The hash parameters are actually handled on the client-side by the AlfSearchList widget.
Maybe you could explain what you're trying to achieve so that I can suggest an alternative - i.e. the end goal for the user, not the specifics of the coding you're trying to achieve.
We will be reading the querystring values something like below in the .get.js file.
function getNodeRef(){
var queryString = page.url.getQueryString();
var nodeRef = "NOT FOUND";
var stringArray = queryString.split("&");
for (var t = 0; t < stringArray.length; t++) {
if (stringArray[t].indexOf('nodeRef=') > -1) {
nodeRef = stringArray[t].split('=')[1];
break;
}
}
if (nodeRef !== "NOT FOUND") {
nodeRef = nodeRef.replace("://", "/");
return nodeRef;
}
else {
throw new Error("Node Reference is not found.");
}
}
It may be help you and we will wait for Dave Drapper suggestion also.

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!

find last editor aka author of Item in Sitecore

Ok I've used this in the past to get last editor and it works great when running on sites that use workflow but it doesnt return any users for sites that do not use workflow..
var contentWorkflow = contentItem.Database.WorkflowProvider.GetWorkflow(contentItem);
var contentHistory = contentWorkflow.GetHistory(contentItem);
if (contentHistory.Length > 0)
{
//submitting user (string)
string lastUser = contentHistory[contentHistory.Length - 1].User;
}
Can you reply with a way to get last editor of an item regardless of whether they use workflow or not?
Thanks
Try to use
contentItem.Statistics.UpdatedBy
You can check more members of the ItemStatistics class here.

Drupal user access callback selective response

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.

how to detect in drupal node form if its edit or add form?

Is there a way to detect if the node form being viewed is the "edit" or "add new node" form?
Detect where? In a hook_alter? In a template? Somewhere else?
In general, the approach would be to get ahold of the $node object, and see if it's nid field is set. If it is, it's an edit.
Also you can make use of URL, if you don't want to load entire node object.
When it is new node addition, then in the URL arg(0) will be "node", arg(1) will be "add", arg(2) will be "content_type_name" whereas in the case of node viewing arg(0) will be node and arg(1) will be nid(i.e Numeric).
This is just an alternative way to detect.
check these answers from drupal.stackexchange.com
for example:
function mymodule_form_node_form_alter(&$form, &$form_state) {
$node = $form_state['node'];
if (!isset($node->nid) || isset($node->is_new)) {
// This is a new node.
}
else {
// This is not a new node.
}
}
or use the arg() function as previously has been indicated.
if ($node->is_new) {do_something_for_new_node();}

Resources