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.
17 class SettingsForm extends ConfigFormBase {
20 * The aggregator plugin managers.
22 * @var \Drupal\aggregator\Plugin\AggregatorPluginManager[]
24 protected $managers = [];
27 * The instantiated plugin instances that have configuration forms.
29 * @var \Drupal\Core\Plugin\PluginFormInterface[]
31 protected $configurableInstances = [];
34 * The aggregator plugin definitions.
38 protected $definitions = [
45 * Constructs a \Drupal\aggregator\SettingsForm object.
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.
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;
62 'fetcher' => $fetcher_manager,
63 'parser' => $parser_manager,
64 'processor' => $processor_manager,
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']]);
77 public static function create(ContainerInterface $container) {
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')
90 public function getFormId() {
91 return 'aggregator_admin_form';
97 protected function getEditableConfigNames() {
98 return ['aggregator.settings'];
104 public function buildForm(array $form, FormStateInterface $form_state) {
105 $config = $this->config('aggregator.settings');
107 // Global aggregator settings.
108 $form['aggregator_allowed_html_tags'] = [
109 '#type' => 'textfield',
110 '#title' => $this->t('Allowed HTML tags'),
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.'),
117 // Only show basic configuration if there are actually options.
119 if (count($this->definitions['fetcher']) > 1) {
120 $basic_conf['aggregator_fetcher'] = [
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'),
128 if (count($this->definitions['parser']) > 1) {
129 $basic_conf['aggregator_parser'] = [
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'),
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'),
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.'),
153 $form['basic_conf'] += $basic_conf;
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;
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;
187 return parent::buildForm($form, $form_state);
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);
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);
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'));
216 if ($form_state->hasValue('aggregator_parser')) {
217 $config->set('parser', $form_state->getValue('aggregator_parser'));
219 if ($form_state->hasValue('aggregator_processors')) {
220 $config->set('processors', array_filter($form_state->getValue('aggregator_processors')));