MessageNotifyUiSenderMailSettingsForm.php in Message UI 8        
                          
                  
                        
  
  
  
  
  
File
  modules/message_notify_ui/src/Plugin/MessageNotifyUiSenderSettingsForm/MessageNotifyUiSenderMailSettingsForm.php
  
    View source  
  <?php
namespace Drupal\message_notify_ui\Plugin\MessageNotifyUiSenderSettingsForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\message_notify\MessageNotifier;
use Drupal\message_notify_ui\MessageNotifyUiSenderSettingsFormBase;
use Drupal\message_notify_ui\MessageNotifyUiSenderSettingsFormInterface;
class MessageNotifyUiSenderMailSettingsForm extends MessageNotifyUiSenderSettingsFormBase implements MessageNotifyUiSenderSettingsFormInterface {
  
  public function form() {
    return [
      'use_custom' => [
        '#type' => 'checkbox',
        '#title' => t('Use custom email'),
        '#description' => t('Use the message owner message'),
      ],
      'email' => [
        '#type' => 'email',
        '#title' => t('Email address'),
        '#description' => t('The email address'),
        '#states' => [
          'visible' => [
            ':input[name="use_custom"]' => [
              'checked' => TRUE,
            ],
          ],
        ],
      ],
    ];
  }
  
  public function validate(array $form, FormStateInterface $formState) {
    if ($formState
      ->getValue('use_custom') && !$formState
      ->getValue('email')) {
      $formState
        ->setErrorByName('email', t('The email field cannot be empty.'));
    }
  }
  
  public function submit(MessageNotifier $notifier, FormStateInterface $formState) {
    $settings = [];
    if ($formState
      ->getValue('use_custom')) {
      $settings['mail'] = $formState
        ->getValue('email');
    }
    if ($formState
      ->getValue('language')) {
      $settings['language override'] = $formState
        ->getValue($formState
        ->getValue('language'));
    }
    if ($notifier
      ->send($this
      ->getMessage(), $settings, 'email')) {
      \Drupal::messenger()
        ->addMessage(t('The email sent successfully.'));
    }
  }
}