Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
A client of mine wants to give anonymous users the ability to view chats as they happen, but not participate in them. The user would have to log in to participate, essentially removing the ability for guest comments.
I have achieved this with some hook_form_alter()s, but I wonder if this is a feature anyone else might like to have.
My code is below. It could obviously be achieved differently if it was included in the module. You would also likely want a hook_update_n to give everyone who has "access chats" permission the new "participate in chats" permission.
<?php
/**
* Implementation of hook_perm().
*/
function mymodule_perm() {
return array('participate in chats');
}
/**
* Implementation of hook_form_alter().
*/
function mymodule_form_alter(&$form, $form_state, $form_id) {
global $user;
switch ($form_id) {
// Hide chatroom buttons from those who don't have access
case 'chatroom_chat_buttons':
if (!user_access('participate in chats')) {
$form['chatroom_message_entry_box']['#access'] = FALSE;
$form['chatroom_message_entry_submit']['#access'] = FALSE;
// Show a login link if they are not logged in
if ($user->uid == 0) {
$node = glodigital_parse_menu_node_object();
if ($message = variable_get('chatroom_chat_login_text', t('Please !login to participate in this chat.'))) {
$message = str_replace('!login', l(t('log in'), 'user/login', array(
'query' => 'destination=node/'. $node->nid,
)), $message);
}
}
// Show a message saying they don't have access
else {
$message = variable_get('chatroom_chat_logged_in_text', t('You do not have access to participate in this chat.'));
}
if ($message) {
$form['chatroom_message_login_text'] = array(
'#type' => 'markup',
'#value' => '<div id="chatroom-chat-buttons">'. $message .'</div>',
);
}
}
break;
// Add chatroom settings for users to view but not participate.
case 'chatroom_admin_settings':
$form['chatroom_chat_login_text'] = array(
'#type' => 'textfield',
'#title' => t('Login to chat text'),
'#description' => t('The text to be displayed to anonymous users who don\'t have permission to chat. Leave blank to display nothing.'),
'#default_value' => variable_get('chatroom_chat_login_text', t('Please !login to participate in this chat.')),
'#size' => 60,
);
$form['chatroom_chat_logged_in_text'] = array(
'#type' => 'textfield',
'#title' => t('Unable to chat text'),
'#description' => t('The text to be displayed to logged in users who don\'t have permission to chat. Leave blank to display nothing.'),
'#default_value' => variable_get('chatroom_chat_logged_in_text', t('You do not have access to participate in this chat.')),
'#size' => 60,
);
break;
}
}
?>
Thoughts?
Comments
Comment #1
Anonymous (not verified) CreditAttribution: Anonymous commentedi'm not opposed to adding this feature. i'll evaluate your patch today.
Comment #2
Michsk CreditAttribution: Michsk commentedsounds like a nice option, i'm interested.
Comment #3
mstrelan CreditAttribution: mstrelan commentedComment #4
Michsk CreditAttribution: Michsk commentedyeh i to would like to use this for not registered users.
Comment #5
TheChemic CreditAttribution: TheChemic commentedI would be very interested in this feature. Ideally (for me), it would allow an anonymous user to view a chat. And perhaps, if not too difficult to code, allow the user to attempt to chat with an automated response "to chat, please login or register" with links provided.
Comment #6
Michsk CreditAttribution: Michsk commentedTheChemic good one!