123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- <?php
- namespace Drupal\system\Form;
- use Drupal\Core\Config\ConfigFactoryInterface;
- use Drupal\Core\Form\ConfigFormBase;
- use Drupal\Core\Form\FormStateInterface;
- use Drupal\Core\ImageToolkit\ImageToolkitManager;
- use Symfony\Component\DependencyInjection\ContainerInterface;
- /**
- * Configures image toolkit settings for this site.
- *
- * @internal
- */
- class ImageToolkitForm extends ConfigFormBase {
- /**
- * An array containing currently available toolkits.
- *
- * @var \Drupal\Core\ImageToolkit\ImageToolkitInterface[]
- */
- protected $availableToolkits = [];
- /**
- * Constructs a ImageToolkitForm object.
- *
- * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
- * The factory for configuration objects.
- * @param \Drupal\Core\ImageToolkit\ImageToolkitManager $manager
- * The image toolkit plugin manager.
- */
- public function __construct(ConfigFactoryInterface $config_factory, ImageToolkitManager $manager) {
- parent::__construct($config_factory);
- foreach ($manager->getAvailableToolkits() as $id => $definition) {
- $this->availableToolkits[$id] = $manager->createInstance($id);
- }
- }
- /**
- * {@inheritdoc}
- */
- public static function create(ContainerInterface $container) {
- return new static(
- $container->get('config.factory'),
- $container->get('image.toolkit.manager')
- );
- }
- /**
- * {@inheritdoc}
- */
- public function getFormId() {
- return 'system_image_toolkit_settings';
- }
- /**
- * {@inheritdoc}
- */
- protected function getEditableConfigNames() {
- return ['system.image'];
- }
- /**
- * {@inheritdoc}
- */
- public function buildForm(array $form, FormStateInterface $form_state) {
- $current_toolkit = $this->config('system.image')->get('toolkit');
- $form['image_toolkit'] = [
- '#type' => 'radios',
- '#title' => $this->t('Select an image processing toolkit'),
- '#default_value' => $current_toolkit,
- '#options' => [],
- ];
- // If we have more than one image toolkit, allow the user to select the one
- // to use, and load each of the toolkits' settings form.
- foreach ($this->availableToolkits as $id => $toolkit) {
- $definition = $toolkit->getPluginDefinition();
- $form['image_toolkit']['#options'][$id] = $definition['title'];
- $form['image_toolkit_settings'][$id] = [
- '#type' => 'details',
- '#title' => $this->t('@toolkit settings', ['@toolkit' => $definition['title']]),
- '#open' => TRUE,
- '#tree' => TRUE,
- '#states' => [
- 'visible' => [
- ':radio[name="image_toolkit"]' => ['value' => $id],
- ],
- ],
- ];
- $form['image_toolkit_settings'][$id] += $toolkit->buildConfigurationForm([], $form_state);
- }
- return parent::buildForm($form, $form_state);
- }
- /**
- * {@inheritdoc}
- */
- public function validateForm(array &$form, FormStateInterface $form_state) {
- parent::validateForm($form, $form_state);
- // Call the form validation handler for each of the toolkits.
- foreach ($this->availableToolkits as $toolkit) {
- $toolkit->validateConfigurationForm($form, $form_state);
- }
- }
- /**
- * {@inheritdoc}
- */
- public function submitForm(array &$form, FormStateInterface $form_state) {
- $this->config('system.image')
- ->set('toolkit', $form_state->getValue('image_toolkit'))
- ->save();
- // Call the form submit handler for each of the toolkits.
- foreach ($this->availableToolkits as $toolkit) {
- $toolkit->submitConfigurationForm($form, $form_state);
- }
- parent::submitForm($form, $form_state);
- }
- }
|