You are here

public function Settings::buildForm in Courier 8

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

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides ConfigFormBase::buildForm

File

src/Form/Settings.php, line 84

Class

Settings
Configure Courier settings.

Namespace

Drupal\courier\Form

Code

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;
}