View source
<?php
namespace Drupal\message_ui\Form;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Language\Language;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MessageForm extends ContentEntityForm {
protected $entityTypeManager;
protected $account;
protected $languageManager;
public static function create(ContainerInterface $container) {
$instance = parent::create($container);
$instance->languageManager = $container
->get('language_manager');
$instance->entityTypeManager = $container
->get('entity_type.manager');
$instance->account = $container
->get('current_user');
return $instance;
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
$message = $this->entity;
$template = $this->entityTypeManager
->getStorage('message_template')
->load($this->entity
->bundle());
if ($this
->config('message_ui.settings')
->get('show_preview')) {
$form['text'] = [
'#type' => 'item',
'#title' => t('Message template'),
'#markup' => implode("\n", $template
->getText()),
];
}
$form['advanced'] = [
'#type' => 'details',
'#attributes' => [
'class' => [
'entity-meta',
],
],
'#weight' => 99,
];
$form['owner'] = [
'#type' => 'fieldset',
'#title' => t('Owner information'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#group' => 'advanced',
'#weight' => 90,
'#attributes' => [
'class' => [
'message-form-owner',
],
],
'#attached' => [
'library' => [
'message_ui/message_ui.message',
],
'drupalSettings' => [
'message_ui' => [
'anonymous' => \Drupal::config('message_ui.settings')
->get('anonymous'),
],
],
],
];
if (isset($form['uid'])) {
$form['uid']['#group'] = 'owner';
}
if (isset($form['created'])) {
$form['created']['#group'] = 'owner';
}
$tokens = $message
->getArguments();
$access = $this->account
->hasPermission('update tokens') || $this->account
->hasPermission('bypass message access control');
if (!empty($tokens) && $access) {
$form['tokens'] = [
'#type' => 'fieldset',
'#title' => t('Tokens and arguments'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#group' => 'advanced',
'#weight' => 110,
];
$form['tokens']['replace_tokens'] = [
'#type' => 'select',
'#title' => t('Update tokens value automatically'),
'#description' => t('By default, the hard coded values will be replaced automatically. If unchecked - you can update their value manually.'),
'#default_value' => 'no_update',
'#options' => [
'no_update' => t("Don't update"),
'update' => t('Update automatically'),
'update_manually' => t('Update manually'),
],
];
$form['tokens']['values'] = [
'#type' => 'container',
'#states' => [
'visible' => [
':input[name="replace_tokens"]' => [
'value' => 'update_manually',
],
],
],
];
foreach ($message
->getArguments() as $name => $value) {
$form['tokens']['values'][$name] = [
'#type' => 'textfield',
'#title' => t("@name's value", [
'@name' => $name,
]),
'#default_value' => $value,
];
}
}
$form['langcode'] = [
'#title' => $this
->t('Language'),
'#type' => 'language_select',
'#default_value' => $message
->getUntranslated()
->language()
->getId(),
'#languages' => Language::STATE_ALL,
'#access' => $this->languageManager
->isMultilingual(),
];
return $form;
}
protected function actions(array $form, FormStateInterface $form_state) {
$element = parent::actions($form, $form_state);
$message = $this->entity;
$element['save'] = $element['submit'];
if ($message
->isNew()) {
$element['save']['#value'] = t('Create');
}
else {
$element['save']['#value'] = t('Update');
}
$element['save']['#weight'] = 0;
$mid = $message
->id();
$url = is_object($message) && !empty($mid) ? Url::fromRoute('entity.message.canonical', [
'message' => $mid,
]) : Url::fromRoute('message.overview_templates');
$link = Link::fromTextAndUrl(t('Cancel'), $url)
->toString();
$element['cancel'] = [
'#type' => 'markup',
'#markup' => $link,
];
$element['submit']['#access'] = FALSE;
return $element;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$message = $this->entity;
$uid = $form_state
->getValue('uid');
if (is_array($uid) && !empty($uid[0]['target_id'])) {
$message
->setOwnerId($uid[0]['target_id']);
}
$replace_tokens = $form_state
->getValue('replace_tokens');
$token_actions = empty($replace_tokens) ? [] : $replace_tokens;
if ($args = $message
->getArguments()) {
if (!empty($token_actions) && $token_actions != 'no_update') {
foreach (array_keys($args) as $token) {
if ($token_actions == 'update') {
$token_name = str_replace([
'@{',
'}',
], [
'[',
']',
], $token);
$token_service = \Drupal::token();
$value = $token_service
->replace($token_name, [
'message' => $message,
]);
}
else {
$value = $form_state
->getValue($token);
}
$args[$token] = $value;
}
}
}
$this->entity
->setArguments($args);
}
public function save(array $form, FormStateInterface $form_state) {
$message = $this->entity;
$insert = $message
->isNew();
$message
->save();
$message_link = $message
->toLink($this
->t('View'))
->toString();
$context = [
'@type' => $message
->getTemplate()
->id(),
'%title' => 'Message:' . $message
->id(),
'link' => $message_link,
];
$t_args = [
'@type' => $message
->getEntityType()
->getLabel(),
'%title' => 'Message:' . $message
->id(),
];
if ($insert) {
$this
->logger('content')
->notice('@type: added %title.', $context);
$this
->messenger()
->addMessage(t('@type %title has been created.', $t_args));
}
else {
$this
->logger('content')
->notice('@type: updated %title.', $context);
$this
->messenger()
->addMessage(t('@type %title has been updated.', $t_args));
}
if ($message
->id()) {
$form_state
->setValue('mid', $message
->id());
$form_state
->set('mid', $message
->id());
if ($message
->access('view')) {
$form_state
->setRedirect('entity.message.canonical', [
'message' => $message
->id(),
]);
}
else {
$form_state
->setRedirect('<front>');
}
}
else {
$this
->messenger()
->addMessage(t('The message could not be saved.'), 'error');
$form_state
->setRebuild();
}
}
}