48b2dfe87e28f0d1140b1e78580dcc280580f081
[yaffs-website] / 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 class SettingsForm extends ConfigFormBase {
18
19   /**
20    * The aggregator plugin managers.
21    *
22    * @var \Drupal\aggregator\Plugin\AggregatorPluginManager[]
23    */
24   protected $managers = [];
25
26   /**
27    * The instantiated plugin instances that have configuration forms.
28    *
29    * @var \Drupal\Core\Plugin\PluginFormInterface[]
30    */
31   protected $configurableInstances = [];
32
33   /**
34    * The aggregator plugin definitions.
35    *
36    * @var array
37    */
38   protected $definitions = [
39     'fetcher' => [],
40     'parser' => [],
41     'processor' => [],
42   ];
43
44   /**
45    * Constructs a \Drupal\aggregator\SettingsForm object.
46    *
47    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
48    *   The factory for configuration objects.
49    * @param \Drupal\aggregator\Plugin\AggregatorPluginManager $fetcher_manager
50    *   The aggregator fetcher plugin manager.
51    * @param \Drupal\aggregator\Plugin\AggregatorPluginManager $parser_manager
52    *   The aggregator parser plugin manager.
53    * @param \Drupal\aggregator\Plugin\AggregatorPluginManager $processor_manager
54    *   The aggregator processor plugin manager.
55    * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
56    *   The string translation manager.
57    */
58   public function __construct(ConfigFactoryInterface $config_factory, AggregatorPluginManager $fetcher_manager, AggregatorPluginManager $parser_manager, AggregatorPluginManager $processor_manager, TranslationInterface $string_translation) {
59     parent::__construct($config_factory);
60     $this->stringTranslation = $string_translation;
61     $this->managers = [
62       'fetcher' => $fetcher_manager,
63       'parser' => $parser_manager,
64       'processor' => $processor_manager,
65     ];
66     // Get all available fetcher, parser and processor definitions.
67     foreach (['fetcher', 'parser', 'processor'] as $type) {
68       foreach ($this->managers[$type]->getDefinitions() as $id => $definition) {
69         $this->definitions[$type][$id] = SafeMarkup::format('@title <span class="description">@description</span>', ['@title' => $definition['title'], '@description' => $definition['description']]);
70       }
71     }
72   }
73
74   /**
75    * {@inheritdoc}
76    */
77   public static function create(ContainerInterface $container) {
78     return new static(
79       $container->get('config.factory'),
80       $container->get('plugin.manager.aggregator.fetcher'),
81       $container->get('plugin.manager.aggregator.parser'),
82       $container->get('plugin.manager.aggregator.processor'),
83       $container->get('string_translation')
84     );
85   }
86
87   /**
88    * {@inheritdoc}
89    */
90   public function getFormId() {
91     return 'aggregator_admin_form';
92   }
93
94   /**
95    * {@inheritdoc}
96    */
97   protected function getEditableConfigNames() {
98     return ['aggregator.settings'];
99   }
100
101   /**
102    * {@inheritdoc}
103    */
104   public function buildForm(array $form, FormStateInterface $form_state) {
105     $config = $this->config('aggregator.settings');
106
107     // Global aggregator settings.
108     $form['aggregator_allowed_html_tags'] = [
109       '#type' => 'textfield',
110       '#title' => $this->t('Allowed HTML tags'),
111       '#size' => 80,
112       '#maxlength' => 255,
113       '#default_value' => $config->get('items.allowed_html'),
114       '#description' => $this->t('A space-separated list of HTML tags allowed in the content of feed items. Disallowed tags are stripped from the content.'),
115     ];
116
117     // Only show basic configuration if there are actually options.
118     $basic_conf = [];
119     if (count($this->definitions['fetcher']) > 1) {
120       $basic_conf['aggregator_fetcher'] = [
121         '#type' => 'radios',
122         '#title' => $this->t('Fetcher'),
123         '#description' => $this->t('Fetchers download data from an external source. Choose a fetcher suitable for the external source you would like to download from.'),
124         '#options' => $this->definitions['fetcher'],
125         '#default_value' => $config->get('fetcher'),
126       ];
127     }
128     if (count($this->definitions['parser']) > 1) {
129       $basic_conf['aggregator_parser'] = [
130         '#type' => 'radios',
131         '#title' => $this->t('Parser'),
132         '#description' => $this->t('Parsers transform downloaded data into standard structures. Choose a parser suitable for the type of feeds you would like to aggregate.'),
133         '#options' => $this->definitions['parser'],
134         '#default_value' => $config->get('parser'),
135       ];
136     }
137     if (count($this->definitions['processor']) > 1) {
138       $basic_conf['aggregator_processors'] = [
139         '#type' => 'checkboxes',
140         '#title' => $this->t('Processors'),
141         '#description' => $this->t('Processors act on parsed feed data, for example they store feed items. Choose the processors suitable for your task.'),
142         '#options' => $this->definitions['processor'],
143         '#default_value' => $config->get('processors'),
144       ];
145     }
146     if (count($basic_conf)) {
147       $form['basic_conf'] = [
148         '#type' => 'details',
149         '#title' => $this->t('Basic configuration'),
150         '#description' => $this->t('For most aggregation tasks, the default settings are fine.'),
151         '#open' => TRUE,
152       ];
153       $form['basic_conf'] += $basic_conf;
154     }
155
156     // Call buildConfigurationForm() on the active fetcher and parser.
157     foreach (['fetcher', 'parser'] as $type) {
158       $active = $config->get($type);
159       if (array_key_exists($active, $this->definitions[$type])) {
160         $instance = $this->managers[$type]->createInstance($active);
161         if ($instance instanceof PluginFormInterface) {
162           $form = $instance->buildConfigurationForm($form, $form_state);
163           // Store the instance for validate and submit handlers.
164           // Keying by ID would bring conflicts, because two instances of a
165           // different type could have the same ID.
166           $this->configurableInstances[] = $instance;
167         }
168       }
169     }
170
171     // Implementing processor plugins will expect an array at $form['processors'].
172     $form['processors'] = [];
173     // Call buildConfigurationForm() for each active processor.
174     foreach ($this->definitions['processor'] as $id => $definition) {
175       if (in_array($id, $config->get('processors'))) {
176         $instance = $this->managers['processor']->createInstance($id);
177         if ($instance instanceof PluginFormInterface) {
178           $form = $instance->buildConfigurationForm($form, $form_state);
179           // Store the instance for validate and submit handlers.
180           // Keying by ID would bring conflicts, because two instances of a
181           // different type could have the same ID.
182           $this->configurableInstances[] = $instance;
183         }
184       }
185     }
186
187     return parent::buildForm($form, $form_state);
188   }
189
190   /**
191    * {@inheritdoc}
192    */
193   public function validateForm(array &$form, FormStateInterface $form_state) {
194     parent::validateForm($form, $form_state);
195     // Let active plugins validate their settings.
196     foreach ($this->configurableInstances as $instance) {
197       $instance->validateConfigurationForm($form, $form_state);
198     }
199   }
200
201   /**
202    * {@inheritdoc}
203    */
204   public function submitForm(array &$form, FormStateInterface $form_state) {
205     parent::submitForm($form, $form_state);
206     $config = $this->config('aggregator.settings');
207     // Let active plugins save their settings.
208     foreach ($this->configurableInstances as $instance) {
209       $instance->submitConfigurationForm($form, $form_state);
210     }
211
212     $config->set('items.allowed_html', $form_state->getValue('aggregator_allowed_html_tags'));
213     if ($form_state->hasValue('aggregator_fetcher')) {
214       $config->set('fetcher', $form_state->getValue('aggregator_fetcher'));
215     }
216     if ($form_state->hasValue('aggregator_parser')) {
217       $config->set('parser', $form_state->getValue('aggregator_parser'));
218     }
219     if ($form_state->hasValue('aggregator_processors')) {
220       $config->set('processors', array_filter($form_state->getValue('aggregator_processors')));
221     }
222     $config->save();
223   }
224
225 }