3014c5b9beb3d5b99905b1f475dcb4dba999849c
[yaffs-website] / web / core / modules / aggregator / src / Form / SettingsForm.php
1 <?php
2
3 namespace Drupal\aggregator\Form;
4
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;
13
14 /**
15  * Configures aggregator settings for this site.
16  *
17  * @internal
18  */
19 class SettingsForm extends ConfigFormBase {
20
21   /**
22    * The aggregator plugin managers.
23    *
24    * @var \Drupal\aggregator\Plugin\AggregatorPluginManager[]
25    */
26   protected $managers = [];
27
28   /**
29    * The instantiated plugin instances that have configuration forms.
30    *
31    * @var \Drupal\Core\Plugin\PluginFormInterface[]
32    */
33   protected $configurableInstances = [];
34
35   /**
36    * The aggregator plugin definitions.
37    *
38    * @var array
39    */
40   protected $definitions = [
41     'fetcher' => [],
42     'parser' => [],
43     'processor' => [],
44   ];
45
46   /**
47    * Constructs a \Drupal\aggregator\SettingsForm object.
48    *
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.
59    */
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;
63     $this->managers = [
64       'fetcher' => $fetcher_manager,
65       'parser' => $parser_manager,
66       'processor' => $processor_manager,
67     ];
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']]);
72       }
73     }
74   }
75
76   /**
77    * {@inheritdoc}
78    */
79   public static function create(ContainerInterface $container) {
80     return new static(
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')
86     );
87   }
88
89   /**
90    * {@inheritdoc}
91    */
92   public function getFormId() {
93     return 'aggregator_admin_form';
94   }
95
96   /**
97    * {@inheritdoc}
98    */
99   protected function getEditableConfigNames() {
100     return ['aggregator.settings'];
101   }
102
103   /**
104    * {@inheritdoc}
105    */
106   public function buildForm(array $form, FormStateInterface $form_state) {
107     $config = $this->config('aggregator.settings');
108
109     // Global aggregator settings.
110     $form['aggregator_allowed_html_tags'] = [
111       '#type' => 'textfield',
112       '#title' => $this->t('Allowed HTML tags'),
113       '#size' => 80,
114       '#maxlength' => 255,
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.'),
117     ];
118
119     // Only show basic configuration if there are actually options.
120     $basic_conf = [];
121     if (count($this->definitions['fetcher']) > 1) {
122       $basic_conf['aggregator_fetcher'] = [
123         '#type' => 'radios',
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'),
128       ];
129     }
130     if (count($this->definitions['parser']) > 1) {
131       $basic_conf['aggregator_parser'] = [
132         '#type' => 'radios',
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'),
137       ];
138     }
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'),
146       ];
147     }
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.'),
153         '#open' => TRUE,
154       ];
155       $form['basic_conf'] += $basic_conf;
156     }
157
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;
169         }
170       }
171     }
172
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;
185         }
186       }
187     }
188
189     return parent::buildForm($form, $form_state);
190   }
191
192   /**
193    * {@inheritdoc}
194    */
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);
200     }
201   }
202
203   /**
204    * {@inheritdoc}
205    */
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);
212     }
213
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'));
217     }
218     if ($form_state->hasValue('aggregator_parser')) {
219       $config->set('parser', $form_state->getValue('aggregator_parser'));
220     }
221     if ($form_state->hasValue('aggregator_processors')) {
222       $config->set('processors', array_filter($form_state->getValue('aggregator_processors')));
223     }
224     $config->save();
225   }
226
227 }