You are here

class SocialSwiftmailSettingsForm in Open Social 8.9

Same name and namespace in other branches
  1. 8.2 modules/social_features/social_swiftmail/src/Form/SocialSwiftmailSettingsForm.php \Drupal\social_swiftmail\Form\SocialSwiftmailSettingsForm
  2. 8.3 modules/social_features/social_swiftmail/src/Form/SocialSwiftmailSettingsForm.php \Drupal\social_swiftmail\Form\SocialSwiftmailSettingsForm
  3. 8.4 modules/social_features/social_swiftmail/src/Form/SocialSwiftmailSettingsForm.php \Drupal\social_swiftmail\Form\SocialSwiftmailSettingsForm
  4. 8.5 modules/social_features/social_swiftmail/src/Form/SocialSwiftmailSettingsForm.php \Drupal\social_swiftmail\Form\SocialSwiftmailSettingsForm
  5. 8.6 modules/social_features/social_swiftmail/src/Form/SocialSwiftmailSettingsForm.php \Drupal\social_swiftmail\Form\SocialSwiftmailSettingsForm
  6. 8.7 modules/social_features/social_swiftmail/src/Form/SocialSwiftmailSettingsForm.php \Drupal\social_swiftmail\Form\SocialSwiftmailSettingsForm
  7. 8.8 modules/social_features/social_swiftmail/src/Form/SocialSwiftmailSettingsForm.php \Drupal\social_swiftmail\Form\SocialSwiftmailSettingsForm
  8. 10.3.x modules/social_features/social_swiftmail/src/Form/SocialSwiftmailSettingsForm.php \Drupal\social_swiftmail\Form\SocialSwiftmailSettingsForm
  9. 10.0.x modules/social_features/social_swiftmail/src/Form/SocialSwiftmailSettingsForm.php \Drupal\social_swiftmail\Form\SocialSwiftmailSettingsForm
  10. 10.1.x modules/social_features/social_swiftmail/src/Form/SocialSwiftmailSettingsForm.php \Drupal\social_swiftmail\Form\SocialSwiftmailSettingsForm
  11. 10.2.x modules/social_features/social_swiftmail/src/Form/SocialSwiftmailSettingsForm.php \Drupal\social_swiftmail\Form\SocialSwiftmailSettingsForm

Class SocialSwiftmailSettingsForm.

@package Drupal\social_swiftmail\Form

Hierarchy

Expanded class hierarchy of SocialSwiftmailSettingsForm

1 string reference to 'SocialSwiftmailSettingsForm'
social_swiftmail.routing.yml in modules/social_features/social_swiftmail/social_swiftmail.routing.yml
modules/social_features/social_swiftmail/social_swiftmail.routing.yml

File

modules/social_features/social_swiftmail/src/Form/SocialSwiftmailSettingsForm.php, line 19

Namespace

Drupal\social_swiftmail\Form
View source
class SocialSwiftmailSettingsForm extends ConfigFormBase {

  /**
   * The 'email' activity destination plugin.
   *
   * @var \Drupal\activity_send_email\Plugin\ActivityDestination\EmailActivityDestination
   */
  protected $emailActivityDestination;

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

  /**
   * SocialSwiftmailSettingsForm constructor.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The configuration factory.
   * @param \Drupal\activity_creator\Plugin\ActivityDestinationManager $activity_destination_manager
   *   The activity destination manager.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   */
  public function __construct(ConfigFactoryInterface $config_factory, ActivityDestinationManager $activity_destination_manager, ModuleHandlerInterface $module_handler) {
    parent::__construct($config_factory);
    $this->emailActivityDestination = $activity_destination_manager
      ->createInstance('email');
    $this->batchBuilder = new BatchBuilder();
    $this->moduleHandler = $module_handler;
  }

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

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'social_swiftmail.settings',
    ];
  }

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this
      ->config('social_swiftmail.settings');
    $form['notification'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Default email notification settings'),
      '#open' => FALSE,
    ];

    // Settings helper for admins.
    $form['notification']['helper'] = [
      '#type' => 'html_tag',
      '#tag' => 'p',
      '#value' => $this
        ->t('<b>Please note:</b> the change below will not impact the users who have changed their default email notification settings.'),
    ];

    // Get grouped default templates.
    $items = _activity_send_email_default_template_items();

    // Get all message templates.
    $email_message_templates = $this->emailActivityDestination
      ->getSendEmailMessageTemplates();

    // Alter message templates and add them to specific group.
    $this->moduleHandler
      ->alter('activity_send_email_notifications', $items, $email_message_templates);

    // Sort a list of email frequencies by weight.
    $email_frequencies = sort_email_frequency_options();
    $notification_options = [];

    // Place the sorted data in an actual form option.
    foreach ($email_frequencies as $option) {
      if ($option['id'] !== 'none') {
        $notification_options[$option['id']] = $option['name'];
      }
    }
    $template_frequencies = $config
      ->get('template_frequencies') ?: [];
    foreach ($items as $item_id => $item) {
      $rows = [];
      foreach ($item['templates'] as $template) {
        $rows[] = $this
          ->buildRow($template, $notification_options, $template_frequencies);
      }
      $form['notification'][$item_id] = [
        '#type' => 'table',
        '#caption' => [
          '#markup' => '<h6>' . $item['title'] . '</h6>',
        ],
        '#rows' => $rows,
      ];
    }
    $form['template'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Template configuration'),
      '#open' => FALSE,
    ];
    $template_header = $config
      ->get('template_header');
    $form['template']['template_header'] = [
      '#title' => $this
        ->t('Template header'),
      '#type' => 'text_format',
      '#default_value' => $template_header['value'] ?: '',
      '#format' => $template_header['format'] ?: 'mail_html',
      '#allowed_formats' => [
        'mail_html',
      ],
      '#description' => $this
        ->t('Enter information you want to show in the email notifications header'),
    ];
    $template_footer = $config
      ->get('template_footer');
    $form['template']['template_footer'] = [
      '#title' => $this
        ->t('Template footer'),
      '#type' => 'text_format',
      '#default_value' => $template_footer['value'] ?: '',
      '#format' => $template_footer['format'] ?: 'mail_html',
      '#allowed_formats' => [
        'mail_html',
      ],
      '#description' => $this
        ->t('Enter information you want to show in the email notifications footer'),
    ];
    $form['remove_open_social_branding'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Remove Open Social Branding'),
      '#description' => $this
        ->t('Open Social Branding will be replaced by site name (and slogan if available).'),
      '#default_value' => $config
        ->get('remove_open_social_branding'),
    ];
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    parent::submitForm($form, $form_state);

    // Save config.
    $config = $this
      ->config('social_swiftmail.settings');
    $config
      ->set('remove_open_social_branding', $form_state
      ->getValue('remove_open_social_branding'));

    // Set notification settings.
    $templates = $this->emailActivityDestination
      ->getSendEmailMessageTemplates();
    $user_inputs = $form_state
      ->getUserInput();
    foreach (array_keys($templates) as $template) {
      if (isset($user_inputs[$template])) {
        $config
          ->set('template_frequencies.' . $template, $user_inputs[$template]);
      }
    }

    // Set the template header and footer settings.
    $config
      ->set('template_header', $form_state
      ->getValue('template_header'));
    $config
      ->set('template_footer', $form_state
      ->getValue('template_footer'));
    $config
      ->save();
  }

  /**
   * Returns row for table.
   *
   * @param string $template
   *   Template ID.
   * @param array $notification_options
   *   Array of options.
   * @param array $template_frequencies
   *   Frequencies for all templates from config.
   *
   * @return array[]
   *   Row.
   */
  private function buildRow($template, array $notification_options, array $template_frequencies) {
    $email_message_templates = $this->emailActivityDestination
      ->getSendEmailMessageTemplates();
    $row = [
      [
        'width' => '50%',
        'data' => [
          '#plain_text' => $email_message_templates[$template],
        ],
      ],
    ];
    $default_value = isset($template_frequencies[$template]) ? $template_frequencies[$template] : 'immediately';
    foreach ($notification_options as $notification_id => $notification_option) {
      $parents_for_id = [
        $template,
        $notification_id,
      ];
      $row[] = [
        'data' => [
          '#type' => 'radio',
          '#title' => $notification_option,
          '#return_value' => $notification_id,
          '#value' => $default_value === $notification_id ? $notification_id : FALSE,
          '#name' => $template,
          '#id' => HtmlUtility::getUniqueId('edit-' . implode('-', $parents_for_id)),
        ],
      ];
    }
    return $row;
  }

}

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
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.
SocialSwiftmailSettingsForm::$emailActivityDestination protected property The 'email' activity destination plugin.
SocialSwiftmailSettingsForm::$moduleHandler protected property The module handler.
SocialSwiftmailSettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
SocialSwiftmailSettingsForm::buildRow private function Returns row for table.
SocialSwiftmailSettingsForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
SocialSwiftmailSettingsForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
SocialSwiftmailSettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
SocialSwiftmailSettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
SocialSwiftmailSettingsForm::__construct public function SocialSwiftmailSettingsForm constructor. Overrides ConfigFormBase::__construct
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.