class MailEditTemplateForm in Mail Editor 8
Edit an email template.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\mail_edit\Form\MailEditTemplateForm
Expanded class hierarchy of MailEditTemplateForm
1 string reference to 'MailEditTemplateForm'
File
- src/
Form/ MailEditTemplateForm.php, line 13
Namespace
Drupal\mail_edit\FormView source
class MailEditTemplateForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'mail_edit_template_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, $id = NULL, $lang = NULL) {
// Load the template for this object.
$template = $this
->getTemplate($id);
$form['id'] = [
'#type' => 'value',
'#value' => $id,
];
$form['description'] = [
'#markup' => isset($template['description']) ? Xss::filter($template['description']) : '',
'#access' => isset($template['description']),
];
$form['config'] = [
'#title' => $this
->t('Config'),
'#type' => 'textfield',
'#default_value' => $template['config'],
'#disabled' => TRUE,
];
$form['email'] = [
'#title' => $this
->t('Email'),
'#type' => 'textfield',
'#default_value' => $template['name'],
'#disabled' => TRUE,
];
$form['message']['subject'] = [
'#title' => $this
->t('Subject'),
'#type' => 'textfield',
'#default_value' => $template['subject'],
'#maxlength' => 180,
'#required' => TRUE,
];
$form['message']['body'] = [
'#title' => $this
->t('Email body'),
'#type' => 'textarea',
'#default_value' => $template['body'],
'#required' => TRUE,
];
// If the Token module is installed, show a link to the token tree.
$module_handler = \Drupal::moduleHandler();
if ($module_handler
->moduleExists('token')) {
$module_name = $this
->getModuleName($id);
// Trigger hook_mail_edit_token_types().
// The 'user' entity will always be available.
$tokens = [
'user',
] + (array) $module_handler
->invoke($module_name, 'mail_edit_token_types', [
$template['name'],
]);
// Show a link to the token browser.
$form['message']['token_tree'] = [
'#theme' => 'token_tree_link',
'#token_types' => $tokens,
'#show_restricted' => TRUE,
'#show_nested' => FALSE,
];
}
// @todo WYSIWYG support.
// @todo Plaintext support.
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Save'),
'#weight' => 10,
];
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
// @todo
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Work out the template ID.
$id = $form_state
->getValue('id');
// Get the config object for this template.
$configFactory = $this
->configFactory()
->getEditable($this
->getConfigName($id));
$name = $this
->getEmailName($id);
// Update the config object.
$configFactory
->set($name . '.subject', $form_state
->getValue('subject'));
$configFactory
->set($name . '.body', $form_state
->getValue('body'));
$configFactory
->save();
$this
->messenger()
->addMessage($this
->t('Email "%mesg" has been updated.', [
'%mesg' => $name,
]));
$form_state
->setRedirect('mail_edit.list');
}
/**
* Extract the module's name from an email ID.
*
* @param string $id
* A string in the format 'MODULENAME.CONFIGNAME.TEMPLATENAME'.
*
* @return string
* The name of the module which specifies the email.
*/
private function getModuleName($id) {
$parts = explode('.', $id);
return $parts[0];
}
/**
* Extract the email's config object name from an email ID.
*
* @param string $id
* A string in the format 'MODULENAME.CONFIGNAME.TEMPLATENAME'.
*
* @return string
* The name of the config object, which will be the first two portions of
* the ID when split on the period character.
*/
private function getConfigName($id) {
$parts = explode('.', $id);
return $parts[0] . '.' . $parts[1];
}
/**
* Extract the email's config object name from an email ID.
*
* @param string $id
* A string in the format 'MODULENAME.CONFIGNAME.TEMPLATENAME'.
*
* @return \Drupal\Core\Config\Config
* A full config object.
*/
private function getConfig($id) {
return $this
->config($this
->getConfigName($id));
}
/**
* Extract the name of the email item from an email ID.
*
* @param string $id
* A string in the format 'MODULENAME.CONFIGNAME.TEMPLATENAME'.
*
* @return string
* The email template name.
*/
private function getEmailName($id) {
$parts = explode('.', $id);
unset($parts[0]);
unset($parts[1]);
return implode('.', $parts);
}
/**
* Load an email template from a combination string.
*
* @param string $id
* A combination of the config entity's machine name and the email's name.
*
* @return array
* Will contain the following elements:
* - subject - The email's subject line.
* - body - The email's body text.
* - config - The name of the config object this was found in.
* - name - The name of email template.
*/
private function getTemplate($id) {
// Load the config entity.
/* @var $config \Drupal\Core\Config\Config */
$config = $this
->getConfig($id);
// The email structure's name.
$template_name = $this
->getEmailName($id);
// If the config object was found, generate it.
if (!empty($config)) {
// Extract the specific config object that was requested.
$template = $config
->get($template_name);
}
// If the config object didn't exist or the template wasn't defined, try
// checking to make sure it was defined via the hook. This will allow email
// config objects to be dynamically generated but block someone from being
// able to create random config objects.
if (empty($config) || !isset($template)) {
$module_handler = \Drupal::moduleHandler();
$module_name = $this
->getModuleName($id);
$config_name = $this
->getConfigName($id);
// Trigger hook_mail_edit_templates().
$data = $module_handler
->invoke($module_name, 'mail_edit_templates');
if (!isset($data, $data[$config_name], $data[$config_name][$template_name])) {
throw new NotFoundHttpException();
}
}
// If the template wasn't loaded, or doesn't exist, create an empty one so
// that it can be saved.
if (empty($template) || !is_array($template) || !isset($template['subject']) || !isset($template['body'])) {
$template = [
'subject' => '',
'body' => '',
];
}
$template['config'] = $config
->getName();
$template['name'] = $template_name;
return $template;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
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 | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
87 |
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. | |
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. | |
MailEditTemplateForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
MailEditTemplateForm:: |
private | function | Extract the email's config object name from an email ID. | |
MailEditTemplateForm:: |
private | function | Extract the email's config object name from an email ID. | |
MailEditTemplateForm:: |
private | function | Extract the name of the email item from an email ID. | |
MailEditTemplateForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
MailEditTemplateForm:: |
private | function | Extract the module's name from an email ID. | |
MailEditTemplateForm:: |
private | function | Load an email template from a combination string. | |
MailEditTemplateForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
MailEditTemplateForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
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. | |
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. |