You are here

class Settings in Courier 8

Same name in this branch
  1. 8 src/Form/Settings.php \Drupal\courier\Form\Settings
  2. 8 courier_system/src/Form/Settings.php \Drupal\courier_system\Form\Settings
Same name and namespace in other branches
  1. 2.x src/Form/Settings.php \Drupal\courier\Form\Settings

Configure Courier settings.

Hierarchy

Expanded class hierarchy of Settings

2 string references to 'Settings'
courier.links.task.yml in ./courier.links.task.yml
courier.links.task.yml
courier.routing.yml in ./courier.routing.yml
courier.routing.yml

File

src/Form/Settings.php, line 15

Namespace

Drupal\courier\Form
View source
class Settings extends ConfigFormBase {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The RNG event manager.
   *
   * @var \Drupal\rng\EventManagerInterface
   */
  protected $eventManager;

  /**
   * The identity channel manager.
   *
   * @var \Drupal\courier\Service\IdentityChannelManagerInterface
   */
  protected $identityChannelManager;

  /**
   * Constructs a \Drupal\system\ConfigFormBase object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The factory for configuration objects.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\courier\Service\IdentityChannelManagerInterface $identity_channel_manager
   *   The identity channel manager.
   */
  public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager, IdentityChannelManagerInterface $identity_channel_manager) {
    parent::__construct($config_factory);
    $this->entityTypeManager = $entity_type_manager;
    $this->identityChannelManager = $identity_channel_manager;
  }

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

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildForm($form, $form_state);
    $config = $this
      ->config('courier.settings');
    $preferences = $config
      ->get('channel_preferences');
    $header = [
      'label' => $this
        ->t('Channel'),
      'weight' => $this
        ->t('Weight'),
      'enabled' => [
        'data' => $this
          ->t('Enabled'),
        'class' => [
          'checkbox',
        ],
      ],
    ];
    $form['identity_channel_preference'] = [
      '#title' => $this
        ->t('Channel preference defaults'),
      '#type' => 'vertical_tabs',
    ];
    $form['identity_types']['#tree'] = TRUE;
    $identity_types = $this->identityChannelManager
      ->getIdentityTypes();
    foreach ($identity_types as $identity_type) {
      $entity_definition = $this->entityTypeManager
        ->getDefinition($identity_type);
      $t_args = [
        '@identity_type' => $entity_definition
          ->getLabel(),
      ];
      $form['identity_types'][$identity_type] = [
        '#type' => 'details',
        '#title' => $entity_definition
          ->getLabel(),
        '#open' => TRUE,
        '#group' => 'identity_channel_preference',
      ];
      $form['identity_types'][$identity_type]['channels'] = [
        '#prefix' => '<p>' . $this
          ->t("The following channels are attempted, in order, for @identity_types who have not set preferences for themselves.\nThe first successful message for a channel will be transmitted, all subsequent channels are ignored.", $t_args) . '</p>',
        '#type' => 'table',
        '#header' => $header,
        '#empty' => $this
          ->t('No channels found for @identity_type.'),
        '#attributes' => [
          'id' => 'identity-types-' . $identity_type,
        ],
        '#tabledrag' => [
          [
            'action' => 'order',
            'relationship' => 'sibling',
            'group' => 'channel-weight',
          ],
        ],
      ];

      // Ensure channels are ordered correctly and apply is enabled to value.
      $channels = [];
      $channels_all = $this->identityChannelManager
        ->getChannelsForIdentityType($identity_type);

      // Add existing channels in, ensure channels still exist.
      if (array_key_exists($identity_type, $preferences)) {
        $channels = array_fill_keys(array_intersect($preferences[$identity_type], $channels_all), TRUE);
      }

      // Add in channels missing (disabled) from config.
      $channels += array_fill_keys(array_diff($channels_all, $channels), FALSE);
      foreach ($channels as $channel => $enabled) {
        $entity_definition = $this->entityTypeManager
          ->getDefinition($channel);
        $t_args['@channel'] = $channel;
        $row = [];
        $row['#attributes']['class'][] = 'draggable';
        $row['label']['#markup'] = $entity_definition
          ->getLabel();
        $row['weight'] = [
          '#type' => 'weight',
          '#title' => t('Weight for @channel', $t_args),
          '#title_display' => 'invisible',
          '#default_value' => NULL,
          '#attributes' => [
            'class' => [
              'channel-weight',
            ],
          ],
        ];
        $row['enabled'] = [
          '#type' => 'checkbox',
          '#title' => $this
            ->t('Enabled'),
          '#title_display' => 'invisible',
          '#default_value' => $enabled,
          '#wrapper_attributes' => [
            'class' => [
              'checkbox',
            ],
          ],
        ];
        $form['identity_types'][$identity_type]['channels'][$channel] = $row;
      }
    }
    $form['devel'] = [
      '#type' => 'details',
      '#open' => TRUE,
    ];
    $form['devel']['skip_queue'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Skip queue'),
      '#description' => $this
        ->t('Whether messages skip the load balancing queue and process in the same request. Only turn on this setting when debugging, do not use it on production sites.'),
      '#default_value' => $config
        ->get('skip_queue'),
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $config = $this
      ->config('courier.settings');
    $channel_preferences = [];
    foreach ($form_state
      ->getValue('identity_types') as $identity_type => $settings) {
      foreach ($settings['channels'] as $channel => $row) {
        if (!empty($row['enabled'])) {
          $channel_preferences[$identity_type][] = $channel;
        }
      }
    }
    $config
      ->set('skip_queue', $form_state
      ->getValue('skip_queue'))
      ->set('channel_preferences', $channel_preferences)
      ->save();
    drupal_set_message(t('Settings saved.'));
  }

}

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.
Settings::$entityTypeManager protected property The entity type manager.
Settings::$eventManager protected property The RNG event manager.
Settings::$identityChannelManager protected property The identity channel manager.
Settings::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
Settings::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
Settings::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
Settings::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
Settings::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
Settings::__construct public function Constructs a \Drupal\system\ConfigFormBase object. 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.