PageRenderTime 60ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/aggregator/lib/Drupal/aggregator/Form/SettingsForm.php

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