PageRenderTime 37ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/core/lib/Drupal/Core/Form/ConfigFormBaseTrait.php

http://github.com/drupal/drupal
PHP | 69 lines | 24 code | 6 blank | 39 comment | 5 complexity | acbf49eaafcf5013147f4aa84d991f21 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. namespace Drupal\Core\Form;
  3. use Drupal\Core\Config\ConfigFactoryInterface;
  4. /**
  5. * Provides access to configuration for forms.
  6. *
  7. * This trait provides a config() method that returns override free and mutable
  8. * config objects if the configuration name is in the array returned by the
  9. * getEditableConfigNames() implementation.
  10. *
  11. * Forms that present configuration to the user have to take care not to save
  12. * configuration overrides to the stored configuration since overrides are often
  13. * environment specific. Default values of form elements should be obtained from
  14. * override free configuration objects. However, if a form reacts to
  15. * configuration in any way, for example sends an email to the system.site:mail
  16. * address, then it is important that the value comes from a configuration
  17. * object with overrides. Therefore, override free and editable configuration
  18. * objects are limited to those listed by the getEditableConfigNames() method.
  19. */
  20. trait ConfigFormBaseTrait {
  21. /**
  22. * Retrieves a configuration object.
  23. *
  24. * @param string $name
  25. * The name of the configuration object to retrieve. The name corresponds to
  26. * a configuration file. For @code \Drupal::config('book.admin') @endcode,
  27. * the config object returned will contain the contents of book.admin
  28. * configuration file.
  29. *
  30. * @return \Drupal\Core\Config\Config|\Drupal\Core\Config\ImmutableConfig
  31. * An editable configuration object if the given name is listed in the
  32. * getEditableConfigNames() method or an immutable configuration object if
  33. * not.
  34. */
  35. protected function config($name) {
  36. /** @var \Drupal\Core\Config\ConfigFactoryInterface $config_factory */
  37. if (method_exists($this, 'configFactory')) {
  38. $config_factory = $this->configFactory();
  39. }
  40. elseif (property_exists($this, 'configFactory')) {
  41. $config_factory = $this->configFactory;
  42. }
  43. if (!isset($config_factory) || !($config_factory instanceof ConfigFactoryInterface)) {
  44. throw new \LogicException('No config factory available for ConfigFormBaseTrait');
  45. }
  46. if (in_array($name, $this->getEditableConfigNames())) {
  47. // Get a mutable object from the factory.
  48. $config = $config_factory->getEditable($name);
  49. }
  50. else {
  51. $config = $config_factory->get($name);
  52. }
  53. return $config;
  54. }
  55. /**
  56. * Gets the configuration names that will be editable.
  57. *
  58. * @return array
  59. * An array of configuration object names that are editable if called in
  60. * conjunction with the trait's config() method.
  61. */
  62. abstract protected function getEditableConfigNames();
  63. }