Skip to content
Merged
1 change: 1 addition & 0 deletions config/sync/config_ignore.settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions config/sync/core.extension.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions config/sync/os2loop_share_with_a_friend.settings.yml
Original file line number Diff line number Diff line change
@@ -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 <a href=\"/node/[node:nid]\">[node:title]</a> on [site:name] with you:\r\n\r\n[os2loop_share_with_a_friend:message]\r\n\r\nBest regards,\r\n[site:name]"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# OS2Loop share with a friend

This module makes users able to share content with friends pr email.
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

/**
* @file
* The module file for os2loop_share_with_a_friend.
*/

/**
* Implements hook_mail().
*/
function os2loop_share_with_a_friend_mail($key, &$message, $params) {
Drupal::service('os2loop_share_with_a_friend.mail_helper')->mail($key, $message, $params);
}
Original file line number Diff line number Diff line change
@@ -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'
Original file line number Diff line number Diff line change
@@ -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'
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/**
* @file
* Token integration for the views module.
*/

use Drupal\Core\Render\BubbleableMetadata;

/**
* Implements hook_tokens().
*/
function os2loop_share_with_a_friend_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
return Drupal::service('os2loop_share_with_a_friend.mail_helper')->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();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

namespace Drupal\os2loop_share_with_a_friend\Form;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\os2loop_settings\Settings;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Configure share with a friend admin settings for this site.
*/
class SettingsForm extends ConfigFormBase {
use StringTranslationTrait;

/**
* Config setting name.
*
* @var string
*/
public const SETTINGS_NAME = 'os2loop_share_with_a_friend.settings';

/**
* The settings.
*
* @var \Drupal\os2loop_settings\Settings
*/
private $settings;

/**
* Constructor.
*/
public function __construct(ConfigFactoryInterface $config_factory, Settings $settings) {
parent::__construct($config_factory);
$this->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);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
<?php

namespace Drupal\os2loop_share_with_a_friend\Form;

use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Mail\MailManagerInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Url;
use Symfony\Component\HttpFoundation\RedirectResponse;

/**
* Share with a friend form.
*
* @package Drupal\os2loop_share_with_a_friend\Form
*/
class ShareWithAFriendForm extends FormBase implements ContainerInjectionInterface {
/**
* The route mathcer.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
protected $routeMatcher;

/**
* The mail manager.
*
* @var \Drupal\Core\Mail\MailManagerInterface
*/
protected $mailManager;

/**
* The mail manager.
*
* @var \Drupal\Core\Session\AccountProxyInterface
*/
protected $currentUser;

/**
* The messenger.
*
* @var \Drupal\Core\Messenger\MessengerInterface
*/
protected $messenger;

/**
* Constructs a share with a friend form.
*
* @param \Drupal\Core\Routing\RouteMatchInterface $routeMatcher
* The route matcher.
* @param \Drupal\Core\Mail\MailManagerInterface $mailManager
* The mail manager.
* @param \Drupal\Core\Session\AccountProxyInterface $currentUser
* The current user.
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* The messenger.
*/
public function __construct(RouteMatchInterface $routeMatcher, MailManagerInterface $mailManager, AccountProxyInterface $currentUser, MessengerInterface $messenger) {
$this->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();
}

}

}
Loading