You are here

class EmailBodyForm in Mass Contact 8

Email body settings form.

Hierarchy

Expanded class hierarchy of EmailBodyForm

1 file declares its use of EmailBodyForm
EmailBodyFormTest.php in src/Tests/Form/EmailBodyFormTest.php
1 string reference to 'EmailBodyForm'
mass_contact.routing.yml in ./mass_contact.routing.yml
mass_contact.routing.yml

File

src/Form/EmailBodyForm.php, line 16

Namespace

Drupal\mass_contact\Form
View source
class EmailBodyForm extends SettingsFormBase {

  /**
   * The Mass Contact helper service.
   *
   * @var \Drupal\mass_contact\MassContactInterface
   */
  protected $massContact;

  /**
   * The module handler service.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * Constructs the email body form.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory service.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler service.
   * @param \Drupal\mass_contact\MassContactInterface $mass_contact
   *   The mass contact helper service.
   */
  public function __construct(ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler, MassContactInterface $mass_contact) {
    parent::__construct($config_factory);
    $this->massContact = $mass_contact;
    $this->moduleHandler = $module_handler;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('config.factory'), $container
      ->get('module_handler'), $container
      ->get('mass_contact'));
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'mass_contact_email_body_settings';
  }

  /**
   * {@inheritdoc}
   */
  protected function getConfigKeys() {
    return [
      'message_format',
      'message_prefix',
      'message_suffix',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildForm($form, $form_state);
    $config = $this
      ->config('mass_contact.settings');

    // HTML options.
    $form['html_settings'] = [
      '#type' => 'details',
      '#open' => $this->massContact
        ->htmlSupported(),
      '#title' => $this
        ->t('HTML Settings'),
    ];
    if ($this->massContact
      ->htmlSupported()) {
      $format = $config
        ->get('message_format');
      $options = array_map(function (FilterFormatInterface $filter_format) {
        return $filter_format
          ->label();
      }, filter_formats());
      $form['html_settings']['message_format'] = [
        '#type' => 'select',
        '#title' => $this
          ->t('The default text format'),
        '#default_value' => $format,
        '#description' => $this
          ->t('This is the text format that will be initially selected. If you do not want to allow HTML messages, then specify a plain text text format and do not allow it to be overridden below. Keep in mind that the user sending the message may not have access to all the text formats that are available here.'),
        '#options' => $options,
      ];
    }
    else {
      $form['html_settings']['message_format'] = [
        '#type' => 'hidden',
        '#value' => 'plain_text',
      ];
      $form['html_settings']['no_mimemail'] = [
        '#type' => 'item',
        '#description' => $this
          ->t('To use HTML email for mass contact messages, the <a href="@mime">Mime Mail</a> module or <a href="@swift">Swiftmailer</a> module is required', [
          '@mime' => Url::fromUri('https://www.drupal.org/project/mimemail')
            ->toString(),
          '@swift' => Url::fromUri('https://www.drupal.org/project/swiftmailer')
            ->toString(),
        ]),
      ];
    }

    // Supplemental texts that are prepended and/or appended to every message.
    $form['supplemental_texts'] = [
      '#type' => 'details',
      '#open' => $config
        ->get('message_prefix.value') || $config
        ->get('message_suffix.value'),
      '#title' => $this
        ->t('Supplemental message body texts'),
      '#description' => $this
        ->t('You may specify additional text to insert before and/or after the message text of every mass email that is sent.'),
    ];
    $form['supplemental_texts']['message_prefix'] = [
      '#type' => 'text_format',
      '#title' => $this
        ->t('Text to be prepended to all messages'),
      '#default_value' => $config
        ->get('message_prefix.value'),
      '#format' => $config
        ->get('message_prefix.format'),
      '#description' => $this
        ->t('The text you specify in this field will be added to all Mass Contact messages sent out and will be placed before the message text entered in by the sender.'),
    ];
    $form['supplemental_texts']['message_suffix'] = [
      '#type' => 'text_format',
      '#title' => $this
        ->t('Text to be appended to all messages'),
      '#default_value' => $config
        ->get('message_suffix.value'),
      '#format' => $config
        ->get('message_suffix.format'),
      '#description' => $this
        ->t('The text you specify in this field will be added to all Mass Contact messages sent out and will be placed after the message text entered in by the sender.'),
    ];
    if (!$this->massContact
      ->htmlSupported()) {
      $form['supplemental_texts']['message_prefix']['#allowed_formats'] = [
        'plain_text',
      ];
      $form['supplemental_texts']['message_suffix']['#allowed_formats'] = [
        'plain_text',
      ];
    }
    if ($this->moduleHandler
      ->moduleExists('token')) {
      $form['supplemental_texts']['token_tree'] = [
        '#theme' => 'token_tree_link',
        '#token_types' => [
          'global',
        ],
        '#theme_wrappers' => [
          'form_element',
        ],
      ];
    }

    // Attachment options.
    // @todo Port attachment options.
    // @see https://www.drupal.org/node/2867544
    return $form;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
EmailBodyForm::$massContact protected property The Mass Contact helper service.
EmailBodyForm::$moduleHandler protected property The module handler service.
EmailBodyForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
EmailBodyForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
EmailBodyForm::getConfigKeys protected function Get config keys to save. Overrides SettingsFormBase::getConfigKeys
EmailBodyForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
EmailBodyForm::__construct public function Constructs the email body form. Overrides ConfigFormBase::__construct
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
SettingsFormBase::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
SettingsFormBase::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.