class Settings in Courier 8
Same name in this branch
- 8 src/Form/Settings.php \Drupal\courier\Form\Settings
- 8 courier_system/src/Form/Settings.php \Drupal\courier_system\Form\Settings
Same name and namespace in other branches
- 2.x courier_system/src/Form/Settings.php \Drupal\courier_system\Form\Settings
Configure Courier System settings.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
- class \Drupal\courier_system\Form\Settings
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
Expanded class hierarchy of Settings
2 string references to 'Settings'
- courier.links.task.yml in ./
courier.links.task.yml - courier.links.task.yml
- courier_system.routing.yml in courier_system/
courier_system.routing.yml - courier_system/courier_system.routing.yml
File
- courier_system/
src/ Form/ Settings.php, line 19
Namespace
Drupal\courier_system\FormView source
class Settings extends ConfigFormBase {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityManagerInterface
*/
protected $entityManager;
/**
* The courier manager.
*
* @var \Drupal\courier\Service\CourierManagerInterface
*/
protected $courierManager;
/**
* Constructs a configuration form.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The factory for configuration objects.
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager.
* @param \Drupal\courier\Service\CourierManagerInterface $courier_manager
* The courier manager.
*/
public function __construct(ConfigFactoryInterface $config_factory, EntityManagerInterface $entity_manager, CourierManagerInterface $courier_manager) {
parent::__construct($config_factory);
$this->entityManager = $entity_manager;
$this->courierManager = $courier_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('entity.manager'), $container
->get('courier.manager'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'courier_system_settings';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'courier_system.settings',
];
}
/**
* Get mail supported by courier_system.
*
* @return array
* Mail IDs keyed by module name.
*/
protected function getSystemMails() {
$options['user'] = [
'user_cancel_confirm' => [
'title' => $this
->t('Account cancellation confirmation'),
'description' => $this
->t('Sent to users when they attempt to cancel their accounts.'),
],
'user_password_reset' => [
'title' => $this
->t('Notify user when password reset'),
'description' => $this
->t('Sent to users who request a new password.'),
],
'user_status_activated' => [
'title' => $this
->t('Notify user when account is activated'),
'description' => $this
->t('Sent to users upon account activation (when an administrator activates an account of a user who has already registered, on a site where administrative approval is required)'),
],
'user_status_blocked' => [
'title' => $this
->t('Account blocked'),
'description' => $this
->t('Sent to users when their accounts are blocked.'),
],
'user_status_canceled' => [
'title' => $this
->t('Account canceled'),
'description' => $this
->t('Sent to users when they attempt to cancel their accounts.'),
],
'user_register_admin_created' => [
'title' => $this
->t('Welcome (new user created by administrator)'),
'description' => $this
->t('Sent to new member accounts created by an administrator.'),
],
'user_register_no_approval_required' => [
'title' => $this
->t('Welcome (no approval required)'),
'description' => $this
->t('Sent to new members upon registering, when no administrator approval is required.'),
],
'user_register_pending_approval' => [
'title' => $this
->t('Welcome (awaiting approval)'),
'description' => $this
->t('Sent to new members upon registering, when administrative approval is required.'),
],
];
return $options;
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
$config = $this
->config('courier_system.settings');
$override = $config
->get('override');
// Actions.
$form['actions'] = [
'#type' => 'details',
'#attributes' => [
'class' => [
'container-inline',
],
],
'#open' => TRUE,
];
$form['actions']['operation'] = [
'#title' => $this
->t('With selection'),
'#type' => 'select',
'#options' => [
'copy_email' => $this
->t('Copy Drupal email to Courier'),
'enable' => $this
->t('Override Drupal email (enable selected)'),
'disable' => $this
->t('Restore Drupal email (disable selected)'),
'delete' => $this
->t('Delete'),
],
'#empty_option' => $this
->t(' - Select - '),
'#button_type' => 'primary',
];
$form['actions']['apply'] = [
'#type' => 'submit',
'#value' => $this
->t('Apply'),
'#button_type' => 'primary',
];
// List items.
$form['list'] = [
'#type' => 'courier_template_collection_list',
'#title' => $this
->t('Replace Drupal mails'),
'#checkboxes' => TRUE,
'#items' => [],
];
$header = [
$this
->t('Module'),
$this
->t('Description'),
];
$form['add_missing'] = [
'#type' => 'details',
'#title' => $this
->t('Add missing messages'),
];
$form['add_missing']['table'] = [
'#type' => 'table',
'#header' => $header,
'#tree' => TRUE,
'#tableselect' => TRUE,
'#multiple' => TRUE,
'#empty' => $this
->t('No messages are missing.'),
];
$form['add_missing']['submit'] = [
'#type' => 'submit',
'#value' => t('Create messages'),
'#submit' => [
[
$this,
'submitCreateMessages',
],
],
];
foreach ($this
->getSystemMails() as $module => $mails) {
foreach ($mails as $mail_id => $definition) {
$gtc_id = 'courier_system.' . $mail_id;
if ($gtc = GlobalTemplateCollection::load($gtc_id)) {
$template_collection = $gtc
->getTemplateCollection();
$form['list']['#items'][$mail_id] = [
'#title' => $this
->t('@module: @title (@status)', [
'@title' => $definition['title'],
'@module' => \Drupal::moduleHandler()
->getName($module),
'@status' => !empty($override[$mail_id]) ? $this
->t('enabled - using Courier') : $this
->t('disabled - using Drupal'),
]),
'#description' => $definition['description'],
'#template_collection' => $template_collection,
];
}
else {
$row = [];
$row['module']['#markup'] = \Drupal::moduleHandler()
->getName($module);
$row['title']['#markup'] = $definition['title'];
$form['add_missing']['table'][$mail_id] = $row;
}
}
}
$form['add_missing']['#open'] = !count($form['list']['#items']);
if ($count = count(Element::children($form['add_missing']['table']))) {
$form['add_missing']['#title'] = $this
->t('Add missing messages (@count)', [
'@count' => $count,
]);
}
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$message = $this
->t('No operations were executed.');
$config = $this
->config('courier_system.settings');
// Template collections keyed by mail ID.
/** @var \Drupal\courier\Entity\GlobalTemplateCollectionInterface[] $global_template_collections */
$global_template_collections = [];
foreach ($this
->getSystemMails() as $module => $mails) {
foreach ($mails as $mail_id => $definition) {
$gtc = GlobalTemplateCollection::load('courier_system.' . $mail_id);
if ($gtc) {
$global_template_collections[$mail_id] = $gtc;
}
}
}
// List of checked mail IDs.
$checkboxes = [];
foreach ($form_state
->getValue([
'list',
'checkboxes',
]) as $id => $checked) {
if ($checked) {
$checkboxes[] = $id;
}
}
$operation = $form_state
->getValue('operation');
$override = $config
->get('override');
foreach ($checkboxes as $mail_id) {
if (isset($global_template_collections[$mail_id])) {
$gtc = $global_template_collections[$mail_id];
$template_collection = $gtc
->getTemplateCollection();
if (in_array($operation, [
'enable',
'disable',
])) {
$enable = $operation == 'enable';
$override[$mail_id] = $enable;
$message = $enable ? $this
->t('Messages enabled.') : $this
->t('Messages disabled.');
}
elseif ($operation == 'delete') {
$message = $this
->t('Messages deleted');
$gtc
->delete();
$template_collection
->delete();
unset($override[$mail_id]);
}
elseif ($operation == 'copy_email') {
$this
->copyCoreToCourierEmail($template_collection, $mail_id);
$template_collection
->save();
$message = $this
->t('Messages copied from Drupal to Courier.');
}
}
}
$config
->set('override', $override)
->save();
drupal_set_message($message);
}
/**
* Submit handler for create messages table.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
public function submitCreateMessages(array &$form, FormStateInterface $form_state) {
$config = $this
->config('courier_system.settings');
$override = $config
->get('override');
// Create template collections.
foreach ($form_state
->getValue('table') as $mail_id => $create) {
if ($create) {
$values = [
'id' => 'courier_system.' . $mail_id,
];
$gtc = GlobalTemplateCollection::create($values);
$gtc
->save();
$template_collection = $gtc
->getTemplateCollection();
// Owner.
// @todo set owner when DER can reference configs.
// See issue: https://www.drupal.org/node/2555027
// Context.
// Create global context for accounts if it does not exist.
/** @var \Drupal\courier\CourierContextInterface $courier_context */
if (!($courier_context = CourierContext::load('courier_system_user'))) {
$courier_context = CourierContext::create([
'label' => t('Courier System: Account'),
'id' => 'courier_system_user',
'tokens' => [
'user',
],
]);
$courier_context
->save();
}
$template_collection
->setContext($courier_context);
// Contents.
$this
->copyCoreToCourierEmail($template_collection, $mail_id);
$template_collection
->save();
$override[$mail_id] = TRUE;
}
}
$config
->set('override', $override)
->save();
}
/**
* Copy email contents from Drupal to Courier email templates.
*
* Template collection and email template must be created prior to calling.
*
* @param \Drupal\courier\TemplateCollectionInterface $template_collection
* A template collection entity.
* @param string $mail_id
* A mail ID as defined in $this->getSystemMails().
*/
protected function copyCoreToCourierEmail(TemplateCollectionInterface &$template_collection, $mail_id) {
// Only user is supported at this time.
$key = substr($mail_id, strlen('user_'));
$user_mails = $this
->config('user.mail');
$mail = $user_mails
->get($key);
/** @var \Drupal\courier\Entity\Email $courier_email */
if ($courier_email = $template_collection
->getTemplate('courier_email')) {
foreach ($mail as &$value) {
$value = nl2br($value);
$value = str_replace('[user:name]', '[identity:label]', $value);
}
$courier_email
->setSubject($mail['subject'])
->setBody($mail['body'])
->save();
$template_collection
->setTemplate($courier_email);
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfigFormBaseTrait:: |
protected | function | Retrieves a configuration object. | |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
62 |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
Settings:: |
protected | property | The courier manager. | |
Settings:: |
protected | property | The entity type manager. | |
Settings:: |
public | function |
Form constructor. Overrides ConfigFormBase:: |
|
Settings:: |
protected | function | Copy email contents from Drupal to Courier email templates. | |
Settings:: |
public static | function |
Instantiates a new instance of this class. Overrides ConfigFormBase:: |
|
Settings:: |
protected | function |
Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait:: |
|
Settings:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
Settings:: |
protected | function | Get mail supported by courier_system. | |
Settings:: |
public | function | Submit handler for create messages table. | |
Settings:: |
public | function |
Form submission handler. Overrides ConfigFormBase:: |
|
Settings:: |
public | function |
Constructs a configuration form. Overrides ConfigFormBase:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |