diff --git a/web/profiles/custom/os2loop/modules/os2loop_flag_content/README.md b/web/profiles/custom/os2loop/modules/os2loop_flag_content/README.md index 04e0ab348..5ee1a2e42 100644 --- a/web/profiles/custom/os2loop/modules/os2loop_flag_content/README.md +++ b/web/profiles/custom/os2loop/modules/os2loop_flag_content/README.md @@ -2,4 +2,6 @@ This module makes users able to write the editorial office regarding specific content. -The config is defined in Flag Content Admin Form (/admin/config/os2loop_flag_content/settings). +The config can be edited in Configuration » OS2Loop » OS2Loop Flag content settings or: + +```(/admin/config/os2loop/os2loop_flag_content/settings)``` diff --git a/web/profiles/custom/os2loop/modules/os2loop_flag_content/os2loop_flag_content.routing.yml b/web/profiles/custom/os2loop/modules/os2loop_flag_content/os2loop_flag_content.routing.yml index 63839b34f..e18e1d5c6 100644 --- a/web/profiles/custom/os2loop/modules/os2loop_flag_content/os2loop_flag_content.routing.yml +++ b/web/profiles/custom/os2loop/modules/os2loop_flag_content/os2loop_flag_content.routing.yml @@ -1,13 +1,13 @@ os2loop_flag_content.settings: path: '/admin/config/os2loop/os2loop_flag_content/settings' defaults: - _form: '\Drupal\os2loop_flag_content\Form\FlagContentAdminForm' + _form: '\Drupal\os2loop_flag_content\Form\SettingsForm' _title: 'os2loop_flag_content' requirements: _permission: 'administer site configuration' -os2loop_flag_content.flag_content_form: - path: '/node/{node}/flag_content_form' +os2loop_flag_content.flag_content: + path: '/node/{node}/flag_content' defaults: _form: '\Drupal\os2loop_flag_content\Form\FlagContentForm' options: diff --git a/web/profiles/custom/os2loop/modules/os2loop_flag_content/os2loop_flag_content.services.yml b/web/profiles/custom/os2loop/modules/os2loop_flag_content/os2loop_flag_content.services.yml index 845655481..6c8182e13 100644 --- a/web/profiles/custom/os2loop/modules/os2loop_flag_content/os2loop_flag_content.services.yml +++ b/web/profiles/custom/os2loop/modules/os2loop_flag_content/os2loop_flag_content.services.yml @@ -5,3 +5,6 @@ services: - '@config.factory' os2loop_flag_content.mail_helper: class: Drupal\os2loop_flag_content\Helper\MailHelper + arguments: + - '@token' + - '@Drupal\os2loop_settings\Settings' diff --git a/web/profiles/custom/os2loop/modules/os2loop_flag_content/os2loop_flag_content.tokens.inc b/web/profiles/custom/os2loop/modules/os2loop_flag_content/os2loop_flag_content.tokens.inc new file mode 100644 index 000000000..44e36c3ab --- /dev/null +++ b/web/profiles/custom/os2loop/modules/os2loop_flag_content/os2loop_flag_content.tokens.inc @@ -0,0 +1,22 @@ +tokens($type, $tokens, $data); +} + +/** + * Implements hook_token_info(). + */ +function os2loop_flag_content_token_info() { + return Drupal::service('os2loop_flag_content.mail_helper')->tokenInfo(); +} diff --git a/web/profiles/custom/os2loop/modules/os2loop_flag_content/src/Form/FlagContentForm.php b/web/profiles/custom/os2loop/modules/os2loop_flag_content/src/Form/FlagContentForm.php index 846acad87..4bb053ace 100644 --- a/web/profiles/custom/os2loop/modules/os2loop_flag_content/src/Form/FlagContentForm.php +++ b/web/profiles/custom/os2loop/modules/os2loop_flag_content/src/Form/FlagContentForm.php @@ -12,6 +12,7 @@ use Drupal\Core\Mail\MailManagerInterface; use Drupal\Core\Session\AccountProxyInterface; use Drupal\Core\Url; +use Symfony\Component\HttpFoundation\RedirectResponse; /** * Flag content form. @@ -55,7 +56,7 @@ class FlagContentForm extends FormBase implements ContainerInjectionInterface { protected $messenger; /** - * Constructs an flag content form. + * Constructs a flag content form. * * @param \Drupal\Core\Routing\RouteMatchInterface $routeMatcher * The route matcher. @@ -126,6 +127,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { ]; $form['actions']['#type'] = 'actions'; + $form['actions']['submit'] = [ '#type' => 'submit', '#value' => $this->t('Send'), @@ -138,7 +140,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { $form['actions']['cancel'] = [ '#type' => 'link', - '#url' => new Url('entity.node.canonical', ['node' => $node->id()]), + '#url' => $node->toUrl(), '#title' => $this->t('Cancel'), '#attributes' => [ 'class' => [ @@ -150,11 +152,6 @@ public function buildForm(array $form, FormStateInterface $form_state) { return $form; } - /** - * {@inheritdoc} - */ - public function validateForm(array &$form, FormStateInterface $form_state) {} - /** * {@inheritdoc} */ @@ -167,15 +164,18 @@ public function submitForm(array &$form, FormStateInterface $form_state) { $key = 'flag_content'; $params['reason'] = $form_state->getValue('reason'); $params['message'] = $message; - $params['node_title'] = $node->label(); + $params['node'] = $node; $langcode = $this->currentUser->getPreferredLangcode(); $send = TRUE; $result = $this->mailManager->mail($module, $key, $to, $langcode, $params, NULL, $send); if ($result['result'] !== TRUE) { - $this->messenger->addMessage($this->t('There was a problem sending your message and it was not sent.'), 'error'); + $this->messenger->addError($this->t('There was a problem sending your message and it was not sent.')); } else { - $this->messenger->addError($this->t('Your message has been sent.')); + $this->messenger->addStatus($this->t('Your message has been sent.')); + $redirectUrl = Url::fromRoute('entity.node.canonical', ['node' => $node->id()])->toString(); + $response = new RedirectResponse($redirectUrl); + $response->send(); } } diff --git a/web/profiles/custom/os2loop/modules/os2loop_flag_content/src/Form/FlagContentAdminForm.php b/web/profiles/custom/os2loop/modules/os2loop_flag_content/src/Form/SettingsForm.php similarity index 55% rename from web/profiles/custom/os2loop/modules/os2loop_flag_content/src/Form/FlagContentAdminForm.php rename to web/profiles/custom/os2loop/modules/os2loop_flag_content/src/Form/SettingsForm.php index 8fa5c9cd7..b88241ee1 100644 --- a/web/profiles/custom/os2loop/modules/os2loop_flag_content/src/Form/FlagContentAdminForm.php +++ b/web/profiles/custom/os2loop/modules/os2loop_flag_content/src/Form/SettingsForm.php @@ -8,14 +8,14 @@ /** * Configure flag content admin settings for this site. */ -class FlagContentAdminForm extends ConfigFormBase { +class SettingsForm extends ConfigFormBase { /** * Config settings. * * @var string */ - const SETTINGS = 'os2loop_flag_content.settings'; + const SETTINGS_NAME = 'os2loop_flag_content.settings'; /** * {@inheritdoc} @@ -29,7 +29,7 @@ public function getFormId() { */ protected function getEditableConfigNames() { return [ - static::SETTINGS, + static::SETTINGS_NAME, ]; } @@ -37,7 +37,7 @@ protected function getEditableConfigNames() { * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { - $config = $this->config(static::SETTINGS); + $config = $this->config(static::SETTINGS_NAME); $form['reasons'] = [ '#type' => 'textarea', @@ -52,6 +52,30 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#default_value' => $config->get('to_email'), ]; + $form['email_template'] = [ + '#type' => 'textarea', + '#required' => TRUE, + '#title' => $this->t('Email template for flag content body'), + '#default_value' => $config->get('template_body'), + ]; + + $form['email_template_tokens'] = [ + '#theme' => 'token_tree_link', + '#token_types' => ['user', 'node', 'os2loop_flag_content'], + ]; + + $form['subject_template'] = [ + '#type' => 'textarea', + '#required' => TRUE, + '#title' => $this->t('Subject template for flag content subject'), + '#default_value' => $config->get('template_subject'), + ]; + + $form['subject_template_tokens'] = [ + '#theme' => 'token_tree_link', + '#token_types' => ['user', 'node', 'os2loop_flag_content'], + ]; + return parent::buildForm($form, $form_state); } @@ -61,10 +85,11 @@ public function buildForm(array $form, FormStateInterface $form_state) { public function submitForm(array &$form, FormStateInterface $form_state) { // Retrieve the configuration. $form_state->getValue('content_types'); - $this->configFactory->getEditable(static::SETTINGS) + $this->configFactory->getEditable(static::SETTINGS_NAME) ->set('reasons', $form_state->getValue('reasons')) - ->set('content_types', $form_state->getValue('content_types')) ->set('to_email', $form_state->getValue('to_email')) + ->set('template_subject', $form_state->getValue('subject_template')) + ->set('template_body', $form_state->getValue('email_template')) ->save(); parent::submitForm($form, $form_state); diff --git a/web/profiles/custom/os2loop/modules/os2loop_flag_content/src/Helper/MailHelper.php b/web/profiles/custom/os2loop/modules/os2loop_flag_content/src/Helper/MailHelper.php index 06a7f5eb8..f538f29d8 100644 --- a/web/profiles/custom/os2loop/modules/os2loop_flag_content/src/Helper/MailHelper.php +++ b/web/profiles/custom/os2loop/modules/os2loop_flag_content/src/Helper/MailHelper.php @@ -3,30 +3,108 @@ namespace Drupal\os2loop_flag_content\Helper; use Drupal\Core\StringTranslation\StringTranslationTrait; +use Drupal\Core\Utility\Token; +use Drupal\os2loop_flag_content\Form\SettingsForm; +use Drupal\os2loop_settings\Settings; /** * MailHelper for creating mail templates. */ class MailHelper { use StringTranslationTrait; + /** + * The toke. + * + * @var \Drupal\Core\Utility\Token + */ + protected $token; + + /** + * The config. + * + * @var \Drupal\Core\Config\ImmutableConfig + */ + private $config; + + /** + * Constructor. + */ + public function __construct(Token $token, Settings $settings) { + $this->token = $token; + $this->config = $settings->getConfig(SettingsForm::SETTINGS_NAME); + } /** * Implements hook_mail(). */ public function mail($key, &$message, $params) { - $options = [ - 'langcode' => $message['langcode'], - ]; switch ($key) { case 'flag_content': - $subject = $this->t('Flagged content: @reason with @title', [ - '@title' => $params['node_title'], - '@reason' => $params['reason'], - ], $options); + $body_template = $this->config->get('template_body'); + $subject_template = $this->config->get('template_subject'); + $data = [ + 'node' => $params['node'], + 'os2loop_flag_content' => [ + 'message' => $params['message'], + 'reason' => $params['reason'], + ], + ]; + $body = $this->renderTemplate($body_template, $data); + $subject = $this->renderTemplate($subject_template, $data); $message['subject'] = $subject; - $message['body'][] = $params['message']; + $message['body'][] = $body; break; } } + /** + * Renders content of a mail. + */ + public function renderTemplate($template, array $data) { + return $this->token->replace($template, $data, []); + + } + + /** + * Implements hook_tokens(). + */ + public function tokens($type, $tokens, array $data) { + $replacements = []; + if ('os2loop_flag_content' === $type && isset($data[$type])) { + foreach ($tokens as $name => $original) { + if (isset($data[$type][$name])) { + $replacements[$original] = $data[$type][$name]; + } + } + } + return $replacements; + } + + /** + * Implements hook_token_info(). + */ + public function tokenInfo() { + return [ + 'types' => [ + 'os2loop_flag_content' => [ + 'name' => $this->t('Flag content'), + 'description' => $this->t('Tokens related to flag content.'), + 'needs-data' => 'os2loop_flag_content', + ], + ], + 'tokens' => [ + 'os2loop_flag_content' => [ + 'message' => [ + 'name' => $this->t('The message'), + 'description' => $this->t('The message.'), + ], + 'reason' => [ + 'name' => $this->t('The reason'), + 'description' => $this->t('The reason.'), + ], + ], + ], + ]; + } + } diff --git a/web/profiles/custom/os2loop/themes/os2loop_theme/templates/navigation/content-tasks.html.twig b/web/profiles/custom/os2loop/themes/os2loop_theme/templates/navigation/content-tasks.html.twig index 510a29a7b..5576042ea 100644 --- a/web/profiles/custom/os2loop/themes/os2loop_theme/templates/navigation/content-tasks.html.twig +++ b/web/profiles/custom/os2loop/themes/os2loop_theme/templates/navigation/content-tasks.html.twig @@ -16,7 +16,7 @@ {% endif %} {% if is_granted('contact editorial office', node) %}
{% endif %} {% if is_granted('share with a friend', node) %}