You are here

public function MessagePurgePluginManager::purgeSettingsForm in Message 8

Construct the purge method form.

This can be used on either the message template form, or the global message settings form.

Parameters

array $form: The form array.

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

array $purge_settings: The default purge plugin settings to use.

File

src/MessagePurgePluginManager.php, line 48

Class

MessagePurgePluginManager
Plugin type manager for MessagePurge plugins.

Namespace

Drupal\message

Code

public function purgeSettingsForm(array &$form, FormStateInterface $form_state, array $purge_settings) {

  // Loop through all purge plugins and add to form.
  $form['settings']['purge_methods'] = [
    '#type' => 'table',
    '#states' => [
      'visible' => [
        // Configure visibility for both the template form and the global
        // settings form.
        [
          [
            ':input[name="settings[purge_override]"]' => [
              'checked' => TRUE,
            ],
          ],
          'or',
          [
            ':input[name="settings[purge_enable]"]' => [
              'checked' => TRUE,
            ],
          ],
        ],
      ],
    ],
    '#theme_wrappers' => [
      'form_element',
    ],
    '#header' => [
      $this
        ->t('Purge method'),
      $this
        ->t('Weight'),
      $this
        ->t('Enabled'),
      $this
        ->t('Settings'),
    ],
    '#tabledrag' => [
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'message-purge-order-weight',
      ],
    ],
  ];
  $user_input = $form_state
    ->getUserInput();
  $definitions = $this
    ->getDefinitions();
  $this
    ->sortDefinitions($definitions, $purge_settings);
  foreach ($definitions as $plugin_id => $definition) {

    /** @var \Drupal\message\MessagePurgeInterface $plugin */
    $plugin = $this
      ->createInstance($plugin_id, isset($purge_settings[$plugin_id]) ? $purge_settings[$plugin_id] : []);

    // Create the table row.
    $form['settings']['purge_methods'][$plugin_id]['#attributes']['class'][] = 'draggable';
    $form['settings']['purge_methods'][$plugin_id]['#weight'] = isset($user_input['settings']['purge_methods']) ? $user_input['settings']['purge_methods'][$plugin_id]['weight'] : $plugin
      ->getWeight();
    $form['settings']['purge_methods'][$plugin_id]['plugin'] = [
      '#tree' => FALSE,
      'data' => [
        'label' => [
          '#plain_text' => $plugin
            ->label(),
        ],
      ],
    ];

    // Purge weight.
    $form['settings']['purge_methods'][$plugin_id]['weight'] = [
      '#type' => 'weight',
      '#title' => $this
        ->t('Weight for @title', [
        '@title' => $plugin
          ->label(),
      ]),
      '#title_display' => 'invisible',
      '#default_value' => $plugin
        ->getWeight(),
      '#attributes' => [
        'class' => [
          'message-purge-order-weight',
        ],
      ],
    ];

    // Plugin enabled.
    $form['settings']['purge_methods'][$plugin_id]['enabled'] = [
      '#type' => 'checkbox',
      '#title' => $plugin
        ->description(),
      '#default_value' => isset($purge_settings[$plugin_id]),
    ];

    // Purge plugin-specific settings.
    $element = [];
    $form['settings']['purge_methods'][$plugin_id]['data'] = $plugin
      ->buildConfigurationForm($element, $form_state);
  }
}