View source
<?php
namespace Drupal\message_notify_ui\Form;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\message_notify\MessageNotifier;
use Drupal\message_notify_ui\MessageNotifyUiSenderSettingsFormManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\message_notify\Plugin\Notifier\Manager;
class MessageNotifyForm extends EntityForm {
protected $entityTypeManager;
protected $entity;
protected $entityTypeBundleInfo;
protected $time;
protected $messageNotifier;
protected $messageNotifierManager;
protected $messageNotifyUiSenderSettingsForm;
protected $languageManager;
protected $plugins;
public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info, TimeInterface $time, MessageNotifier $message_notifier, Manager $message_notify_manager, MessageNotifyUiSenderSettingsFormManager $message_notify_ui_setting_form_manager, LanguageManagerInterface $language_manager) {
$this->entityTypeManager = $entity_type_manager;
$this->entityTypeBundleInfo = $entity_type_bundle_info ?: \Drupal::service('entity_type.bundle.info');
$this->time = $time ?: \Drupal::service('datetime.time');
$this->messageNotifier = $message_notifier;
$this->messageNotifierManager = $message_notify_manager;
$this->messageNotifyUiSenderSettingsForm = $message_notify_ui_setting_form_manager;
$this->languageManager = $language_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('entity_type.bundle.info'), $container
->get('datetime.time'), $container
->get('message_notify.sender'), $container
->get('plugin.message_notify.notifier.manager'), $container
->get('plugin.manager.message_notify_ui_sender_settings_form'), $container
->get('language_manager'));
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['actions'] = parent::buildForm($form, $form_state)['actions'];
$senders = [];
foreach ($this->messageNotifierManager
->getDefinitions() as $definition) {
$senders[$definition['id']] = $definition['title'];
}
$senders_ids = array_keys($senders);
$form['senders'] = [
'#type' => 'radios',
'#title' => $this
->t('Select a notifier handler'),
'#options' => $senders,
'#default_value' => reset($senders_ids),
];
$form['senders_form'] = [];
if ($this->languageManager
->isMultilingual()) {
$languages = [];
foreach ($this->languageManager
->getLanguages() as $language) {
$languages[$language
->getId()] = $language
->getName();
}
$form['language'] = [
'#type' => 'select',
'#title' => $this
->t('Select a language'),
'#options' => $languages,
];
}
foreach ($this->messageNotifyUiSenderSettingsForm
->getDefinitions() as $definition) {
$plugin = $this->messageNotifyUiSenderSettingsForm
->createInstance($definition['id']);
$this->plugins[$definition['notify_plugin']] = $definition['id'];
$form['senders_form'][$definition['id']] = $plugin
->form();
}
$form['actions'] = [
'#type' => 'actions',
'submit' => [
'#type' => 'submit',
'#value' => $this
->t('Notify'),
'#submit' => [
'::submitForm',
'::save',
],
'#button_type' => 'primary',
],
];
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$this->messageNotifyUiSenderSettingsForm
->createInstance($this->plugins[$form_state
->getValue('senders')])
->setMessage($this->entity)
->validate($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$plugin = $form_state
->getValue('senders');
$this->messageNotifyUiSenderSettingsForm
->createInstance($this->plugins[$plugin])
->setMessage($this->entity)
->submit($this->messageNotifier, $form_state);
}
}