You are here

abstract class WebformAdminConfigBaseForm in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Form/AdminConfig/WebformAdminConfigBaseForm.php \Drupal\webform\Form\AdminConfig\WebformAdminConfigBaseForm

Base webform admin settings form.

Hierarchy

Expanded class hierarchy of WebformAdminConfigBaseForm

File

src/Form/AdminConfig/WebformAdminConfigBaseForm.php, line 17

Namespace

Drupal\webform\Form\AdminConfig
View source
abstract class WebformAdminConfigBaseForm extends ConfigFormBase {

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

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $config = $this
      ->config('webform.settings');
    _webform_config_update($config);
    $config
      ->save();
    parent::submitForm($form, $form_state);
  }

  /**
   * Build bulk operation settings for webforms and submissions.
   *
   * @param array $settings
   *   Webform settings.
   * @param $entity_type_id
   *   The entity type id. (webform or webform_submission)
   *
   * @return array
   */
  protected function buildBulkOperations(array $settings, $entity_type_id) {
    $element = [
      '#type' => 'details',
      '#title' => $entity_type_id === 'webform_submission' ? $this
        ->t('Submissions bulk operations settings') : $this
        ->t('Form bulk operations settings'),
      '#open' => TRUE,
      '#tree' => TRUE,
    ];

    // Enable.
    $settings += [
      $entity_type_id . '_bulk_form' => TRUE,
    ];
    $element[$entity_type_id . '_bulk_form'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Enabled webform bulk operations'),
      '#description' => $entity_type_id === 'webform_submission' ? $this
        ->t('If checked, bulk operations will be displayed on the submission results page.') : $this
        ->t('If checked, bulk operations will be displayed on the form manager page.'),
      '#return_value' => TRUE,
      '#default_value' => $settings[$entity_type_id . '_bulk_form'],
    ];

    // Actions.
    $options = [];
    $default_actions = [];

    /** @var \Drupal\system\ActionConfigEntityInterface[] $actions */
    $actions = \Drupal::entityTypeManager()
      ->getStorage('action')
      ->loadMultiple();
    foreach ($actions as $action) {
      if ($action
        ->getType() === $entity_type_id) {
        $options[$action
          ->id()] = [
          'label' => $action
            ->label(),
        ];
        $default_actions[] = $action
          ->id();
      }
    }
    $settings += [
      $entity_type_id . '_bulk_form_actions' => $default_actions,
    ];
    $element[$entity_type_id . '_bulk_form_actions'] = [
      '#type' => 'webform_tableselect_sort',
      '#title' => $entity_type_id === 'webform_submission' ? $this
        ->t('Submissions selected actions') : $this
        ->t('Form selected actions'),
      '#header' => [
        'label' => $this
          ->t('Selected actions'),
      ],
      '#options' => $options,
      '#default_value' => array_combine($settings[$entity_type_id . '_bulk_form_actions'], $settings[$entity_type_id . '_bulk_form_actions']),
      '#states' => [
        'visible' => [
          ':input[name="bulk_form_settings[' . $entity_type_id . '_bulk_form]"]' => [
            'checked' => TRUE,
          ],
        ],
        'required' => [
          ':input[name="bulk_form_settings[' . $entity_type_id . '_bulk_form]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
      '#element_validate' => [
        [
          get_class($this),
          'validateBulkFormActions',
        ],
      ],
    ];
    WebformElementHelper::fixStatesWrapper($element[$entity_type_id . '_bulk_form_actions']);
    return $element;
  }

  /**
   * Form API callback. Validate bulk form actions.
   */
  public static function validateBulkFormActions(array &$element, FormStateInterface $form_state) {
    $actions_value = NestedArray::getValue($form_state
      ->getValues(), $element['#parents']);
    $enabled_parents = $element['#parents'];
    $enabled_parents[1] = str_replace('_actions', '', $enabled_parents[1]);
    $enabled_value = NestedArray::getValue($form_state
      ->getValues(), $enabled_parents);
    if (!empty($enabled_value) && empty($actions_value)) {
      $form_state
        ->setErrorByName(NULL, t('@name field is required.', [
        '@name' => $element['#title'],
      ]));
    }

    // Convert actions associative array of values to an indexed array.
    $actions_value = array_values($actions_value);
    $element['#value'] = $actions_value;
    $form_state
      ->setValueForElement($element, $actions_value);
  }

  /****************************************************************************/

  // Exclude plugins.

  /****************************************************************************/

  /**
   * Build excluded plugins element.
   *
   * @param \Drupal\Component\Plugin\PluginManagerInterface $plugin_manager
   *   A webform element, handler, or exporter plugin manager.
   * @param array $excluded_ids
   *   An array of excluded ids.
   *
   * @return array
   *   A table select element used to excluded plugins by id.
   */
  protected function buildExcludedPlugins(PluginManagerInterface $plugin_manager, array $excluded_ids) {
    $header = [
      'title' => [
        'data' => $this
          ->t('Title'),
      ],
      'id' => [
        'data' => $this
          ->t('Name'),
        'class' => [
          RESPONSIVE_PRIORITY_LOW,
        ],
      ],
      'description' => [
        'data' => $this
          ->t('Description'),
        'class' => [
          RESPONSIVE_PRIORITY_LOW,
        ],
      ],
    ];
    $ids = [];
    $options = [];
    $plugins = $this
      ->getPluginDefinitions($plugin_manager);
    foreach ($plugins as $id => $plugin_definition) {
      $ids[$id] = $id;
      $description = [
        'data' => [
          'content' => [
            '#markup' => $plugin_definition['description'],
          ],
        ],
      ];
      if (!empty($plugin_definition['deprecated'])) {
        $description['data']['deprecated'] = [
          '#type' => 'webform_message',
          '#message_message' => $plugin_definition['deprecated_message'],
          '#message_type' => 'warning',
        ];
      }
      $options[$id] = [
        'title' => $plugin_definition['label'],
        'id' => $plugin_definition['id'],
        'description' => $description,
      ];
    }
    $element = [
      '#type' => 'tableselect',
      '#header' => $header,
      '#options' => $options,
      '#required' => TRUE,
      '#sticky' => TRUE,
      '#default_value' => array_diff($ids, $excluded_ids),
    ];
    TableSelect::setProcessTableSelectCallback($element);
    return $element;
  }

  /**
   * Convert included ids returned from table select element to excluded ids.
   *
   * @param \Drupal\Component\Plugin\PluginManagerInterface $plugin_manager
   *   A webform element, handler, or exporter plugin manager.
   * @param array $included_ids
   *   An array of included_ids.
   *
   * @return array
   *   An array of excluded ids.
   *
   * @see \Drupal\webform\Form\WebformAdminSettingsForm::buildExcludedPlugins
   */
  protected function convertIncludedToExcludedPluginIds(PluginManagerInterface $plugin_manager, array $included_ids) {
    $ids = [];
    $plugins = $this
      ->getPluginDefinitions($plugin_manager);
    foreach ($plugins as $id => $plugin) {
      $ids[$id] = $id;
    }
    $excluded_ids = array_diff($ids, array_filter($included_ids));
    ksort($excluded_ids);
    return $excluded_ids;
  }

  /**
   * Get plugin definitions.
   *
   * @param \Drupal\Component\Plugin\PluginManagerInterface $plugin_manager
   *   A webform element, handler, or exporter plugin manager.
   *
   * @return array
   *   Plugin definitions.
   */
  protected function getPluginDefinitions(PluginManagerInterface $plugin_manager) {
    $plugins = $plugin_manager
      ->getDefinitions();
    $plugins = $plugin_manager
      ->getSortedDefinitions($plugins);
    if ($plugin_manager instanceof WebformElementManagerInterface) {
      unset($plugins['webform_element']);
    }
    elseif ($plugin_manager instanceof WebformHandlerManager || $plugin_manager instanceof WebformVariantManager) {
      unset($plugins['broken']);
    }
    return $plugins;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBase::buildForm public function Form constructor. Overrides FormInterface::buildForm 26
ConfigFormBase::create public static function Instantiates a new instance of this class. Overrides FormBase::create 13
ConfigFormBase::__construct public function Constructs a \Drupal\system\ConfigFormBase object. 11
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
FormInterface::getFormId public function Returns a unique string identifying the form. 236
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.
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.
WebformAdminConfigBaseForm::buildBulkOperations protected function Build bulk operation settings for webforms and submissions.
WebformAdminConfigBaseForm::buildExcludedPlugins protected function Build excluded plugins element.
WebformAdminConfigBaseForm::convertIncludedToExcludedPluginIds protected function Convert included ids returned from table select element to excluded ids.
WebformAdminConfigBaseForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
WebformAdminConfigBaseForm::getPluginDefinitions protected function Get plugin definitions.
WebformAdminConfigBaseForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm 8
WebformAdminConfigBaseForm::validateBulkFormActions public static function Form API callback. Validate bulk form actions.