3 namespace Drupal\aggregator\Form;
5 use Drupal\aggregator\Plugin\AggregatorPluginManager;
6 use Drupal\Component\Utility\SafeMarkup;
7 use Drupal\Core\Config\ConfigFactoryInterface;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\Plugin\PluginFormInterface;
10 use Drupal\Core\StringTranslation\TranslationInterface;
11 use Drupal\Core\Form\ConfigFormBase;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
15 * Configures aggregator settings for this site.
19 class SettingsForm extends ConfigFormBase {
22 * The aggregator plugin managers.
24 * @var \Drupal\aggregator\Plugin\AggregatorPluginManager[]
26 protected $managers = [];
29 * The instantiated plugin instances that have configuration forms.
31 * @var \Drupal\Core\Plugin\PluginFormInterface[]
33 protected $configurableInstances = [];
36 * The aggregator plugin definitions.
40 protected $definitions = [
47 * Constructs a \Drupal\aggregator\SettingsForm object.
49 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
50 * The factory for configuration objects.
51 * @param \Drupal\aggregator\Plugin\AggregatorPluginManager $fetcher_manager
52 * The aggregator fetcher plugin manager.
53 * @param \Drupal\aggregator\Plugin\AggregatorPluginManager $parser_manager
54 * The aggregator parser plugin manager.
55 * @param \Drupal\aggregator\Plugin\AggregatorPluginManager $processor_manager
56 * The aggregator processor plugin manager.
57 * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
58 * The string translation manager.
60 public function __construct(ConfigFactoryInterface $config_factory, AggregatorPluginManager $fetcher_manager, AggregatorPluginManager $parser_manager, AggregatorPluginManager $processor_manager, TranslationInterface $string_translation) {
61 parent::__construct($config_factory);
62 $this->stringTranslation = $string_translation;
64 'fetcher' => $fetcher_manager,
65 'parser' => $parser_manager,
66 'processor' => $processor_manager,
68 // Get all available fetcher, parser and processor definitions.
69 foreach (['fetcher', 'parser', 'processor'] as $type) {
70 foreach ($this->managers[$type]->getDefinitions() as $id => $definition) {
71 $this->definitions[$type][$id] = SafeMarkup::format('@title <span class="description">@description</span>', ['@title' => $definition['title'], '@description' => $definition['description']]);
79 public static function create(ContainerInterface $container) {
81 $container->get('config.factory'),
82 $container->get('plugin.manager.aggregator.fetcher'),
83 $container->get('plugin.manager.aggregator.parser'),
84 $container->get('plugin.manager.aggregator.processor'),
85 $container->get('string_translation')
92 public function getFormId() {
93 return 'aggregator_admin_form';
99 protected function getEditableConfigNames() {
100 return ['aggregator.settings'];
106 public function buildForm(array $form, FormStateInterface $form_state) {
107 $config = $this->config('aggregator.settings');
109 // Global aggregator settings.
110 $form['aggregator_allowed_html_tags'] = [
111 '#type' => 'textfield',
112 '#title' => $this->t('Allowed HTML tags'),
115 '#default_value' => $config->get('items.allowed_html'),
116 '#description' => $this->t('A space-separated list of HTML tags allowed in the content of feed items. Disallowed tags are stripped from the content.'),
119 // Only show basic configuration if there are actually options.
121 if (count($this->definitions['fetcher']) > 1) {
122 $basic_conf['aggregator_fetcher'] = [
124 '#title' => $this->t('Fetcher'),
125 '#description' => $this->t('Fetchers download data from an external source. Choose a fetcher suitable for the external source you would like to download from.'),
126 '#options' => $this->definitions['fetcher'],
127 '#default_value' => $config->get('fetcher'),
130 if (count($this->definitions['parser']) > 1) {
131 $basic_conf['aggregator_parser'] = [
133 '#title' => $this->t('Parser'),
134 '#description' => $this->t('Parsers transform downloaded data into standard structures. Choose a parser suitable for the type of feeds you would like to aggregate.'),
135 '#options' => $this->definitions['parser'],
136 '#default_value' => $config->get('parser'),
139 if (count($this->definitions['processor']) > 1) {
140 $basic_conf['aggregator_processors'] = [
141 '#type' => 'checkboxes',
142 '#title' => $this->t('Processors'),
143 '#description' => $this->t('Processors act on parsed feed data, for example they store feed items. Choose the processors suitable for your task.'),
144 '#options' => $this->definitions['processor'],
145 '#default_value' => $config->get('processors'),
148 if (count($basic_conf)) {
149 $form['basic_conf'] = [
150 '#type' => 'details',
151 '#title' => $this->t('Basic configuration'),
152 '#description' => $this->t('For most aggregation tasks, the default settings are fine.'),
155 $form['basic_conf'] += $basic_conf;
158 // Call buildConfigurationForm() on the active fetcher and parser.
159 foreach (['fetcher', 'parser'] as $type) {
160 $active = $config->get($type);
161 if (array_key_exists($active, $this->definitions[$type])) {
162 $instance = $this->managers[$type]->createInstance($active);
163 if ($instance instanceof PluginFormInterface) {
164 $form = $instance->buildConfigurationForm($form, $form_state);
165 // Store the instance for validate and submit handlers.
166 // Keying by ID would bring conflicts, because two instances of a
167 // different type could have the same ID.
168 $this->configurableInstances[] = $instance;
173 // Implementing processor plugins will expect an array at $form['processors'].
174 $form['processors'] = [];
175 // Call buildConfigurationForm() for each active processor.
176 foreach ($this->definitions['processor'] as $id => $definition) {
177 if (in_array($id, $config->get('processors'))) {
178 $instance = $this->managers['processor']->createInstance($id);
179 if ($instance instanceof PluginFormInterface) {
180 $form = $instance->buildConfigurationForm($form, $form_state);
181 // Store the instance for validate and submit handlers.
182 // Keying by ID would bring conflicts, because two instances of a
183 // different type could have the same ID.
184 $this->configurableInstances[] = $instance;
189 return parent::buildForm($form, $form_state);
195 public function validateForm(array &$form, FormStateInterface $form_state) {
196 parent::validateForm($form, $form_state);
197 // Let active plugins validate their settings.
198 foreach ($this->configurableInstances as $instance) {
199 $instance->validateConfigurationForm($form, $form_state);
206 public function submitForm(array &$form, FormStateInterface $form_state) {
207 parent::submitForm($form, $form_state);
208 $config = $this->config('aggregator.settings');
209 // Let active plugins save their settings.
210 foreach ($this->configurableInstances as $instance) {
211 $instance->submitConfigurationForm($form, $form_state);
214 $config->set('items.allowed_html', $form_state->getValue('aggregator_allowed_html_tags'));
215 if ($form_state->hasValue('aggregator_fetcher')) {
216 $config->set('fetcher', $form_state->getValue('aggregator_fetcher'));
218 if ($form_state->hasValue('aggregator_parser')) {
219 $config->set('parser', $form_state->getValue('aggregator_parser'));
221 if ($form_state->hasValue('aggregator_processors')) {
222 $config->set('processors', array_filter($form_state->getValue('aggregator_processors')));