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/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 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/README.md b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/README.md new file mode 100644 index 000000000..fc8f0b8c1 --- /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 content 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..6ccded151 --- /dev/null +++ b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/os2loop_share_with_a_friend.info.yml @@ -0,0 +1,7 @@ +name: 'OS2Loop share with a friend' +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.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..e3bbeb603 --- /dev/null +++ b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/os2loop_share_with_a_friend.routing.yml @@ -0,0 +1,18 @@ +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: + parameters: + node: + 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 new file mode 100644 index 000000000..a9c78527f --- /dev/null +++ b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/os2loop_share_with_a_friend.services.yml @@ -0,0 +1,6 @@ +services: + os2loop_share_with_a_friend.mail_helper: + 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/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/Form/SettingsForm.php b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/src/Form/SettingsForm.php new file mode 100644 index 000000000..f616ad8d1 --- /dev/null +++ b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/src/Form/SettingsForm.php @@ -0,0 +1,112 @@ +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['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['email_template_tokens'] = [ + '#theme' => 'token_tree_link', + '#token_types' => ['user', 'node', 'os2loop_share_with_a_friend'], + ]; + + 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 new file mode 100644 index 000000000..628462de4 --- /dev/null +++ b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/src/Form/ShareWithAFriendForm.php @@ -0,0 +1,163 @@ +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) { + /** @var \Drupal\node\NodeInterface $node */ + $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['to_email'] = [ + '#type' => 'email', + '#required' => TRUE, + '#title' => $this->t('Email address of recipient'), + ]; + + $form['actions']['#type'] = 'actions'; + $form['actions']['submit'] = [ + '#type' => 'submit', + '#value' => $this->t('Send'), + '#attributes' => [ + 'class' => [ + 'button', + ], + ], + ]; + $form['actions']['cancel'] = [ + '#type' => 'link', + '#url' => $node->toUrl(), + '#title' => $this->t('Cancel'), + '#attributes' => [ + 'class' => [ + 'button', + ], + ], + ]; + + return $form; + } + + /** + * {@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; + $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->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.')); + $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 new file mode 100644 index 000000000..fb110e856 --- /dev/null +++ b/web/profiles/custom/os2loop/modules/os2loop_share_with_a_friend/src/Helper/MailHelper.php @@ -0,0 +1,103 @@ +token = $token; + $this->config = $settings->getConfig(SettingsForm::SETTINGS_NAME); + } + + /** + * Implements hook_mail(). + */ + public function mail($key, &$message, $params) { + switch ($key) { + case 'share_with_a_friend': + $body_template = $this->config->get('template_body'); + $subject_template = $this->config->get('template_subject'); + $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; + } + } + + /** + * 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_share_with_a_friend' === $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_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' => [ + 'message' => [ + 'name' => $this->t('The message'), + 'description' => $this->t('The message.'), + ], + ], + ], + ]; + } + +} 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..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) %}