ConfigFormBase.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace Drupal\Core\Form;
  3. use Drupal\Core\Config\ConfigFactoryInterface;
  4. use Symfony\Component\DependencyInjection\ContainerInterface;
  5. /**
  6. * Base class for implementing system configuration forms.
  7. */
  8. abstract class ConfigFormBase extends FormBase {
  9. use ConfigFormBaseTrait;
  10. /**
  11. * Constructs a \Drupal\system\ConfigFormBase object.
  12. *
  13. * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
  14. * The factory for configuration objects.
  15. */
  16. public function __construct(ConfigFactoryInterface $config_factory) {
  17. $this->setConfigFactory($config_factory);
  18. }
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public static function create(ContainerInterface $container) {
  23. return new static(
  24. $container->get('config.factory')
  25. );
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function buildForm(array $form, FormStateInterface $form_state) {
  31. $form['actions']['#type'] = 'actions';
  32. $form['actions']['submit'] = [
  33. '#type' => 'submit',
  34. '#value' => $this->t('Save configuration'),
  35. '#button_type' => 'primary',
  36. ];
  37. // By default, render the form using system-config-form.html.twig.
  38. $form['#theme'] = 'system_config_form';
  39. return $form;
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function submitForm(array &$form, FormStateInterface $form_state) {
  45. $this->messenger()->addStatus($this->t('The configuration options have been saved.'));
  46. }
  47. }