diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index 3c4264c..0000000 Binary files a/.DS_Store and /dev/null differ diff --git a/message_private.admin.inc b/message_private.admin.inc deleted file mode 100644 index 102c896..0000000 --- a/message_private.admin.inc +++ /dev/null @@ -1,191 +0,0 @@ - 'checkbox', -// '#title' => t('Message Private Email Notifications'), -// '#default_value' => variable_get(MESSAGE_PRIVATE_EMAIL_NOTIFICATIONS, TRUE), -// '#description' => t('Global On / Off checkbox for emails notifying users of a new private message'), -// ); - - - // Role based message create limit on/off checkbox. - // @FIXME -// // @FIXME -// // The correct configuration object could not be determined. You'll need to -// // rewrite this call manually. -// $form[MESSAGE_PRIVATE_MESSAGE_LIMIT] = array( -// '#type' => 'checkbox', -// '#title' => t('Limit Message Create By Role'), -// '#default_value' => variable_get(MESSAGE_PRIVATE_MESSAGE_LIMIT, FALSE), -// '#description' => t('Impose a message creation limit per interval. Users with multiple roles, get the highest limit from these roles'), -// ); - - - // Conditional fieldset for all message limitation settings. - $form['interval_limit'] = array( - '#title' => t('Message interval limits'), - '#type' => 'fieldset', - '#collapsible' => TRUE, - '#collapsed' => TRUE, - '#states' => array( - 'invisible' => array( - ':input[name="' . MESSAGE_PRIVATE_MESSAGE_LIMIT . '"]' => array('checked' => FALSE), - ), - ), - ); - - // Add a default fieldset. - $form['interval_limit'][MESSAGE_PRIVATE_DEFAULT_INDEX] = array( - '#type' => 'fieldset', - '#title' => 'Default limit', - '#description' => t('Applies to all roles with blank entries below'), - '#collapsible' => TRUE, - '#collapsed' => FALSE, - ); - - // @FIXME -// // @FIXME -// // The correct configuration object could not be determined. You'll need to -// // rewrite this call manually. -// $form['interval_limit'][MESSAGE_PRIVATE_DEFAULT_INDEX][MESSAGE_PRIVATE_DEFAULT_LIMIT] = array( -// '#type' => 'textfield', -// '#title' => t('Limit'), -// '#default_value' => variable_get(MESSAGE_PRIVATE_DEFAULT_LIMIT), -// '#description' => t('Enter a message limit') . ' ' . MESSAGE_PRIVATE_MESSAGE_LIMIT_MIN . ' - ' . MESSAGE_PRIVATE_MESSAGE_LIMIT_MAX, -// ); - - - // @FIXME -// // @FIXME -// // The correct configuration object could not be determined. You'll need to -// // rewrite this call manually. -// $form['interval_limit'][MESSAGE_PRIVATE_DEFAULT_INDEX][MESSAGE_PRIVATE_DEFAULT_INTERVAL] = array( -// '#type' => 'textfield', -// '#title' => t('Interval'), -// '#default_value' => variable_get(MESSAGE_PRIVATE_DEFAULT_INTERVAL), -// '#description' => t('Enter an interval in minutes') . ' ' . MESSAGE_PRIVATE_MESSAGE_INTERVAL_MIN . ' - ' . MESSAGE_PRIVATE_MESSAGE_INTERVAL_MAX, -// ); - - - // Generate variable names for all roles used with get/set in admin form. - foreach (user_roles() as $id => $role) { - $role_name = str_replace(' ', '_', $role); - $limit_name = 'message_private_' . $role_name . '_limit'; - $interval_name = 'message_private_' . $role_name . '_interval'; - - $form['interval_limit'][$id] = array( - '#type' => 'fieldset', - '#title' => $role, - '#collapsible' => TRUE, - '#collapsed' => TRUE, - ); - // @FIXME -// // @FIXME -// // The correct configuration object could not be determined. You'll need to -// // rewrite this call manually. -// $form['interval_limit'][$id][$limit_name] = array( -// '#type' => 'textfield', -// '#title' => t('Limit'), -// '#default_value' => variable_get($limit_name), -// '#description' => t('Enter a message limit') . ' ' . MESSAGE_PRIVATE_MESSAGE_LIMIT_MIN . ' - ' . MESSAGE_PRIVATE_MESSAGE_LIMIT_MAX, -// ); - - // @FIXME -// // @FIXME -// // The correct configuration object could not be determined. You'll need to -// // rewrite this call manually. -// $form['interval_limit'][$id][$interval_name] = array( -// '#type' => 'textfield', -// '#title' => t('Interval'), -// '#default_value' => variable_get($interval_name), -// '#description' => t('Enter an interval in minutes') . ' ' . MESSAGE_PRIVATE_MESSAGE_INTERVAL_MIN . ' - ' . MESSAGE_PRIVATE_MESSAGE_INTERVAL_MAX, -// ); - - } - - return system_settings_form($form); -} - -/** - * Validate the admin settings form. - * - * @param mixed $form - * The admin form array from message_private_admin_settings. - * @param mixed $form_state - * The current state of the admin form array. - */ -function message_private_admin_settings_validate($form, &$form_state) { - - // Validate the default fieldset values. - _message_private_validate_fieldset( - $form['interval_limit'][MESSAGE_PRIVATE_DEFAULT_INDEX][MESSAGE_PRIVATE_DEFAULT_LIMIT], - $form['interval_limit'][MESSAGE_PRIVATE_DEFAULT_INDEX][MESSAGE_PRIVATE_DEFAULT_INTERVAL], - $form_state['values'][MESSAGE_PRIVATE_DEFAULT_LIMIT], - $form_state['values'][MESSAGE_PRIVATE_DEFAULT_INTERVAL] - ); - - // Cycle through the settings for each role and validate. - foreach (user_roles() as $id => $role) { - $role_name = str_replace(' ', '_', $role); - $limit_name = 'message_private_' . $role_name . '_limit'; - $interval_name = 'message_private_' . $role_name . '_interval'; - - _message_private_validate_fieldset( - $form['interval_limit'][$id][$limit_name], - $form['interval_limit'][$id][$interval_name], - $form_state['values'][$limit_name], - $form_state['values'][$interval_name] - ); - } -} - -/** - * Validate limit and interval values and show any errors on the form elements. - * - * @param mixed $limit_element - * Limit form element reference. - * @param mixed $interval_element - * Interval form element reference. - * @param string|int $limit - * Limit value to validate. - * @param string|int $interval - * Interval value to validate. - */ -function _message_private_validate_fieldset(&$limit_element, &$interval_element, $limit, $interval) { - // Validate role settings, check both textfields per fieldset are set. - if (!empty($limit) && !empty($interval)) { - // Check is numeric and between the boundaries. - if (!ctype_digit($limit) || $limit > MESSAGE_PRIVATE_MESSAGE_LIMIT_MAX || $limit < MESSAGE_PRIVATE_MESSAGE_LIMIT_MIN) { - form_error($limit_element, - t('Enter a numerical message limit between') . ' ' - . MESSAGE_PRIVATE_MESSAGE_LIMIT_MIN . ' - ' . MESSAGE_PRIVATE_MESSAGE_LIMIT_MAX . '.'); - } - // Check is numeric and between the boundaries. - if (!ctype_digit($interval) || $interval > MESSAGE_PRIVATE_MESSAGE_INTERVAL_MAX || $interval < MESSAGE_PRIVATE_MESSAGE_INTERVAL_MIN) { - form_error($interval_element, - t('Enter a numerical interval in minutes between') . ' ' - . MESSAGE_PRIVATE_MESSAGE_INTERVAL_MIN . ' - ' . MESSAGE_PRIVATE_MESSAGE_INTERVAL_MAX . '.'); - } - } - elseif (!empty($limit) || !empty($interval)) { - // Show error if only 1 textfield is set in each fieldset. - form_error($limit_element, t('Both a limit and interval value are required.')); - form_error($interval_element); - } -} diff --git a/message_private.module b/message_private.module index 0f4b764..d4a8bc3 100755 --- a/message_private.module +++ b/message_private.module @@ -93,41 +93,6 @@ function message_private_permission() { return $permissions; } -/** - * Implements hook_menu(). - */ -function message_private_menu() { - $items = array(); - - $items['admin/config/system/message-private'] = array( - 'title' => 'Message private settings', - 'description' => 'Configuration settings for message private module.', - 'page callback' => 'drupal_get_form', - 'page arguments' => array('message_private_admin_settings'), - 'file' => 'message_private.admin.inc', - 'access arguments' => array('administer message private'), - ); - - // Add default local task so the Messages view display shows Inbox tab. - $items['user/%/messages/inbox'] = array( - 'title' => 'Inbox', - 'description' => 'Message Private Inbox', - 'type' => MENU_DEFAULT_LOCAL_TASK, - ); - - // Create new non admin path to create messages. - $items['message/create/private-message'] = array( - 'title' => 'Private Message', - 'description' => 'Create a new message Private Message instance', - 'page callback' => 'drupal_get_form', - 'page arguments' => array('message_ui_instance_message_manage', 'private_message'), - 'access arguments' => array('create', 'private_message'), - 'access callback' => 'message_private_access_control', - ); - - return $items; -} - /** * Access callback for Messages tab. * diff --git a/message_private.routing.yml b/message_private.routing.yml index 5d8e10e..f21c4f0 100644 --- a/message_private.routing.yml +++ b/message_private.routing.yml @@ -1,6 +1,7 @@ message_private.admin_settings: path: '/admin/config/system/message-private' defaults: + _form: '\Drupal\message_private\Form\MessagePrivateSettingsForm' _title: 'Message Private settings' requirements: _permission: 'administer message private' diff --git a/src/Form/MessagePrivateSettingsForm.php b/src/Form/MessagePrivateSettingsForm.php new file mode 100644 index 0000000..91c2d3b --- /dev/null +++ b/src/Form/MessagePrivateSettingsForm.php @@ -0,0 +1,253 @@ +get('config.factory'), + $container->get('entity.manager') + ); + } + + /** + * Constructs a \Drupal\system\ConfigFormBase object. + * + * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory + * The factory for configuration objects. + * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager + * The entity manager object. + */ + public function __construct(ConfigFactoryInterface $config_factory, EntityManagerInterface $entity_manager) { + $this->setConfigFactory($config_factory); + $this->entityManager = $entity_manager; + } + + /** + * {@inheritdoc} + */ + public function buildForm(array $form, FormStateInterface $form_state) { + $config = $this->config('message_private.settings'); + + // Global email notifications on/off checkbox. + $form[MESSAGE_PRIVATE_EMAIL_NOTIFICATIONS] = array( + '#type' => 'checkbox', + '#title' => t('Message Private Email Notifications'), + '#default_value' => variable_get(MESSAGE_PRIVATE_EMAIL_NOTIFICATIONS, TRUE), + '#description' => t('Global On / Off checkbox for emails notifying users of a new private message'), + ); + + + // Role based message create limit on/off checkbox. + $form[MESSAGE_PRIVATE_MESSAGE_LIMIT] = array( + '#type' => 'checkbox', + '#title' => t('Limit Message Create By Role'), + '#default_value' => variable_get(MESSAGE_PRIVATE_MESSAGE_LIMIT, FALSE), + '#description' => t('Impose a message creation limit per interval. Users with multiple roles, get the highest limit from these roles'), + ); + + + // Conditional fieldset for all message limitation settings. + $form['interval_limit'] = array( + '#title' => t('Message interval limits'), + '#type' => 'fieldset', + '#collapsible' => TRUE, + '#collapsed' => TRUE, + '#states' => array( + 'invisible' => array( + ':input[name="' . MESSAGE_PRIVATE_MESSAGE_LIMIT . '"]' => array('checked' => FALSE), + ), + ), + ); + + // Add a default fieldset. + $form['interval_limit'][MESSAGE_PRIVATE_DEFAULT_INDEX] = array( + '#type' => 'fieldset', + '#title' => 'Default limit', + '#description' => t('Applies to all roles with blank entries below'), + '#collapsible' => TRUE, + '#collapsed' => FALSE, + ); + + $form['interval_limit'][MESSAGE_PRIVATE_DEFAULT_INDEX][MESSAGE_PRIVATE_DEFAULT_LIMIT] = array( + '#type' => 'textfield', + '#title' => t('Limit'), + '#default_value' => variable_get(MESSAGE_PRIVATE_DEFAULT_LIMIT), + '#description' => t('Enter a message limit') . ' ' . MESSAGE_PRIVATE_MESSAGE_LIMIT_MIN . ' - ' . MESSAGE_PRIVATE_MESSAGE_LIMIT_MAX, + ); + + + $form['interval_limit'][MESSAGE_PRIVATE_DEFAULT_INDEX][MESSAGE_PRIVATE_DEFAULT_INTERVAL] = array( + '#type' => 'textfield', + '#title' => t('Interval'), + '#default_value' => variable_get(MESSAGE_PRIVATE_DEFAULT_INTERVAL), + '#description' => t('Enter an interval in minutes') . ' ' . MESSAGE_PRIVATE_MESSAGE_INTERVAL_MIN . ' - ' . MESSAGE_PRIVATE_MESSAGE_INTERVAL_MAX, + ); + + + // Generate variable names for all roles used with get/set in admin form. + foreach (user_roles() as $id => $role) { + $role_name = str_replace(' ', '_', $role); + $limit_name = 'message_private_' . $role_name . '_limit'; + $interval_name = 'message_private_' . $role_name . '_interval'; + + $form['interval_limit'][$id] = array( + '#type' => 'fieldset', + '#title' => $role, + '#collapsible' => TRUE, + '#collapsed' => TRUE, + ); + + $form['interval_limit'][$id][$limit_name] = array( + '#type' => 'textfield', + '#title' => t('Limit'), + '#default_value' => variable_get($limit_name), + '#description' => t('Enter a message limit') . ' ' . MESSAGE_PRIVATE_MESSAGE_LIMIT_MIN . ' - ' . MESSAGE_PRIVATE_MESSAGE_LIMIT_MAX, + ); + + $form['interval_limit'][$id][$interval_name] = array( + '#type' => 'textfield', + '#title' => t('Interval'), + '#default_value' => variable_get($interval_name), + '#description' => t('Enter an interval in minutes') . ' ' . MESSAGE_PRIVATE_MESSAGE_INTERVAL_MIN . ' - ' . MESSAGE_PRIVATE_MESSAGE_INTERVAL_MAX, + ); + } + + return parent::buildForm($form, $form_state); + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + $config = $this->config('message_private.settings'); + + foreach ($this->defaultKeys() as $key) { + $config->set($key, $form_state->getValue($key)); + } + + $config->save(); + + parent::submitForm($form, $form_state); + } + + /** + * {@inheritdoc} + */ + public function validateForm(array &$form, FormStateInterface $form_state) { + if (strlen($form_state->getValue('phone_number')) < 3) { + $form_state->setErrorByName('phone_number', $this->t('The phone number is too short. Please enter a full phone number.')); + } + // Validate the default fieldset values. + $this->validateFieldset($form_state, + $form['interval_limit'][MESSAGE_PRIVATE_DEFAULT_INDEX][MESSAGE_PRIVATE_DEFAULT_LIMIT], + $form['interval_limit'][MESSAGE_PRIVATE_DEFAULT_INDEX][MESSAGE_PRIVATE_DEFAULT_INTERVAL], + $form_state->getValue(MESSAGE_PRIVATE_DEFAULT_LIMIT), + $form_state->getValue(MESSAGE_PRIVATE_DEFAULT_INTERVAL) + ); + // Cycle through the settings for each role and validate. + foreach (user_roles() as $id => $role) { + $role_name = str_replace(' ', '_', $role); + $limit_name = 'message_private_' . $role_name . '_limit'; + $interval_name = 'message_private_' . $role_name . '_interval'; + + $this->validateFieldset( + $form_state, + $form['interval_limit'][$id][$limit_name], + $form['interval_limit'][$id][$interval_name], + $form_state['values'][$limit_name], + $form_state['values'][$interval_name] + ); + } + } + + /** + * Validate limit and interval values and show any errors on the form elements. + * + * @param \Drupal\Core\Form\FormStateInterface $form_state + * @param mixed $limit_element + * Limit form element reference. + * @param mixed $interval_element + * Interval form element reference. + * @param string|int $limit + * Limit value to validate. + * @param string|int $interval + * Interval value to validate. + */ + private function validateFieldset(FormStateInterface $form_state, &$limit_element, &$interval_element, $limit, $interval) { + // Validate role settings, check both textfields per fieldset are set. + if (!empty($limit) && !empty($interval)) { + // Check is numeric and between the boundaries. + if (!ctype_digit($limit) || $limit > MESSAGE_PRIVATE_MESSAGE_LIMIT_MAX || $limit < MESSAGE_PRIVATE_MESSAGE_LIMIT_MIN) { + $form_state->setErrorByName($limit_element, + t('Enter a numerical message limit between') . ' ' + . MESSAGE_PRIVATE_MESSAGE_LIMIT_MIN . ' - ' . MESSAGE_PRIVATE_MESSAGE_LIMIT_MAX . '.'); + } + // Check is numeric and between the boundaries. + if (!ctype_digit($interval) || $interval > MESSAGE_PRIVATE_MESSAGE_INTERVAL_MAX || $interval < MESSAGE_PRIVATE_MESSAGE_INTERVAL_MIN) { + $form_state->setErrorByName($interval_element, + t('Enter a numerical interval in minutes between') . ' ' + . MESSAGE_PRIVATE_MESSAGE_INTERVAL_MIN . ' - ' . MESSAGE_PRIVATE_MESSAGE_INTERVAL_MAX . '.'); + } + } + elseif (!empty($limit) || !empty($interval)) { + // Show error if only 1 textfield is set in each fieldset. + $form_state->setErrorByName($limit_element, t('Both a limit and interval value are required.')); + $form_state->setErrorByName($interval_element); + } + } +}