Drupal 8: How to unpublish node when user clicked on Delete button - drupal

On my website, The user can add the content and edit the content. On the edit page, there is one delete button to delete the content but I want to use that button to just hide/unpublish the content from the user and the public.
I have tried the below code but it is deleting the content.
function test_entity_predelete(Drupal\Core\Entity\EntityInterface $entity) {
$nid = $entity->id();
$node =Node::load($nid);
$node->setPublished(FALSE);
$node->save();
}

You can try to overwrite action button using hook_form_alter().
$form['actions']['delete']
Instead standard action use your own.

Firstly, do not allow users to delete nodes - by the proper
permissions settings.
Secondly add Publish/Unpublish button or just
show users Publish checkbox on edit form and instruct them how to
use it and how it works.
Maybe combination of modules can be helpful:
https://www.drupal.org/project/publishcontent
https://www.drupal.org/project/view_unpublished
https://www.drupal.org/project/override_node_options

Related

CRM Ribbon Workbench - Hide + Button on Sub-Grid

I have a sub grid on a new entity called Issues, the sub grid is for another new entity called Cost Detail.
I have them both loaded into my solution called Issue, and have opened issue in the ribbon workbench.
What I want to do is when you are on the Issue form and can see the sub-grid I want to be able to hide the + button which is displayed. However when I have hidden this button in the ribbon workbench it also hides the add new button in the 'Associated View' therefore no records can be added.
How do I do it so that only the + button on the sub grid is hidden and not the other view?
EDIT:
In order to hide the add button, you either need to take away create privileges to the role that the user is in or you could do a hack(unsupported) like this:
function hideAddButton(){
var addButton = $('#NameOfGrid_addImageButton');
if(addButton.size())
addButton.hide();
else
setTimeout(hideAddButton, 1000);//checks every second to see if the button exists yet
}
and call the hideAddButton function on form load
There is one answer that I found. If you have a field that unique for that entity, then you can do it with a simple javascript code.
Here is steps that you must follow in ribbon workbench:
Right click the button and customise button.
Add an enable rule, in Steps section add an Custom Javascript Rule, that contains your library and function name, please make sure that default is true.
This must be in your javascirpt library :
function hideAddNew(){
if(Xrm.Page.getAttribute("yourField")){
return false;
}
else {
return true;
}
}
Add a command that contains the enable rule we created.
Add the command to button.
That's it. I test it, it is working.

How do I change the reset password text in Drupal 7 (one-time login)

How do I change the text on the one-time login page for Drupal 7.
"This is a one-time login for user and will expire on date.
Click on this button to log in to the site and change your password.
This login can be used only once."
If you need to change the text on the password reset page, not just in the e-mail, that has to be done via a theme function. There's a page on how to do it (for both D6 and D7) in the Theming Guide on Drupal.org: https://drupal.org/node/350634
It also covers the user login and registration pages. Note that in sample code provided for D7 in Steps 2 & 3, they've left out the function for the password reset page, but you can create a function for that following the same pattern they use for the other two.
Specifically...
In template.php in your custom theme, create this function:
<?php
/**
* Implements hook_form_FORM_ID_alter()
*
* Set custom text on the user password reset form.
*/
function YOUR_THEME_form_user_pass_reset_alter(&$form, &$form_state, $form_id) {
$form['message']['#markup'] = "<p>Your custom text goes here.</p>";
$form['help']['#markup'] = "<p>This is another line of custom text.</p>";
// If you prefer, you can just delete the second line of markup with:
// unset($form['help']);
}
This can be found at the Interface Translation in your settings.
This way you can replace the strings with your own text without having to hook anything.
This page is a Drupal form, just make use of hook_form_alter. The form id is "user_pass_reset".

Show different menu links to user depending upon their Drupal 6 role

I want to display different navigation links and data to a user depending upon their role.
What module(s) could I use to achieve this?
Use hook_menu_alter()
/*
* Implementation of hook_menu_alter()
*/
function MYMODULE_menu_alter(&$items)
{
$items['your/path']['access callback'] = _custom_access_callback_for_this_page();
}
And inside the access callback function (here: _custom_access_callback_for_this_page), write whatever the validation you want.
Don't forget to clear the cache after any changes inside your `hook_menu_alter() implementation.
Hope this helps.
I really like this module for Drupal 6, you should check it out:
Menu per Role

Changing menu links, if user is authenticated or not

I need to change the menu links on my website (and leave the same items names) depending on the user is a guest, or authenticated user.
What's the standard way to do it ?
thanks
You cannot dynamically change a menu item's path, because menu items are cached.
Still, AFAIK, there are two ways to get what you want. Both methods require you to create your menu items with hook_menu in a custom module (not from the Menu UI).
The first method is to create two menu items with identical names and set the access rules so that one is only available for logged guests, the other for authenticated users. Since Drupal will only show menu items that the user is allowed to access, only one will show up at any given moment. In Drupal core, you can see how the user module creates a menu item for anonymous users by looking at the /user/login path in user_menu().
The second method is to create a single menu item and check in the menu callback if the user is logged in. If the user is logged in, you serve one page, if not you serve another. In Drupal core, the /user path works like this. See user_page to see how the code works.
You can dynamically change a menu item's path - see hook_translated_menu_link_alter.
This hook is called before every menu item is rendered IF it has the property ['options']['alter'] = TRUE.
You can set this property to menu items using hook_menu_link_alter.
Example code would be:
function MY_MODULE_menu_link_alter(&$item) {
$item['options']['alter'] = TRUE;
}
function MY_MODULE_translated_menu_link_alter(&$item, $map) {
if($item['mlid']==89) {
$item['link_path'] .= 'my-new-path';
}
}
Instead of altering the link, you coudl create the menues twice: once with the links for regular users and once with the links for registered/admin/... users
You can put a menu into a block and set it to only allow registered users to see the one block and non-registered users the other block. Either by selecting the proper radio button from the Drupal menu within the block creation form or via PHP that will evaluate and depending on it's return value (TRUE/FALSE) displays it. I suggest to go with the first approach.
You can change the menu by using a combination of nodeaccess module and linking to the corresponding pages.
For example, by default guest users cannot access /logout. If you create a link in a menu to logout, it will only display if a user is logged in. With nodeaccess, simply create a node, visit the grant tab and uncheck/check "authenticated users" or "anonymous users" for that node.
http://drupal.org/project/nodeaccess
Cheers,

Drupal html user id

I'm using Drupal 6.16: I think I have a pretty simple question. How can I get the current user id and put it in a menu tab. What I would like to happen is when the user logs in and wants to change their name, email etc to click a menu tab. I image it would look something like this: http://domain.com/user/{userid}/edit
Thanks in advance!
msindle
That's more difficult than you would think, because menu items are cached. There is not a straightforward way to create dynamic menu items with the userID in it.
What you can do, is write a custom module and imitate the behavior of the 'user' path. With an implementation of hook_menu you create a menu item, with the path 'user/edit' (just like user_menu() creates $items['user']). Next you create a menu callback user_edit_page(), similar to user_page(), which gets the id of the current user and returns the user edit page:
function user_edit_page() {
global $user;
if ($user->uid) {
menu_set_active_item('user/'. $user->uid .'/edit');
return menu_execute_active_handler();
}
else {
return drupal_get_form('user_login');
}
}

Resources