From 544483766f6df94caf23c70209cc1005fc3d51f8 Mon Sep 17 00:00:00 2001 From: Paul McCrodden Date: Mon, 4 Jan 2016 21:51:58 +0900 Subject: [PATCH] Added variable defaults, updated action links, local tasks and routing. --- config/install/message_private.settings.yml | 6 +- message_private.links.action.yml | 87 +++----------- message_private.links.task.yml | 43 +------ message_private.module | 122 +++++++++----------- message_private.routing.yml | 5 + 5 files changed, 86 insertions(+), 177 deletions(-) diff --git a/config/install/message_private.settings.yml b/config/install/message_private.settings.yml index bd3bac9..a731eab 100644 --- a/config/install/message_private.settings.yml +++ b/config/install/message_private.settings.yml @@ -1 +1,5 @@ -message_private_message_limit: 0 \ No newline at end of file +message_private_message_limit: 0 +email_notify: 1 +# @todo : default limit init to 0 in yml, needs implementation accordingly. +default_limit: 0 +default_interval: 0 \ No newline at end of file diff --git a/message_private.links.action.yml b/message_private.links.action.yml index 5ef44c7..a79ff92 100644 --- a/message_private.links.action.yml +++ b/message_private.links.action.yml @@ -1,74 +1,15 @@ -#message_ui.create_message: -# route_name: message_ui.create_message -# title: 'Add message' -# appears_on: -# - view.message.page_1 +# Add a 'Create a new message for %user' action link. +# Only display the local task on other users profile, not current user's. +message_private.user.create_message: + route_name: message_ui.create_message_by_type + title: 'Create a new message for...' + appears_on: + - entity.user.canonical -#case 'user/%': -# // Add a 'Create a new message for %user' action link. -# if (is_array($router_item['page_arguments'])) { -# // Get the user object from page arguments. -# foreach ($router_item['page_arguments'] as $arg) { -# if (is_object($arg) && !empty($arg->uid)) { -# $to_user = $arg; -# } -# } -# } -# // Only display the local task on other users profile, not current user's. -# if (!empty($to_user) && $user->uid != $to_user->uid) { -# // @FIXME -#// menu_get_item() has been removed. To retrieve route information, use the -#// RouteMatch object, which you can retrieve by calling \Drupal::routeMatch(). -#// -#// -#// @see https://www.drupal.org/node/2203305 -#// $item = menu_get_item('message/create/private-message'); -# -# $item['title'] = t('Create a new message for') . ' ' . $to_user->name; -# $item['localized_options']['query'] = array('field_message_user_ref' => $to_user->uid); -# $data['actions']['output'][] = array( -# '#theme' => 'menu_local_action', -# '#link' => $item, -# ); -# -# } -# break; - -#case 'user/%/messages': -# // Add a 'Create a new message' action link above message_private view. -# // @FIXME -#// menu_get_item() has been removed. To retrieve route information, use the -#// RouteMatch object, which you can retrieve by calling \Drupal::routeMatch(). -#// -#// -#// @see https://www.drupal.org/node/2203305 -#// $item = menu_get_item('message/create/private-message'); -# -# $item['title'] = t('Create a new message'); -# $data['actions']['output'][] = array( -# '#theme' => 'menu_local_action', -# '#link' => $item, -# ); -# -# break; -# -#// Get from user to Add a 'Create a new message for %user' action link. -# $to_user = \Drupal::entityManager()->getStorage('user')->load($message->uid); -# // Display the local task on other users profile, not current user's. -# if (!empty($to_user) && $user->uid != $to_user->uid) { -# // @FIXME -#// menu_get_item() has been removed. To retrieve route information, use the -#// RouteMatch object, which you can retrieve by calling \Drupal::routeMatch(). -#// -#// -#// @see https://www.drupal.org/node/2203305 -#// $item = menu_get_item('message/create/private-message'); -# -# $item['title'] = t('Create a new message for') . ' ' . $to_user->name; -# $item['localized_options']['query'] = array('field_message_user_ref' => $to_user->uid); -# $data['actions']['output'][] = array( -# '#theme' => 'menu_local_action', -# '#link' => $item, -# ); -# -# } \ No newline at end of file +# Add a 'Create a new message' action link above message_private view. +# Get from user to Add a 'Create a new message for %user' action link +message_private.messages.create_message: + route_name: message_ui.create_message_by_type + title: 'Create a new message for...' + appears_on: + - message_private.messages \ No newline at end of file diff --git a/message_private.links.task.yml b/message_private.links.task.yml index adb36de..5240cc6 100644 --- a/message_private.links.task.yml +++ b/message_private.links.task.yml @@ -1,37 +1,6 @@ -# MENU_DEFAULT_LOCAL_TASK for 'message/%message/view'. -#message_ui.show_message.view: -# route_name: message_ui.show_message -# title: 'View' -# parent_id: message_ui.show_message -# weight: -10 -# -#message_ui.edit_message: -# route_name: message_ui.edit_message -# base_route: message_ui.show_message -# title: 'Edit' -# weight: 0 -# -#message_ui.delete_message: -# route_name: message_ui.delete_message -# base_route: message_ui.show_message -# title: 'Delete' -# weight: 10 - -# case 'message/%': -# $message = \Drupal::routeMatch()->getParameter('message', 1, $router_item['href']); -# if (isset($message->type) && $message->type == 'private_message') { -# // Add a Messages tab to the private_message Message entities. -# // @FIXME -#// menu_get_item() has been removed. To retrieve route information, use the -#// RouteMatch object, which you can retrieve by calling \Drupal::routeMatch(). -#// -#// -#// @see https://www.drupal.org/node/2203305 -#// $item = menu_get_item('user/' . $user->uid . '/messages'); -# -# $item['title'] = t('Messages'); -# $data['tabs'][0]['output'][] = array( -# '#theme' => 'menu_local_task', -# '#link' => $item, -# ); -# } +# To be set only for users with private message permissions. +message_private.messages: + route_name: message_private.messages + base_route: entity.user.canonical + title: 'Messages' + weight: -10 \ No newline at end of file diff --git a/message_private.module b/message_private.module index 4312a08..6b53dd2 100755 --- a/message_private.module +++ b/message_private.module @@ -5,22 +5,24 @@ */ use Drupal\Core\Routing\RouteMatchInterface; +use Drupal\message\Entity\Message; use Drupal\user\Entity\Role; +// @todo : perhaps the string constants can be removed and values used directly. /** * The string name for message limit variable. */ -const MESSAGE_PRIVATE_MESSAGE_LIMIT = 'message_private_message_limit'; +const MESSAGE_PRIVATE_MESSAGE_LIMIT = 'message_limit'; /** * The assoc index to be used when fetching default limit variable. */ -const MESSAGE_PRIVATE_DEFAULT_LIMIT = 'message_private_default_limit'; +const MESSAGE_PRIVATE_DEFAULT_LIMIT = 'default_limit'; /** * The assoc index to be used when fetching default interval variable. */ -const MESSAGE_PRIVATE_DEFAULT_INTERVAL = 'message_private_default_interval'; +const MESSAGE_PRIVATE_DEFAULT_INTERVAL = 'default_interval'; /** * The default index for settings such as role. @@ -172,39 +174,36 @@ function message_private_form_message_private_message_form_validate($form, \Drup */ function message_private_message_insert(\Drupal\message\MessageInterface $message) { // Prepare message notifications for private messages if notifications are on. - // @FIXME -// // @FIXME -// // The correct configuration object could not be determined. You'll need to -// // rewrite this call manually. -// if ($message->type == 'private_message' -// && variable_get(MESSAGE_PRIVATE_EMAIL_NOTIFICATIONS, TRUE)) { -// // Use message load as the $message object has issue with mail function. -// // It causes duplicate entry, possibly as mid is missing and the mail -// // function message_notify_send_message tries to re-save as a new message. -// $message = message_load($message->mid); -// $wrapper = entity_metadata_wrapper('message', $message); -// -// $mail = array(); -// $users = $wrapper->field_message_user_ref->value(); -// -// if (is_array($users)) { -// foreach ($users as $user) { -// $notify = field_get_items('user', $user, 'field_private_message_notify'); -// if (!empty($notify) && is_array($notify)) { -// // Get the 1st value of the array as there is only 1 possible item. -// $notify = array_shift($notify); -// } -// // If the user has set field for notifications, add their email. -// if (isset($notify['value']) && $notify['value']) { -// $mail[] = $user->mail; -// } -// } -// } -// if (!empty($mail)) { -// message_notify_send_message($message, array('mail' => implode(',', $mail))); -// } -// } - + // The correct configuration object could not be determined. You'll need to + // rewrite this call manually. + if ($message->type == 'private_message' + && \Drupal::config('message_private.settings')->get('email_notify')) { + // Use message load as the $message object has issue with mail function. + // It causes duplicate entry, possibly as mid is missing and the mail + // function message_notify_send_message tries to re-save as a new message. + $message = Message::load($message->id()); + + $mail = array(); + $users = $message->get('field_message_private_to_user'); + + if (is_array($users)) { + foreach ($users as $user) { + $notify = $user->get('field_private_message_notify'); + if (!empty($notify) && is_array($notify)) { + // Get the 1st value of the array as there is only 1 possible item. + $notify = array_shift($notify); + } + // If the user has set field for notifications, add their email. + if (isset($notify['value']) && $notify['value']) { + $mail[] = $user->mail; + } + } + } + if (!empty($mail)) { + // @todo: wait for D8 version of message_notify or use alternative? + message_notify_send_message($message, array('mail' => implode(',', $mail))); + } + } } /** @@ -215,15 +214,12 @@ function message_private_message_insert(\Drupal\message\MessageInterface $messag */ // @todo - check form ID matches D8 form id & add config for user notify field. function message_private_form_user_profile_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) { - // @FIXME -// // @FIXME -// // The correct configuration object could not be determined. You'll need to -// // rewrite this call manually. -// if (!\Drupal::currentUser()->hasPermission('bypass private message access control') -// && !variable_get(MESSAGE_PRIVATE_EMAIL_NOTIFICATIONS, TRUE)) { -// $form['field_private_message_notify']['#access'] = FALSE; -// } - + // The correct configuration object could not be determined. You'll need to + // rewrite this call manually. + if (!\Drupal::currentUser()->hasPermission('bypass private message access control') + && !\Drupal::config('message_private.settings')->get('email_notify')) { + $form['field_private_message_notify']['#access'] = FALSE; + } } /** @@ -299,17 +295,15 @@ function template_preprocess_message_private(&$variables) { // @todo - Is there a better location for this? function _message_private_max_message_limit_role($roles) { $limits = array(); - // @FIXME -// // @FIXME -// // The correct configuration object could not be determined. You'll need to -// // rewrite this call manually. -// $limit = variable_get(MESSAGE_PRIVATE_DEFAULT_LIMIT); - - // @FIXME -// // @FIXME -// // The correct configuration object could not be determined. You'll need to -// // rewrite this call manually. -// $interval = variable_get(MESSAGE_PRIVATE_DEFAULT_INTERVAL); + + // The correct configuration object could not be determined. You'll need to + // rewrite this call manually. + // @todo : default limit init to 0 in yml, needs implementation accordingly. + $limit = \Drupal::config('message_private.settings')->get('default_limit'); + + // The correct configuration object could not be determined. You'll need to + // rewrite this call manually. + $interval = \Drupal::config('message_private.settings')->get('default_interval'); // Ensure we have existing valid numerical values for both variables. if (!empty($limit) && ctype_digit($limit) && !empty($interval) && ctype_digit($interval)) { @@ -321,17 +315,13 @@ function _message_private_max_message_limit_role($roles) { $role_name = str_replace(' ', '_', $role); $limit_name = 'message_private_' . $role_name . '_limit'; $interval_name = 'message_private_' . $role_name . '_interval'; - // @FIXME -// // @FIXME -// // The correct configuration object could not be determined. You'll need to -// // rewrite this call manually. -// $interval = variable_get($interval_name); + // The correct configuration object could not be determined. You'll need to + // rewrite this call manually. + $interval = \Drupal::config('message_private.settings')->get($interval_name); - // @FIXME -// // @FIXME -// // The correct configuration object could not be determined. You'll need to -// // rewrite this call manually. -// $limit = variable_get($limit_name); + // The correct configuration object could not be determined. You'll need to + // rewrite this call manually. + $limit = \Drupal::config('message_private.settings')->get($limit_name); // Ensure we have existing valid numerical values for both variables. diff --git a/message_private.routing.yml b/message_private.routing.yml index 7711e00..52683f8 100644 --- a/message_private.routing.yml +++ b/message_private.routing.yml @@ -19,4 +19,9 @@ message_private.messages: message_private.messages.inbox: route_name: message_private.messages title: 'Inbox' + parent_id: message_private.messages + +message_private.messages.sent: + route_name: message_private.messages + title: 'Sent' parent_id: message_private.messages \ No newline at end of file