From 72bce26706afb1acbfdca69a7058a535308253ff Mon Sep 17 00:00:00 2001 From: Mark Fullmer Date: Sun, 4 Oct 2020 09:58:57 -0700 Subject: [PATCH] Ability to prepend text to subject line (#2) * Get user roles from loaded user * Add ability to prepend text to subject line through configuration --- rest_feedback_endpoint.module | 2 +- src/Form/Settings.php | 7 +++++++ src/Plugin/rest/resource/SubmitIssue.php | 6 +++--- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/rest_feedback_endpoint.module b/rest_feedback_endpoint.module index 8cf801f..727beb4 100644 --- a/rest_feedback_endpoint.module +++ b/rest_feedback_endpoint.module @@ -10,7 +10,7 @@ function rest_feedback_endpoint_mail($key, &$message, $params) { switch ($key) { case 'rest_feedback_endpoint': - $message['subject'] = t('Crow interface bug report: @title', ['@title' => $params['title']], $options); + $message['subject'] = t('@title', ['@title' => $params['title']], $options); $message['body'][] = $params['message']; break; } diff --git a/src/Form/Settings.php b/src/Form/Settings.php index 75b31b5..0a5193d 100644 --- a/src/Form/Settings.php +++ b/src/Form/Settings.php @@ -49,6 +49,12 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#description' => $this->t('Single email only'), '#default_value' => $config->get('notification_email'), ]; + $form['subject_line_prefix'] = [ + '#type' => 'textfield', + '#title' => $this->t('Subject line prefix'), + '#description' => $this->t('Prepend the subject line of the email (e.g. "Mysite user feedback: ".'), + '#default_value' => $config->get('subject_line_prefix'), + ]; return parent::buildForm($form, $form_state); } @@ -60,6 +66,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) { $this->configFactory->getEditable(static::SETTINGS) ->set('on', $form_state->getValue('on')) ->set('notification_email', $form_state->getValue('notification_email')) + ->set('subject_line_prefix', $form_state->getValue('subject_line_prefix')) ->save(); parent::submitForm($form, $form_state); } diff --git a/src/Plugin/rest/resource/SubmitIssue.php b/src/Plugin/rest/resource/SubmitIssue.php index 22a290b..f819375 100644 --- a/src/Plugin/rest/resource/SubmitIssue.php +++ b/src/Plugin/rest/resource/SubmitIssue.php @@ -84,8 +84,8 @@ public function post($data) { return new ResourceResponse($response_status); } if (!empty($data['title']) && !empty($data['description'])) { - $roles = $this->currentUser->getRoles(); $user = User::load($this->currentUser->id()); + $roles = $user->getRoles(); $name = $user->get('field_full_name'); $name = $name[0]['value'] ?? $this->currentUser->getUsername(); $reported_roles = array_diff($roles, ['authenticated', 'administrator']); @@ -94,7 +94,7 @@ public function post($data) { $module = 'rest_feedback_endpoint'; $key = 'rest_feedback_endpoint'; $to = $config->get('notification_email'); - $params['message'] = 'The user ' . $name . ' has reported an issue with the Crow web interface.' . PHP_EOL . PHP_EOL; + $params['message'] = 'The user ' . $name . ' has reported an issue with the interface.' . PHP_EOL . PHP_EOL; $params['message'] .= 'SOURCE PAGE: ' . $data['url'] . PHP_EOL . PHP_EOL; $params['message'] .= 'DESCRIPTION: ' . Html::escape($data['description']) . PHP_EOL . PHP_EOL; $params['message'] .= 'USER ACCESS LEVEL: ' . implode(', ', $reported_roles) . PHP_EOL . PHP_EOL; @@ -106,7 +106,7 @@ public function post($data) { else { $params['message'] .= 'CONTACT USER WITH UPDATES ABOUT THE ISSUE: no' . PHP_EOL . PHP_EOL; } - $params['title'] = Html::escape($data['title']); + $params['title'] = $config->get('subject_line_prefix') . Html::escape($data['title']); $langcode = \Drupal::currentUser()->getPreferredLangcode(); $send = TRUE; $response_status['status'] = $mailManager->mail($module, $key, $to, $langcode, $params, NULL, $send);