You are here

class SettingsForm in Drupal 9

Same name in this branch
  1. 9 core/modules/media_library/src/Form/SettingsForm.php \Drupal\media_library\Form\SettingsForm
  2. 9 core/modules/aggregator/src/Form/SettingsForm.php \Drupal\aggregator\Form\SettingsForm
Same name and namespace in other branches
  1. 8 core/modules/aggregator/src/Form/SettingsForm.php \Drupal\aggregator\Form\SettingsForm

Configures aggregator settings for this site.

@internal

Hierarchy

Expanded class hierarchy of SettingsForm

1 file declares its use of SettingsForm
AggregatorPluginSettingsBaseTest.php in core/modules/aggregator/tests/src/Unit/Plugin/AggregatorPluginSettingsBaseTest.php
1 string reference to 'SettingsForm'
aggregator.routing.yml in core/modules/aggregator/aggregator.routing.yml
core/modules/aggregator/aggregator.routing.yml

File

core/modules/aggregator/src/Form/SettingsForm.php, line 19

Namespace

Drupal\aggregator\Form
View source
class SettingsForm extends ConfigFormBase {

  /**
   * The aggregator plugin managers.
   *
   * @var \Drupal\aggregator\Plugin\AggregatorPluginManager[]
   */
  protected $managers = [];

  /**
   * The instantiated plugin instances that have configuration forms.
   *
   * @var \Drupal\Core\Plugin\PluginFormInterface[]
   */
  protected $configurableInstances = [];

  /**
   * The aggregator plugin definitions.
   *
   * @var array
   */
  protected $definitions = [
    'fetcher' => [],
    'parser' => [],
    'processor' => [],
  ];

  /**
   * Constructs a \Drupal\aggregator\SettingsForm object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The factory for configuration objects.
   * @param \Drupal\aggregator\Plugin\AggregatorPluginManager $fetcher_manager
   *   The aggregator fetcher plugin manager.
   * @param \Drupal\aggregator\Plugin\AggregatorPluginManager $parser_manager
   *   The aggregator parser plugin manager.
   * @param \Drupal\aggregator\Plugin\AggregatorPluginManager $processor_manager
   *   The aggregator processor plugin manager.
   * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
   *   The string translation manager.
   */
  public function __construct(ConfigFactoryInterface $config_factory, AggregatorPluginManager $fetcher_manager, AggregatorPluginManager $parser_manager, AggregatorPluginManager $processor_manager, TranslationInterface $string_translation) {
    parent::__construct($config_factory);
    $this->stringTranslation = $string_translation;
    $this->managers = [
      'fetcher' => $fetcher_manager,
      'parser' => $parser_manager,
      'processor' => $processor_manager,
    ];

    // Get all available fetcher, parser and processor definitions.
    foreach ([
      'fetcher',
      'parser',
      'processor',
    ] as $type) {
      foreach ($this->managers[$type]
        ->getDefinitions() as $id => $definition) {
        $this->definitions[$type][$id] = new FormattableMarkup('@title <span class="description">@description</span>', [
          '@title' => $definition['title'],
          '@description' => $definition['description'],
        ]);
      }
    }
  }

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

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this
      ->config('aggregator.settings');

    // Global aggregator settings.
    $form['aggregator_allowed_html_tags'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Allowed HTML tags'),
      '#size' => 80,
      '#maxlength' => 255,
      '#default_value' => $config
        ->get('items.allowed_html'),
      '#description' => $this
        ->t('A space-separated list of HTML tags allowed in the content of feed items. Disallowed tags are stripped from the content.'),
    ];

    // Only show basic configuration if there are actually options.
    $basic_conf = [];
    if (count($this->definitions['fetcher']) > 1) {
      $basic_conf['aggregator_fetcher'] = [
        '#type' => 'radios',
        '#title' => $this
          ->t('Fetcher'),
        '#description' => $this
          ->t('Fetchers download data from an external source. Choose a fetcher suitable for the external source you would like to download from.'),
        '#options' => $this->definitions['fetcher'],
        '#default_value' => $config
          ->get('fetcher'),
      ];
    }
    if (count($this->definitions['parser']) > 1) {
      $basic_conf['aggregator_parser'] = [
        '#type' => 'radios',
        '#title' => $this
          ->t('Parser'),
        '#description' => $this
          ->t('Parsers transform downloaded data into standard structures. Choose a parser suitable for the type of feeds you would like to aggregate.'),
        '#options' => $this->definitions['parser'],
        '#default_value' => $config
          ->get('parser'),
      ];
    }
    if (count($this->definitions['processor']) > 1) {
      $basic_conf['aggregator_processors'] = [
        '#type' => 'checkboxes',
        '#title' => $this
          ->t('Processors'),
        '#description' => $this
          ->t('Processors act on parsed feed data, for example they store feed items. Choose the processors suitable for your task.'),
        '#options' => $this->definitions['processor'],
        '#default_value' => $config
          ->get('processors'),
      ];
    }
    if (count($basic_conf)) {
      $form['basic_conf'] = [
        '#type' => 'details',
        '#title' => $this
          ->t('Basic configuration'),
        '#description' => $this
          ->t('For most aggregation tasks, the default settings are fine.'),
        '#open' => TRUE,
      ];
      $form['basic_conf'] += $basic_conf;
    }

    // Call buildConfigurationForm() on the active fetcher and parser.
    foreach ([
      'fetcher',
      'parser',
    ] as $type) {
      $active = $config
        ->get($type);
      if (array_key_exists($active, $this->definitions[$type])) {
        $instance = $this->managers[$type]
          ->createInstance($active);
        if ($instance instanceof PluginFormInterface) {
          $form = $instance
            ->buildConfigurationForm($form, $form_state);

          // Store the instance for validate and submit handlers.
          // Keying by ID would bring conflicts, because two instances of a
          // different type could have the same ID.
          $this->configurableInstances[] = $instance;
        }
      }
    }

    // Implementing processor plugins will expect an array at $form['processors'].
    $form['processors'] = [];

    // Call buildConfigurationForm() for each active processor.
    foreach ($this->definitions['processor'] as $id => $definition) {
      if (in_array($id, $config
        ->get('processors'))) {
        $instance = $this->managers['processor']
          ->createInstance($id);
        if ($instance instanceof PluginFormInterface) {
          $form = $instance
            ->buildConfigurationForm($form, $form_state);

          // Store the instance for validate and submit handlers.
          // Keying by ID would bring conflicts, because two instances of a
          // different type could have the same ID.
          $this->configurableInstances[] = $instance;
        }
      }
    }
    return parent::buildForm($form, $form_state);
  }

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

    // Let active plugins validate their settings.
    foreach ($this->configurableInstances as $instance) {
      $instance
        ->validateConfigurationForm($form, $form_state);
    }
  }

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

    // Let active plugins save their settings.
    foreach ($this->configurableInstances as $instance) {
      $instance
        ->submitConfigurationForm($form, $form_state);
    }
    $config
      ->set('items.allowed_html', $form_state
      ->getValue('aggregator_allowed_html_tags'));
    if ($form_state
      ->hasValue('aggregator_fetcher')) {
      $config
        ->set('fetcher', $form_state
        ->getValue('aggregator_fetcher'));
    }
    if ($form_state
      ->hasValue('aggregator_parser')) {
      $config
        ->set('parser', $form_state
        ->getValue('aggregator_parser'));
    }
    if ($form_state
      ->hasValue('aggregator_processors')) {
      $config
        ->set('processors', array_filter($form_state
        ->getValue('aggregator_processors')));
    }
    $config
      ->save();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 3
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. 3
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.
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.
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. 27
MessengerTrait::messenger public function Gets the messenger. 27
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.
SettingsForm::$configurableInstances protected property The instantiated plugin instances that have configuration forms.
SettingsForm::$definitions protected property The aggregator plugin definitions.
SettingsForm::$managers protected property The aggregator plugin managers.
SettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
SettingsForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
SettingsForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
SettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
SettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
SettingsForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
SettingsForm::__construct public function Constructs a \Drupal\aggregator\SettingsForm object. Overrides ConfigFormBase::__construct
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
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.