From a287956cf7fff06a6a3e7adfb27feee2bc0362ad Mon Sep 17 00:00:00 2001 From: Sine Date: Thu, 6 May 2021 08:59:00 +0200 Subject: [PATCH 01/10] LOOP-871: create module for sharing questions --- .../os2loop_share_with_a_friend/README.md | 3 + .../os2loop_share_with_a_friend.info.yml | 5 + .../os2loop_share_with_a_friend.module | 13 ++ .../os2loop_share_with_a_friend.routing.yml | 10 ++ .../os2loop_share_with_a_friend.services.yml | 3 + .../src/Form/ShareWithAFriendForm.php | 164 ++++++++++++++++++ .../src/Helper/MailHelper.php | 35 ++++ 7 files changed, 233 insertions(+) create mode 100644 web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/README.md create mode 100644 web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/os2loop_share_with_a_friend.info.yml create mode 100644 web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/os2loop_share_with_a_friend.module create mode 100644 web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/os2loop_share_with_a_friend.routing.yml create mode 100644 web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/os2loop_share_with_a_friend.services.yml create mode 100644 web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/src/Form/ShareWithAFriendForm.php create mode 100644 web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/src/Helper/MailHelper.php diff --git a/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/README.md b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/README.md new file mode 100644 index 000000000..049c0f6ac --- /dev/null +++ b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/README.md @@ -0,0 +1,3 @@ +# OS2Loop share with a friend + +This module makes users able to share questions with friends pr email. diff --git a/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/os2loop_share_with_a_friend.info.yml b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/os2loop_share_with_a_friend.info.yml new file mode 100644 index 000000000..e4d5626ff --- /dev/null +++ b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/os2loop_share_with_a_friend.info.yml @@ -0,0 +1,5 @@ +name: 'OS2Loop share with a friend' +type: module +description: 'OS2Loop share with a friend' +core_version_requirement: ^9 +package: 'OS2Loop' diff --git a/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/os2loop_share_with_a_friend.module b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/os2loop_share_with_a_friend.module new file mode 100644 index 000000000..024173ccd --- /dev/null +++ b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/os2loop_share_with_a_friend.module @@ -0,0 +1,13 @@ +mail($key, $message, $params); +} diff --git a/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/os2loop_share_with_a_friend.routing.yml b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/os2loop_share_with_a_friend.routing.yml new file mode 100644 index 000000000..1701ee966 --- /dev/null +++ b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/os2loop_share_with_a_friend.routing.yml @@ -0,0 +1,10 @@ +os2loop_share_with_a_friend.share_with_a_friend_form: + path: '/node/{node}/share_with_a_friend_form' + defaults: + _form: '\Drupal\os2loop_share_with_a_friend\Form\ShareWithAFriendForm' + options: + parameters: + node: + type: entity:node + requirements: + _permission: 'access content' diff --git a/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/os2loop_share_with_a_friend.services.yml b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/os2loop_share_with_a_friend.services.yml new file mode 100644 index 000000000..6db608821 --- /dev/null +++ b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/os2loop_share_with_a_friend.services.yml @@ -0,0 +1,3 @@ +services: + os2loop_share_with_a_friend.mail_helper: + class: Drupal\os2loop_share_with_a_friend\Helper\MailHelper diff --git a/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/src/Form/ShareWithAFriendForm.php b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/src/Form/ShareWithAFriendForm.php new file mode 100644 index 000000000..0c301b10a --- /dev/null +++ b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/src/Form/ShareWithAFriendForm.php @@ -0,0 +1,164 @@ +routeMatcher = $routeMatcher; + $this->mailManager = $mailManager; + $this->currentUser = $currentUser; + $this->messenger = $messenger; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('current_route_match'), + $container->get('plugin.manager.mail'), + $container->get('current_user'), + $container->get('messenger'), + ); + } + + /** + * {@inheritdoc} + */ + public function getFormId() { + return 'share_with_a_friend_button'; + } + + /** + * {@inheritdoc} + */ + public function buildForm(array $form, FormStateInterface $form_state) { + $node = $this->routeMatcher->getParameter('node'); + + $form['title'] = [ + '#type' => 'page_title', + '#title' => $this->t('Share the following with a friend: @document', ['@document' => $node->label()]), + ]; + + $form['message'] = [ + '#type' => 'textarea', + '#title' => $this->t('Message'), + '#description' => $this->t('Write a message to the recipient'), + '#required' => TRUE, + ]; + + $form['actions']['#type'] = 'actions'; + $form['actions']['submit'] = [ + '#type' => 'submit', + '#value' => $this->t('Send'), + '#attributes' => [ + 'class' => [ + 'button', + ], + ], + ]; + + $form['to_email'] = [ + '#type' => 'email', + '#title' => $this->t('Email address of recipient'), + ]; + + $form['actions']['cancel'] = [ + '#type' => 'link', + '#url' => new Url('entity.node.canonical', ['node' => $node->id()]), + '#title' => $this->t('Cancel'), + '#attributes' => [ + 'class' => [ + 'button', + ], + ], + ]; + + return $form; + } + + /** + * {@inheritdoc} + */ + public function validateForm(array &$form, FormStateInterface $form_state) {} + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + $node = $this->routeMatcher->getParameter('node'); + $message = $form_state->getValue('message'); + $to = $form_state->getValue('to_email'); + $module = 'os2loop_share_with_a_friend'; + $key = 'share_with_a_friend'; + $params['message'] = $message; + $url = Url::fromRoute('entity.node.canonical', ['node' => $node->id()]); + $params['url'] = $url->toString(); + $params['node_title'] = $node->label(); + $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'); + } + else { + $this->messenger->addError($this->t('Your message has been sent.')); + } + } + +} diff --git a/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/src/Helper/MailHelper.php b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/src/Helper/MailHelper.php new file mode 100644 index 000000000..920188b0d --- /dev/null +++ b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/src/Helper/MailHelper.php @@ -0,0 +1,35 @@ + $message['langcode'], + ]; + switch ($key) { + case 'share_with_a_friend': + + $messageString = $this->t('Someone shared the following content with you: @node_title with the following message', [ + '@node_title' => $params['node_title'], + ':url' => $params['url'], + ], $options); + $subject = $this->t('Someone shared content with you!', $options); + $message['subject'] = $subject; + $message['body'][] = $messageString; + $message['body'][] = $params['message']; + break; + } + } + +} From 9e99f247089ec2a09c0ee7f8e5b13313a2bd9f46 Mon Sep 17 00:00:00 2001 From: Sine Date: Thu, 6 May 2021 14:18:28 +0200 Subject: [PATCH 02/10] LOOP-871: used tokens for email template --- .../os2loop_share_with_a_friend.services.yml | 2 ++ .../src/Form/ShareWithAFriendForm.php | 4 +-- .../src/Helper/MailHelper.php | 30 +++++++++++++++---- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/os2loop_share_with_a_friend.services.yml b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/os2loop_share_with_a_friend.services.yml index 6db608821..668bd31ba 100644 --- a/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/os2loop_share_with_a_friend.services.yml +++ b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/os2loop_share_with_a_friend.services.yml @@ -1,3 +1,5 @@ services: os2loop_share_with_a_friend.mail_helper: class: Drupal\os2loop_share_with_a_friend\Helper\MailHelper + arguments: + - '@token' diff --git a/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/src/Form/ShareWithAFriendForm.php b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/src/Form/ShareWithAFriendForm.php index 0c301b10a..c2b07954d 100644 --- a/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/src/Form/ShareWithAFriendForm.php +++ b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/src/Form/ShareWithAFriendForm.php @@ -147,9 +147,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) { $module = 'os2loop_share_with_a_friend'; $key = 'share_with_a_friend'; $params['message'] = $message; - $url = Url::fromRoute('entity.node.canonical', ['node' => $node->id()]); - $params['url'] = $url->toString(); - $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); diff --git a/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/src/Helper/MailHelper.php b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/src/Helper/MailHelper.php index 920188b0d..0620161ff 100644 --- a/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/src/Helper/MailHelper.php +++ b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/src/Helper/MailHelper.php @@ -3,12 +3,30 @@ namespace Drupal\os2loop_share_with_a_friend\Helper; use Drupal\Core\StringTranslation\StringTranslationTrait; +use Drupal\Core\Render\BubbleableMetadata; +use Drupal\Core\Utility\Token; /** * MailHelper for creating mail templates. */ class MailHelper { use StringTranslationTrait; + /** + * The toke. + * + * @var \Drupal\Core\Utility\Token + */ + protected $token; + + /** + * Constructor. + * + * @param \Drupal\Core\Utility\Token $token + * The token. + */ + public function __construct(Token $token) { + $this->token = $token; + } /** * Implements hook_mail(). @@ -19,14 +37,16 @@ public function mail($key, &$message, $params) { ]; switch ($key) { case 'share_with_a_friend': + $node = $params['node']; + // @todo move this to settings + $template = '[current-user:name] shared the following with you: [node:title]([node:url]) with the following message'; + $bubbleable_metadata = new BubbleableMetadata(); + $build['#markup'] = $this->token->replace($template, ['node' => $node], [], $bubbleable_metadata); + $bubbleable_metadata->applyTo($build); - $messageString = $this->t('Someone shared the following content with you: @node_title with the following message', [ - '@node_title' => $params['node_title'], - ':url' => $params['url'], - ], $options); $subject = $this->t('Someone shared content with you!', $options); $message['subject'] = $subject; - $message['body'][] = $messageString; + $message['body'][] = $build['#markup']; $message['body'][] = $params['message']; break; } From eb8904eeeb4d45aaea1d9fc2f138fa29429567d1 Mon Sep 17 00:00:00 2001 From: Sine Date: Thu, 6 May 2021 14:25:55 +0200 Subject: [PATCH 03/10] LOOP-871: add share with a friend to contenttasks --- .../os2loop_theme/templates/navigation/content-tasks.html.twig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 93ec1c961..9b4c79d37 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 @@ -21,7 +21,7 @@ {% endif %} {% if is_granted('share with a friend', node) %} {% endif %} {% if is_granted('update', node) %} From 40395631db0f203d9dd43f46e1e00a3eaa257b4e Mon Sep 17 00:00:00 2001 From: Sine Date: Thu, 6 May 2021 14:46:58 +0200 Subject: [PATCH 04/10] LOOP-871: enabled the module --- config/sync/core.extension.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/config/sync/core.extension.yml b/config/sync/core.extension.yml index 9e819f56e..88ba76b51 100644 --- a/config/sync/core.extension.yml +++ b/config/sync/core.extension.yml @@ -47,6 +47,7 @@ module: os2loop_search_db: 0 os2loop_section_page: 0 os2loop_settings: 0 + os2loop_share_with_a_friend: 0 os2loop_shared: 0 os2loop_subscriptions: 0 os2loop_taxonomy: 0 From 3add6df24643e8d29a3091d3a98bf94a6a2e07fd Mon Sep 17 00:00:00 2001 From: Sine Date: Fri, 7 May 2021 09:54:43 +0200 Subject: [PATCH 05/10] LOOP-871: changes from comments on pr --- .../os2loop_share_with_a_friend/README.md | 2 +- .../os2loop_share_with_a_friend.routing.yml | 4 +-- .../src/Form/ShareWithAFriendForm.php | 31 +++++++++---------- 3 files changed, 18 insertions(+), 19 deletions(-) diff --git a/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/README.md b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/README.md index 049c0f6ac..fc8f0b8c1 100644 --- a/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/README.md +++ b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/README.md @@ -1,3 +1,3 @@ # OS2Loop share with a friend -This module makes users able to share questions with friends pr email. +This module makes users able to share content with friends pr email. diff --git a/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/os2loop_share_with_a_friend.routing.yml b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/os2loop_share_with_a_friend.routing.yml index 1701ee966..7f28fc7d4 100644 --- a/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/os2loop_share_with_a_friend.routing.yml +++ b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/os2loop_share_with_a_friend.routing.yml @@ -1,5 +1,5 @@ -os2loop_share_with_a_friend.share_with_a_friend_form: - path: '/node/{node}/share_with_a_friend_form' +os2loop_share_with_a_friend.share_with_a_friend: + path: '/node/{node}/share_with_a_friend' defaults: _form: '\Drupal\os2loop_share_with_a_friend\Form\ShareWithAFriendForm' options: diff --git a/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/src/Form/ShareWithAFriendForm.php b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/src/Form/ShareWithAFriendForm.php index c2b07954d..2639f1add 100644 --- a/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/src/Form/ShareWithAFriendForm.php +++ b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/src/Form/ShareWithAFriendForm.php @@ -11,9 +11,10 @@ use Drupal\Core\Mail\MailManagerInterface; use Drupal\Core\Session\AccountProxyInterface; use Drupal\Core\Url; +use Symfony\Component\HttpFoundation\RedirectResponse; /** - * Share with a friend from. + * Share with a friend form. * * @package Drupal\os2loop_share_with_a_friend\Form */ @@ -102,8 +103,12 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#required' => TRUE, ]; - $form['actions']['#type'] = 'actions'; - $form['actions']['submit'] = [ + $form['to_email'] = [ + '#type' => 'email', + '#required' => TRUE, + '#title' => $this->t('Email address of recipient'), + ]; + $form['submit'] = [ '#type' => 'submit', '#value' => $this->t('Send'), '#attributes' => [ @@ -113,12 +118,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { ], ]; - $form['to_email'] = [ - '#type' => 'email', - '#title' => $this->t('Email address of recipient'), - ]; - - $form['actions']['cancel'] = [ + $form['cancel'] = [ '#type' => 'link', '#url' => new Url('entity.node.canonical', ['node' => $node->id()]), '#title' => $this->t('Cancel'), @@ -132,11 +132,6 @@ public function buildForm(array $form, FormStateInterface $form_state) { return $form; } - /** - * {@inheritdoc} - */ - public function validateForm(array &$form, FormStateInterface $form_state) {} - /** * {@inheritdoc} */ @@ -152,11 +147,15 @@ public function submitForm(array &$form, FormStateInterface $form_state) { $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.'), 'error'); } 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(); } } From 2f1778ca52118f4f4b95325cb544dc8103188c48 Mon Sep 17 00:00:00 2001 From: Sine Date: Mon, 10 May 2021 14:24:49 +0200 Subject: [PATCH 06/10] LOOP-871: readded actions and used adderror --- .../src/Form/ShareWithAFriendForm.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/src/Form/ShareWithAFriendForm.php b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/src/Form/ShareWithAFriendForm.php index 2639f1add..2cda8e349 100644 --- a/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/src/Form/ShareWithAFriendForm.php +++ b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/src/Form/ShareWithAFriendForm.php @@ -108,7 +108,8 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#required' => TRUE, '#title' => $this->t('Email address of recipient'), ]; - $form['submit'] = [ + + $form['actions']['submit'] = [ '#type' => 'submit', '#value' => $this->t('Send'), '#attributes' => [ @@ -117,8 +118,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { ], ], ]; - - $form['cancel'] = [ + $form['actions']['cancel'] = [ '#type' => 'link', '#url' => new Url('entity.node.canonical', ['node' => $node->id()]), '#title' => $this->t('Cancel'), @@ -128,6 +128,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { ], ], ]; + $form['actions']['#type'] = 'actions'; return $form; } @@ -147,7 +148,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) { $send = TRUE; $result = $this->mailManager->mail($module, $key, $to, $langcode, $params, NULL, $send); if ($result['result'] !== TRUE) { - $this->messenger->addError($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->addStatus($this->t('Your message has been sent.')); From acc15be84f25f11e045f12cf8e0a12fa509a1a99 Mon Sep 17 00:00:00 2001 From: Sine Date: Mon, 10 May 2021 14:26:13 +0200 Subject: [PATCH 07/10] LOOP-871: added token for email message --- .../os2loop_share_with_a_friend.tokens.inc | 22 ++++++ .../src/Helper/MailHelper.php | 71 +++++++++++++++---- 2 files changed, 81 insertions(+), 12 deletions(-) create mode 100644 web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/os2loop_share_with_a_friend.tokens.inc diff --git a/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/os2loop_share_with_a_friend.tokens.inc b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/os2loop_share_with_a_friend.tokens.inc new file mode 100644 index 000000000..93f32b850 --- /dev/null +++ b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/os2loop_share_with_a_friend.tokens.inc @@ -0,0 +1,22 @@ +tokens($type, $tokens, $data); +} + +/** + * Implements hook_token_info(). + */ +function os2loop_share_with_a_friend_token_info() { + return Drupal::service('os2loop_share_with_a_friend.mail_helper')->tokenInfo(); +} diff --git a/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/src/Helper/MailHelper.php b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/src/Helper/MailHelper.php index 0620161ff..160094018 100644 --- a/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/src/Helper/MailHelper.php +++ b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/src/Helper/MailHelper.php @@ -3,7 +3,6 @@ namespace Drupal\os2loop_share_with_a_friend\Helper; use Drupal\Core\StringTranslation\StringTranslationTrait; -use Drupal\Core\Render\BubbleableMetadata; use Drupal\Core\Utility\Token; /** @@ -32,24 +31,72 @@ public function __construct(Token $token) { * Implements hook_mail(). */ public function mail($key, &$message, $params) { - $options = [ - 'langcode' => $message['langcode'], - ]; switch ($key) { case 'share_with_a_friend': $node = $params['node']; // @todo move this to settings - $template = '[current-user:name] shared the following with you: [node:title]([node:url]) with the following message'; - $bubbleable_metadata = new BubbleableMetadata(); - $build['#markup'] = $this->token->replace($template, ['node' => $node], [], $bubbleable_metadata); - $bubbleable_metadata->applyTo($build); - - $subject = $this->t('Someone shared content with you!', $options); + $template = '[current-user:name] shared the following with you: [node:title] ([node:url]) with the following message: [os2loop_share_with_a_friend:message]'; + $data['node'] = $node; + $data['message'] = $params['message']; + $body = $this->renderTemplate($template, $data); + $template = '[current-user:name] wants to share content from [site:name] with you'; + $subject = $this->renderTemplate($template); $message['subject'] = $subject; - $message['body'][] = $build['#markup']; - $message['body'][] = $params['message']; + $message['body'][] = $body; break; } } + /** + * Renders content of a mail. + */ + public function renderTemplate($template, array $data = NULL) { + if (isset($data)) { + return $this->token->replace($template, [ + 'node' => $data['node'], + 'message' => $data['message'], + ], []); + } + else { + return $this->token->replace($template, [], []); + } + } + + /** + * Implements hook_tokens(). + */ + public function tokens($type, $tokens, array $data) { + $replacements = []; + if ($type == 'os2loop_share_with_a_friend' && !empty($data['message'])) { + foreach ($tokens as $name => $original) { + switch ($name) { + case 'message': + $replacements[$original] = $data['message']; + break; + } + } + } + return $replacements; + } + + /** + * Implements hook_token_info(). + */ + public function tokenInfo() { + $types['os2loop_share_with_a_friend'] = [ + 'name' => $this->t('Message type'), + ]; + $tokens['message'] = [ + 'name' => $this->t('Message'), + ]; + + return [ + 'types' => $types, + 'tokens' => [ + 'os2loop_share_with_a_friend' => $tokens, + ], + ]; + + } + } From 1f4fd19578e80565c67d275a0f41844cff448bb0 Mon Sep 17 00:00:00 2001 From: Sine Date: Mon, 17 May 2021 17:31:24 +0200 Subject: [PATCH 08/10] LOOP-871 added template in yml and settingsform --- .../os2loop_share_with_a_friend.settings.yml | 2 + .../os2loop_share_with_a_friend.info.yml | 2 + ...os2loop_share_with_a_friend.links.menu.yml | 6 ++ .../os2loop_share_with_a_friend.routing.yml | 8 ++ .../os2loop_share_with_a_friend.services.yml | 1 + .../src/Form/SettingsForm.php | 99 +++++++++++++++++++ .../src/Form/ShareWithAFriendForm.php | 6 +- .../src/Helper/MailHelper.php | 25 +++-- .../navigation/content-tasks.html.twig | 2 +- 9 files changed, 138 insertions(+), 13 deletions(-) create mode 100644 config/sync/os2loop_share_with_a_friend.settings.yml create mode 100644 web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/os2loop_share_with_a_friend.links.menu.yml create mode 100644 web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/src/Form/SettingsForm.php diff --git a/config/sync/os2loop_share_with_a_friend.settings.yml b/config/sync/os2loop_share_with_a_friend.settings.yml new file mode 100644 index 000000000..9cb0191a6 --- /dev/null +++ b/config/sync/os2loop_share_with_a_friend.settings.yml @@ -0,0 +1,2 @@ +template_subject: '[current-user:name] wants to share content from [site:name] with you' +template_body: '[current-user:name] shared the following with you: [node:title] ([node:url]) with the following message: [os2loop_share_with_a_friend:message]' diff --git a/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/os2loop_share_with_a_friend.info.yml b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/os2loop_share_with_a_friend.info.yml index e4d5626ff..6ccded151 100644 --- a/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/os2loop_share_with_a_friend.info.yml +++ b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/os2loop_share_with_a_friend.info.yml @@ -3,3 +3,5 @@ type: module description: 'OS2Loop share with a friend' core_version_requirement: ^9 package: 'OS2Loop' +dependencies: + - drupal:os2loop_settings diff --git a/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/os2loop_share_with_a_friend.links.menu.yml b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/os2loop_share_with_a_friend.links.menu.yml new file mode 100644 index 000000000..3486d3f5d --- /dev/null +++ b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/os2loop_share_with_a_friend.links.menu.yml @@ -0,0 +1,6 @@ +os2loop_share_with_a_friend.settings: + title: 'OS2Loop Share With A Friend settings' + route_name: os2loop_share_with_a_friend.settings + description: 'Configure OS2Loop Share With A Friend settings' + parent: os2loop.group.admin + weight: 10 diff --git a/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/os2loop_share_with_a_friend.routing.yml b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/os2loop_share_with_a_friend.routing.yml index 7f28fc7d4..e3bbeb603 100644 --- a/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/os2loop_share_with_a_friend.routing.yml +++ b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/os2loop_share_with_a_friend.routing.yml @@ -8,3 +8,11 @@ os2loop_share_with_a_friend.share_with_a_friend: type: entity:node requirements: _permission: 'access content' + +os2loop_share_with_a_friend.settings: + path: '/admin/config/os2loop/os2loop_share_with_a_friend/settings' + defaults: + _form: '\Drupal\os2loop_share_with_a_friend\Form\SettingsForm' + _title: 'OS2Loop share with a friend settings' + requirements: + _permission: 'administer site configuration' diff --git a/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/os2loop_share_with_a_friend.services.yml b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/os2loop_share_with_a_friend.services.yml index 668bd31ba..a9c78527f 100644 --- a/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/os2loop_share_with_a_friend.services.yml +++ b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/os2loop_share_with_a_friend.services.yml @@ -3,3 +3,4 @@ services: class: Drupal\os2loop_share_with_a_friend\Helper\MailHelper arguments: - '@token' + - '@Drupal\os2loop_settings\Settings' diff --git a/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/src/Form/SettingsForm.php b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/src/Form/SettingsForm.php new file mode 100644 index 000000000..0cdcf24f2 --- /dev/null +++ b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/src/Form/SettingsForm.php @@ -0,0 +1,99 @@ +settings = $settings; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('config.factory'), + $container->get(Settings::class) + ); + } + + /** + * {@inheritdoc} + */ + public function getFormId() { + return 'os2loop_share_with_a_friend_settings'; + } + + /** + * {@inheritdoc} + */ + protected function getEditableConfigNames() { + return [ + static::SETTINGS_NAME, + ]; + } + + /** + * {@inheritdoc} + */ + public function buildForm(array $form, FormStateInterface $form_state) { + $config = $this->settings->getConfig(static::SETTINGS_NAME); + + $form['email_template'] = [ + '#type' => 'textarea', + '#title' => $this->t('Email template for share with a friend body'), + '#default_value' => $config->get('template_body'), + ]; + + $form['subject_template'] = [ + '#type' => 'textarea', + '#title' => $this->t('Subject template for share with a friend subject'), + '#default_value' => $config->get('template_subject'), + ]; + + return parent::buildForm($form, $form_state); + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + $this->configFactory->getEditable(static::SETTINGS_NAME) + ->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_share_with_a_friend/src/Form/ShareWithAFriendForm.php b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/src/Form/ShareWithAFriendForm.php index 2cda8e349..213d8ace4 100644 --- a/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/src/Form/ShareWithAFriendForm.php +++ b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/src/Form/ShareWithAFriendForm.php @@ -152,11 +152,11 @@ public function submitForm(array &$form, FormStateInterface $form_state) { } else { $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(); } - $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_share_with_a_friend/src/Helper/MailHelper.php b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/src/Helper/MailHelper.php index 160094018..4e7d4e344 100644 --- a/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/src/Helper/MailHelper.php +++ b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/src/Helper/MailHelper.php @@ -4,6 +4,8 @@ use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\Core\Utility\Token; +use Drupal\os2loop_share_with_a_friend\Form\SettingsForm; +use Drupal\os2loop_settings\Settings; /** * MailHelper for creating mail templates. @@ -11,20 +13,25 @@ class MailHelper { use StringTranslationTrait; /** - * The toke. + * The token. * * @var \Drupal\Core\Utility\Token */ protected $token; /** - * Constructor. + * The config. * - * @param \Drupal\Core\Utility\Token $token - * The token. + * @var \Drupal\Core\Config\ImmutableConfig + */ + private $config; + + /** + * Constructor. */ - public function __construct(Token $token) { + public function __construct(Token $token, Settings $settings) { $this->token = $token; + $this->config = $settings->getConfig(SettingsForm::SETTINGS_NAME); } /** @@ -35,12 +42,12 @@ public function mail($key, &$message, $params) { case 'share_with_a_friend': $node = $params['node']; // @todo move this to settings - $template = '[current-user:name] shared the following with you: [node:title] ([node:url]) with the following message: [os2loop_share_with_a_friend:message]'; + $body_template = $this->config->get('template_body'); + $subject_template = $this->config->get('template_subject'); $data['node'] = $node; $data['message'] = $params['message']; - $body = $this->renderTemplate($template, $data); - $template = '[current-user:name] wants to share content from [site:name] with you'; - $subject = $this->renderTemplate($template); + $body = $this->renderTemplate($body_template, $data); + $subject = $this->renderTemplate($subject_template); $message['subject'] = $subject; $message['body'][] = $body; break; 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 9b4c79d37..510a29a7b 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 @@ -21,7 +21,7 @@ {% endif %} {% if is_granted('share with a friend', node) %} {% endif %} {% if is_granted('update', node) %} From 176d0477f0ca7e9b2e31b378a0dc1845ca554eba Mon Sep 17 00:00:00 2001 From: Sine Date: Mon, 17 May 2021 19:57:53 +0200 Subject: [PATCH 09/10] LOOP-871: added share with a friend settings to config ignore --- config/sync/config_ignore.settings.yml | 1 + config/sync/os2loop_share_with_a_friend.settings.yml | 2 -- 2 files changed, 1 insertion(+), 2 deletions(-) delete mode 100644 config/sync/os2loop_share_with_a_friend.settings.yml diff --git a/config/sync/config_ignore.settings.yml b/config/sync/config_ignore.settings.yml index ee65f50f9..36c0b4cab 100644 --- a/config/sync/config_ignore.settings.yml +++ b/config/sync/config_ignore.settings.yml @@ -3,6 +3,7 @@ ignored_config_entities: - os2loop_flag_content.settings - os2loop_question.settings - os2loop_settings.settings + - os2loop_share_with_a_friend.settings - system.site _core: default_config_hash: UVH1aJ4b44UM-VdPVN7hNNuuVqfReJxwfVeDQH1Hvsk diff --git a/config/sync/os2loop_share_with_a_friend.settings.yml b/config/sync/os2loop_share_with_a_friend.settings.yml deleted file mode 100644 index 9cb0191a6..000000000 --- a/config/sync/os2loop_share_with_a_friend.settings.yml +++ /dev/null @@ -1,2 +0,0 @@ -template_subject: '[current-user:name] wants to share content from [site:name] with you' -template_body: '[current-user:name] shared the following with you: [node:title] ([node:url]) with the following message: [os2loop_share_with_a_friend:message]' From 171752f8c8e4729a962bc03f7234564ee5e6f18d Mon Sep 17 00:00:00 2001 From: Mikkel Ricky Date: Wed, 19 May 2021 14:10:55 +0200 Subject: [PATCH 10/10] LOOP-871: Cleaned up tokens. Added default templates. --- .../os2loop_share_with_a_friend.settings.yml | 2 + .../src/Form/SettingsForm.php | 21 +++++-- .../src/Form/ShareWithAFriendForm.php | 5 +- .../src/Helper/MailHelper.php | 60 +++++++++---------- 4 files changed, 49 insertions(+), 39 deletions(-) create mode 100644 config/sync/os2loop_share_with_a_friend.settings.yml diff --git a/config/sync/os2loop_share_with_a_friend.settings.yml b/config/sync/os2loop_share_with_a_friend.settings.yml new file mode 100644 index 000000000..9bd13d864 --- /dev/null +++ b/config/sync/os2loop_share_with_a_friend.settings.yml @@ -0,0 +1,2 @@ +template_subject: '[current-user:mail] wants to share “[node:title]” on [site:name] with you' +template_body: "[current-user:mail] wants to share [node:title] on [site:name] with you:\r\n\r\n[os2loop_share_with_a_friend:message]\r\n\r\nBest regards,\r\n[site:name]" diff --git a/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/src/Form/SettingsForm.php b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/src/Form/SettingsForm.php index 0cdcf24f2..f616ad8d1 100644 --- a/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/src/Form/SettingsForm.php +++ b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/src/Form/SettingsForm.php @@ -69,16 +69,29 @@ protected function getEditableConfigNames() { public function buildForm(array $form, FormStateInterface $form_state) { $config = $this->settings->getConfig(static::SETTINGS_NAME); + $form['subject_template'] = [ + '#type' => 'textfield', + '#title' => $this->t('Subject template for share with a friend subject'), + '#required' => TRUE, + '#default_value' => $config->get('template_subject'), + ]; + + $form['subject_template_tokens'] = [ + '#theme' => 'token_tree_link', + '#token_types' => ['user', 'node', 'os2loop_share_with_a_friend'], + ]; + $form['email_template'] = [ '#type' => 'textarea', '#title' => $this->t('Email template for share with a friend body'), + '#required' => TRUE, '#default_value' => $config->get('template_body'), + '#token_insert' => TRUE, ]; - $form['subject_template'] = [ - '#type' => 'textarea', - '#title' => $this->t('Subject template for share with a friend subject'), - '#default_value' => $config->get('template_subject'), + $form['email_template_tokens'] = [ + '#theme' => 'token_tree_link', + '#token_types' => ['user', 'node', 'os2loop_share_with_a_friend'], ]; return parent::buildForm($form, $form_state); diff --git a/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/src/Form/ShareWithAFriendForm.php b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/src/Form/ShareWithAFriendForm.php index 213d8ace4..628462de4 100644 --- a/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/src/Form/ShareWithAFriendForm.php +++ b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/src/Form/ShareWithAFriendForm.php @@ -89,6 +89,7 @@ public function getFormId() { * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { + /** @var \Drupal\node\NodeInterface $node */ $node = $this->routeMatcher->getParameter('node'); $form['title'] = [ @@ -109,6 +110,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#title' => $this->t('Email address of recipient'), ]; + $form['actions']['#type'] = 'actions'; $form['actions']['submit'] = [ '#type' => 'submit', '#value' => $this->t('Send'), @@ -120,7 +122,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' => [ @@ -128,7 +130,6 @@ public function buildForm(array $form, FormStateInterface $form_state) { ], ], ]; - $form['actions']['#type'] = 'actions'; return $form; } diff --git a/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/src/Helper/MailHelper.php b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/src/Helper/MailHelper.php index 4e7d4e344..fb110e856 100644 --- a/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/src/Helper/MailHelper.php +++ b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/src/Helper/MailHelper.php @@ -40,16 +40,16 @@ public function __construct(Token $token, Settings $settings) { public function mail($key, &$message, $params) { switch ($key) { case 'share_with_a_friend': - $node = $params['node']; - // @todo move this to settings $body_template = $this->config->get('template_body'); $subject_template = $this->config->get('template_subject'); - $data['node'] = $node; - $data['message'] = $params['message']; - $body = $this->renderTemplate($body_template, $data); - $subject = $this->renderTemplate($subject_template); - $message['subject'] = $subject; - $message['body'][] = $body; + $data = [ + 'node' => $params['node'], + 'os2loop_share_with_a_friend' => [ + 'message' => $params['message'], + ], + ]; + $message['subject'] = $this->renderTemplate($subject_template, $data); + $message['body'][] = $this->renderTemplate($body_template, $data); break; } } @@ -57,16 +57,8 @@ public function mail($key, &$message, $params) { /** * Renders content of a mail. */ - public function renderTemplate($template, array $data = NULL) { - if (isset($data)) { - return $this->token->replace($template, [ - 'node' => $data['node'], - 'message' => $data['message'], - ], []); - } - else { - return $this->token->replace($template, [], []); - } + public function renderTemplate($template, array $data) { + return $this->token->replace($template, $data, []); } /** @@ -74,15 +66,14 @@ public function renderTemplate($template, array $data = NULL) { */ public function tokens($type, $tokens, array $data) { $replacements = []; - if ($type == 'os2loop_share_with_a_friend' && !empty($data['message'])) { + if ('os2loop_share_with_a_friend' === $type && isset($data[$type])) { foreach ($tokens as $name => $original) { - switch ($name) { - case 'message': - $replacements[$original] = $data['message']; - break; + if (isset($data[$type][$name])) { + $replacements[$original] = $data[$type][$name]; } } } + return $replacements; } @@ -90,20 +81,23 @@ public function tokens($type, $tokens, array $data) { * Implements hook_token_info(). */ public function tokenInfo() { - $types['os2loop_share_with_a_friend'] = [ - 'name' => $this->t('Message type'), - ]; - $tokens['message'] = [ - 'name' => $this->t('Message'), - ]; - return [ - 'types' => $types, + 'types' => [ + 'os2loop_share_with_a_friend' => [ + 'name' => $this->t('Share with a friend'), + 'description' => $this->t('Tokens related to share with a friend.'), + 'needs-data' => 'os2loop_share_with_a_friend', + ], + ], 'tokens' => [ - 'os2loop_share_with_a_friend' => $tokens, + 'os2loop_share_with_a_friend' => [ + 'message' => [ + 'name' => $this->t('The message'), + 'description' => $this->t('The message.'), + ], + ], ], ]; - } }