Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)```
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
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_flag_content_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
return Drupal::service('os2loop_flag_content.mail_helper')->tokens($type, $tokens, $data);
}

/**
* Implements hook_token_info().
*/
function os2loop_flag_content_token_info() {
return Drupal::service('os2loop_flag_content.mail_helper')->tokenInfo();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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'),
Expand All @@ -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' => [
Expand All @@ -150,11 +152,6 @@ public function buildForm(array $form, FormStateInterface $form_state) {
return $form;
}

/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {}

/**
* {@inheritdoc}
*/
Expand All @@ -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();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -29,15 +29,15 @@ public function getFormId() {
*/
protected function getEditableConfigNames() {
return [
static::SETTINGS,
static::SETTINGS_NAME,
];
}

/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->config(static::SETTINGS);
$config = $this->config(static::SETTINGS_NAME);

$form['reasons'] = [
'#type' => 'textarea',
Expand All @@ -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);
}

Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.'),
],
],
],
];
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
{% endif %}
{% if is_granted('contact editorial office', node) %}
<div class="icon flag" title="{{ 'contact editorial office'|t }}">
<a aria-label="{{ 'Contact editorial staff'|t }}" href="{{ path('os2loop_flag_content.flag_content_form', {node: node.id}) }}"></a>
<a aria-label="{{ 'Contact editorial staff'|t }}" href="{{ path('os2loop_flag_content.flag_content', {node: node.id}) }}"></a>
</div>
{% endif %}
{% if is_granted('share with a friend', node) %}
Expand Down