diff --git a/core/lib/Drupal/Core/Form/FormBase.php b/core/lib/Drupal/Core/Form/FormBase.php index cbdd0cc..4d42a56 100644 --- a/core/lib/Drupal/Core/Form/FormBase.php +++ b/core/lib/Drupal/Core/Form/FormBase.php @@ -7,6 +7,7 @@ namespace Drupal\Core\Form; +use Drupal\Core\Config\ConfigFactory; use Drupal\Core\DependencyInjection\ContainerInjectionInterface; use Drupal\Core\DependencyInjection\DependencySerialization; use Drupal\Core\Routing\UrlGeneratorInterface; @@ -41,6 +42,13 @@ protected $urlGenerator; /** + * The config factory. + * + * @var \Drupal\Core\Config\ConfigFactory + */ + protected $configFactory; + + /** * {@inheritdoc} */ public static function create(ContainerInterface $container) { @@ -90,6 +98,19 @@ protected function translationManager() { } /** + * Gets the config factory. + * + * @return \Drupal\Core\Config\ConfigFactory + * The config factory. + */ + protected function config() { + if (!$this->configFactory) { + $this->configFactory = $this->container()->get('config.factory'); + } + return $this->configFactory; + } + + /** * Sets the translation manager for this form. * * @param \Drupal\Core\StringTranslation\TranslationInterface $translation_manager @@ -104,6 +125,20 @@ public function setTranslationManager(TranslationInterface $translation_manager) } /** + * Sets the config factory for this form. + * + * @param \Drupal\Core\Config\ConfigFactory $config_factory + * The config factory. + * + * @return self + * The form. + */ + public function setConfigFactory(ConfigFactory $config_factory) { + $this->configFactory = $config_factory; + return $this; + } + + /** * Gets the request object. * * @return \Symfony\Component\HttpFoundation\Request $request diff --git a/core/modules/aggregator/lib/Drupal/aggregator/Form/AggregatorCategorizeFormBase.php b/core/modules/aggregator/lib/Drupal/aggregator/Form/AggregatorCategorizeFormBase.php index 74f4347..ca83881 100644 --- a/core/modules/aggregator/lib/Drupal/aggregator/Form/AggregatorCategorizeFormBase.php +++ b/core/modules/aggregator/lib/Drupal/aggregator/Form/AggregatorCategorizeFormBase.php @@ -10,7 +10,6 @@ use Drupal\aggregator\FeedInterface; use Drupal\aggregator\ItemStorageControllerInterface; use Drupal\Component\Utility\String; -use Drupal\Core\Config\Config; use Drupal\Core\Database\Connection; use Drupal\Core\Entity\EntityRenderControllerInterface; use Drupal\Core\Form\FormBase; @@ -63,15 +62,13 @@ * The item render controller. * @param \Drupal\Core\Database\Connection $database * The database connection. - * @param \Drupal\Core\Config\Config $config - * The aggregator config. * @param \Drupal\aggregator\ItemStorageControllerInterface $aggregator_item_storage * The aggregator item storage controller. */ - public function __construct(EntityRenderControllerInterface $aggregator_item_renderer, Connection $database, Config $config, ItemStorageControllerInterface $aggregator_item_storage) { + public function __construct(EntityRenderControllerInterface $aggregator_item_renderer, Connection $database, ItemStorageControllerInterface $aggregator_item_storage) { $this->aggregatorItemRenderer = $aggregator_item_renderer; $this->database = $database; - $this->config = $config; + $this->config = $this->config()->get('aggregator.settings'); $this->aggregatorItemStorage = $aggregator_item_storage; } @@ -82,7 +79,6 @@ public static function create(ContainerInterface $container) { return new static( $container->get('plugin.manager.entity')->getRenderController('aggregator_item'), $container->get('database'), - $container->get('config.factory')->get('aggregator.settings'), $container->get('plugin.manager.entity')->getStorageController('aggregator_item') ); } diff --git a/core/modules/config/lib/Drupal/config/Form/ConfigSync.php b/core/modules/config/lib/Drupal/config/Form/ConfigSync.php index 8f6f87b..2542df1 100644 --- a/core/modules/config/lib/Drupal/config/Form/ConfigSync.php +++ b/core/modules/config/lib/Drupal/config/Form/ConfigSync.php @@ -53,11 +53,6 @@ class ConfigSync extends FormBase { protected $eventDispatcher; /** - * @var \Drupal\Core\Config\ConfigFactory; - */ - protected $configFactory; - - /** * @var \Drupal\Core\Entity\EntityManager; */ protected $entity_manager; @@ -80,19 +75,16 @@ class ConfigSync extends FormBase { * The lock object. * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher * Event dispatcher. - * @param \Drupal\Core\Config\ConfigFactory $config_factory - * Configuration object factory. * @param \Drupal\Core\Entity\EntityManager $entity_manager * Entity manager. * @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator * The url generator service. */ - public function __construct(StorageInterface $sourceStorage, StorageInterface $targetStorage, LockBackendInterface $lock, EventDispatcherInterface $event_dispatcher, ConfigFactory $config_factory, EntityManager $entity_manager, UrlGeneratorInterface $url_generator) { + public function __construct(StorageInterface $sourceStorage, StorageInterface $targetStorage, LockBackendInterface $lock, EventDispatcherInterface $event_dispatcher, EntityManager $entity_manager, UrlGeneratorInterface $url_generator) { $this->sourceStorage = $sourceStorage; $this->targetStorage = $targetStorage; $this->lock = $lock; $this->eventDispatcher = $event_dispatcher; - $this->configFactory = $config_factory; $this->entity_manager = $entity_manager; $this->urlGenerator = $url_generator; } @@ -106,7 +98,6 @@ public static function create(ContainerInterface $container) { $container->get('config.storage'), $container->get('lock'), $container->get('event_dispatcher'), - $container->get('config.factory'), $container->get('entity.manager'), $container->get('url_generator') ); @@ -209,7 +200,7 @@ public function submitForm(array &$form, array &$form_state) { $config_importer = new ConfigImporter( $form_state['storage_comparer'], $this->eventDispatcher, - $this->configFactory, + $this->config(), $this->entity_manager, $this->lock ); diff --git a/core/modules/system/lib/Drupal/system/SystemConfigFormBase.php b/core/modules/system/lib/Drupal/system/SystemConfigFormBase.php index bceca12..47fa7b1 100644 --- a/core/modules/system/lib/Drupal/system/SystemConfigFormBase.php +++ b/core/modules/system/lib/Drupal/system/SystemConfigFormBase.php @@ -71,4 +71,15 @@ public function submitForm(array &$form, array &$form_state) { drupal_set_message($this->t('The configuration options have been saved.')); } + /** + * {@inheritdoc} + */ + protected function config() { + if (!$this->configFactory) { + $container = $this->container(); + $this->configFactory = $container->get('config.factory'); + $this->configFactory->enterContext($container->get('config.context.free')); + } + return $this->configFactory; + } } diff --git a/core/modules/system/tests/modules/form_test/lib/Drupal/form_test/FormTestArgumentsObject.php b/core/modules/system/tests/modules/form_test/lib/Drupal/form_test/FormTestArgumentsObject.php index 46c078b..3041d9f 100644 --- a/core/modules/system/tests/modules/form_test/lib/Drupal/form_test/FormTestArgumentsObject.php +++ b/core/modules/system/tests/modules/form_test/lib/Drupal/form_test/FormTestArgumentsObject.php @@ -54,7 +54,7 @@ public function validateForm(array &$form, array &$form_state) { */ public function submitForm(array &$form, array &$form_state) { drupal_set_message($this->t('The FormTestArgumentsObject::submitForm() method was used for this form.')); - \Drupal::config('form_test.object') + $this->config()->get('form_test.object') ->set('bananas', $form_state['values']['bananas']) ->save(); } diff --git a/core/modules/system/tests/modules/form_test/lib/Drupal/form_test/FormTestControllerObject.php b/core/modules/system/tests/modules/form_test/lib/Drupal/form_test/FormTestControllerObject.php index 05d2dd5..853f5e0 100644 --- a/core/modules/system/tests/modules/form_test/lib/Drupal/form_test/FormTestControllerObject.php +++ b/core/modules/system/tests/modules/form_test/lib/Drupal/form_test/FormTestControllerObject.php @@ -7,7 +7,6 @@ namespace Drupal\form_test; -use Drupal\Core\Config\ConfigFactory; use Drupal\Core\Form\FormBase; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -17,23 +16,6 @@ class FormTestControllerObject extends FormBase { /** - * The config factory. - * - * @var \Drupal\Core\Config\ConfigFactory - */ - protected $configFactory; - - /** - * Constructs a new FormTestControllerObject. - * - * @param \Drupal\Core\Config\ConfigFactory $config_factory - * The config factory. - */ - public function __construct(ConfigFactory $config_factory) { - $this->configFactory = $config_factory; - } - - /** * {@inheritdoc} */ public function getFormID() { @@ -45,9 +27,7 @@ public function getFormID() { */ public static function create(ContainerInterface $container) { drupal_set_message(t('The FormTestControllerObject::create() method was used for this form.')); - return new static( - $container->get('config.factory') - ); + return new static(); } /** @@ -84,7 +64,7 @@ public function validateForm(array &$form, array &$form_state) { */ public function submitForm(array &$form, array &$form_state) { drupal_set_message($this->t('The FormTestControllerObject::submitForm() method was used for this form.')); - $this->configFactory->get('form_test.object') + $this->config()->get('form_test.object') ->set('bananas', $form_state['values']['bananas']) ->save(); } diff --git a/core/modules/system/tests/modules/form_test/lib/Drupal/form_test/FormTestObject.php b/core/modules/system/tests/modules/form_test/lib/Drupal/form_test/FormTestObject.php index 4798b44..8d23a5e 100644 --- a/core/modules/system/tests/modules/form_test/lib/Drupal/form_test/FormTestObject.php +++ b/core/modules/system/tests/modules/form_test/lib/Drupal/form_test/FormTestObject.php @@ -55,7 +55,7 @@ public function validateForm(array &$form, array &$form_state) { */ public function submitForm(array &$form, array &$form_state) { drupal_set_message($this->t('The FormTestObject::submitForm() method was used for this form.')); - \Drupal::config('form_test.object') + $this->config()->get('form_test.object') ->set('bananas', $form_state['values']['bananas']) ->save(); } diff --git a/core/modules/system/tests/modules/form_test/lib/Drupal/form_test/FormTestServiceObject.php b/core/modules/system/tests/modules/form_test/lib/Drupal/form_test/FormTestServiceObject.php index a1495c5..d8bd2f6 100644 --- a/core/modules/system/tests/modules/form_test/lib/Drupal/form_test/FormTestServiceObject.php +++ b/core/modules/system/tests/modules/form_test/lib/Drupal/form_test/FormTestServiceObject.php @@ -53,7 +53,7 @@ public function validateForm(array &$form, array &$form_state) { */ public function submitForm(array &$form, array &$form_state) { drupal_set_message($this->t('The FormTestServiceObject::submitForm() method was used for this form.')); - \Drupal::config('form_test.object') + $this->config()->get('form_test.object') ->set('bananas', $form_state['values']['bananas']) ->save(); } diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Form/OverviewTerms.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Form/OverviewTerms.php index fb682bc..9664381 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Form/OverviewTerms.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Form/OverviewTerms.php @@ -8,7 +8,6 @@ namespace Drupal\taxonomy\Form; use Drupal\Core\Form\FormBase; -use Drupal\Core\Config\ConfigFactory; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\taxonomy\VocabularyInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -19,13 +18,6 @@ class OverviewTerms extends FormBase { /** - * Taxonomy config. - * - * @var \Drupal\Core\Config\Config - */ - protected $config; - - /** * The module handler service. * * @var \Drupal\Core\Extension\ModuleHandlerInterface @@ -35,13 +27,10 @@ class OverviewTerms extends FormBase { /** * Constructs an OverviewTerms object. * - * @param \Drupal\Core\Config\ConfigFactory $config_factory - * The config factory service. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler * The module handler service. */ - public function __construct(ConfigFactory $config_factory, ModuleHandlerInterface $module_handler) { - $this->config = $config_factory->get('taxonomy.settings'); + public function __construct(ModuleHandlerInterface $module_handler) { $this->moduleHandler = $module_handler; } @@ -50,7 +39,6 @@ public function __construct(ConfigFactory $config_factory, ModuleHandlerInterfac */ public static function create(ContainerInterface $container) { return new static( - $container->get('config.factory'), $container->get('module_handler') ); } @@ -87,7 +75,7 @@ public function buildForm(array $form, array &$form_state, VocabularyInterface $ $page = $this->getRequest()->query->get('page') ?: 0; // Number of terms per page. - $page_increment = $this->config->get('terms_per_page_admin'); + $page_increment = $this->config()->get('taxonomy.settings')->get('terms_per_page_admin'); // Elements shown on this page. $page_entries = 0; // Elements at the root level before this page. diff --git a/core/modules/user/lib/Drupal/user/Form/UserLoginForm.php b/core/modules/user/lib/Drupal/user/Form/UserLoginForm.php index f2a3940..890ee6b 100644 --- a/core/modules/user/lib/Drupal/user/Form/UserLoginForm.php +++ b/core/modules/user/lib/Drupal/user/Form/UserLoginForm.php @@ -7,7 +7,6 @@ namespace Drupal\user\Form; -use Drupal\Core\Config\ConfigFactory; use Drupal\Core\Flood\FloodInterface; use Drupal\Core\Form\FormBase; use Drupal\user\UserStorageControllerInterface; @@ -19,13 +18,6 @@ class UserLoginForm extends FormBase { /** - * The config factory. - * - * @var \Drupal\Core\Config\ConfigFactory - */ - protected $configFactory; - - /** * The flood service. * * @var \Drupal\Core\Flood\FloodInterface @@ -42,15 +34,12 @@ class UserLoginForm extends FormBase { /** * Constructs a new UserLoginForm. * - * @param \Drupal\Core\Config\ConfigFactory $config_factory - * The config factory. * @param \Drupal\Core\Flood\FloodInterface $flood * The flood service. * @param \Drupal\user\UserStorageControllerInterface $user_storage * The user storage controller. */ - public function __construct(ConfigFactory $config_factory, FloodInterface $flood, UserStorageControllerInterface $user_storage) { - $this->configFactory = $config_factory; + public function __construct(FloodInterface $flood, UserStorageControllerInterface $user_storage) { $this->flood = $flood; $this->userStorage = $user_storage; } @@ -60,7 +49,6 @@ public function __construct(ConfigFactory $config_factory, FloodInterface $flood */ public static function create(ContainerInterface $container) { return new static( - $container->get('config.factory'), $container->get('flood'), $container->get('entity.manager')->getStorageController('user') ); @@ -83,7 +71,7 @@ public function buildForm(array $form, array &$form_state) { '#title' => $this->t('Username'), '#size' => 60, '#maxlength' => USERNAME_MAX_LENGTH, - '#description' => $this->t('Enter your @s username.', array('@s' => $this->configFactory->get('system.site')->get('name'))), + '#description' => $this->t('Enter your @s username.', array('@s' => $this->config()->get('system.site')->get('name'))), '#required' => TRUE, '#attributes' => array( 'autocorrect' => 'off', @@ -138,7 +126,7 @@ public function validateName(array &$form, array &$form_state) { */ public function validateAuthentication(array &$form, array &$form_state) { $password = trim($form_state['values']['pass']); - $flood_config = $this->configFactory->get('user.flood'); + $flood_config = $this->config()->get('user.flood'); if (!empty($form_state['values']['name']) && !empty($password)) { // Do not allow any login from the current user's IP if the limit has been // reached. Default is 50 failed attempts allowed in one hour. This is @@ -184,7 +172,7 @@ public function validateAuthentication(array &$form, array &$form_state) { * This validation function should always be the last one. */ public function validateFinal(array &$form, array &$form_state) { - $flood_config = $this->configFactory->get('user.flood'); + $flood_config = $this->config()->get('user.flood'); if (empty($form_state['uid'])) { // Always register an IP-based failed login event. $this->flood->register('user.failed_login_ip', $flood_config->get('ip_window'));