ImageToolkitForm.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace Drupal\system\Form;
  3. use Drupal\Core\Config\ConfigFactoryInterface;
  4. use Drupal\Core\Form\ConfigFormBase;
  5. use Drupal\Core\Form\FormStateInterface;
  6. use Drupal\Core\ImageToolkit\ImageToolkitManager;
  7. use Symfony\Component\DependencyInjection\ContainerInterface;
  8. /**
  9. * Configures image toolkit settings for this site.
  10. *
  11. * @internal
  12. */
  13. class ImageToolkitForm extends ConfigFormBase {
  14. /**
  15. * An array containing currently available toolkits.
  16. *
  17. * @var \Drupal\Core\ImageToolkit\ImageToolkitInterface[]
  18. */
  19. protected $availableToolkits = [];
  20. /**
  21. * Constructs a ImageToolkitForm object.
  22. *
  23. * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
  24. * The factory for configuration objects.
  25. * @param \Drupal\Core\ImageToolkit\ImageToolkitManager $manager
  26. * The image toolkit plugin manager.
  27. */
  28. public function __construct(ConfigFactoryInterface $config_factory, ImageToolkitManager $manager) {
  29. parent::__construct($config_factory);
  30. foreach ($manager->getAvailableToolkits() as $id => $definition) {
  31. $this->availableToolkits[$id] = $manager->createInstance($id);
  32. }
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public static function create(ContainerInterface $container) {
  38. return new static(
  39. $container->get('config.factory'),
  40. $container->get('image.toolkit.manager')
  41. );
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function getFormId() {
  47. return 'system_image_toolkit_settings';
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. protected function getEditableConfigNames() {
  53. return ['system.image'];
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function buildForm(array $form, FormStateInterface $form_state) {
  59. $current_toolkit = $this->config('system.image')->get('toolkit');
  60. $form['image_toolkit'] = [
  61. '#type' => 'radios',
  62. '#title' => $this->t('Select an image processing toolkit'),
  63. '#default_value' => $current_toolkit,
  64. '#options' => [],
  65. ];
  66. // If we have more than one image toolkit, allow the user to select the one
  67. // to use, and load each of the toolkits' settings form.
  68. foreach ($this->availableToolkits as $id => $toolkit) {
  69. $definition = $toolkit->getPluginDefinition();
  70. $form['image_toolkit']['#options'][$id] = $definition['title'];
  71. $form['image_toolkit_settings'][$id] = [
  72. '#type' => 'details',
  73. '#title' => $this->t('@toolkit settings', ['@toolkit' => $definition['title']]),
  74. '#open' => TRUE,
  75. '#tree' => TRUE,
  76. '#states' => [
  77. 'visible' => [
  78. ':radio[name="image_toolkit"]' => ['value' => $id],
  79. ],
  80. ],
  81. ];
  82. $form['image_toolkit_settings'][$id] += $toolkit->buildConfigurationForm([], $form_state);
  83. }
  84. return parent::buildForm($form, $form_state);
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. public function validateForm(array &$form, FormStateInterface $form_state) {
  90. parent::validateForm($form, $form_state);
  91. // Call the form validation handler for each of the toolkits.
  92. foreach ($this->availableToolkits as $toolkit) {
  93. $toolkit->validateConfigurationForm($form, $form_state);
  94. }
  95. }
  96. /**
  97. * {@inheritdoc}
  98. */
  99. public function submitForm(array &$form, FormStateInterface $form_state) {
  100. $this->config('system.image')
  101. ->set('toolkit', $form_state->getValue('image_toolkit'))
  102. ->save();
  103. // Call the form submit handler for each of the toolkits.
  104. foreach ($this->availableToolkits as $toolkit) {
  105. $toolkit->submitConfigurationForm($form, $form_state);
  106. }
  107. parent::submitForm($form, $form_state);
  108. }
  109. }