diff --git c/core/lib/Drupal/Core/Form/FormBase.php w/core/lib/Drupal/Core/Form/FormBase.php index cbdd0cc..2fad298 100644 --- c/core/lib/Drupal/Core/Form/FormBase.php +++ w/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,29 @@ protected function translationManager() { } /** + * Retrieves a configuration object. + * + * This is the main entry point to the configuration API. Calling + * @code $this->config('book.admin') @endcode will return a configuration + * object in which the book module can store its administrative settings. + * + * @param string $name + * The name of the configuration object to retrieve. The name corresponds to + * a configuration file. For @code \Drupal::config('book.admin') @endcode, + * the config object returned will contain the contents of book.admin + * configuration file. + * + * @return \Drupal\Core\Config\Config + * A configuration object. + */ + protected function config($name) { + if (!$this->configFactory) { + $this->configFactory = $this->container()->get('config.factory'); + } + return $this->configFactory->get($name); + } + + /** * Sets the translation manager for this form. * * @param \Drupal\Core\StringTranslation\TranslationInterface $translation_manager @@ -104,6 +135,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 c/core/modules/aggregator/lib/Drupal/aggregator/Form/AggregatorCategorizeFormBase.php w/core/modules/aggregator/lib/Drupal/aggregator/Form/AggregatorCategorizeFormBase.php index 74f4347..794dcce 100644 --- c/core/modules/aggregator/lib/Drupal/aggregator/Form/AggregatorCategorizeFormBase.php +++ w/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('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 c/core/modules/config/lib/Drupal/config/Form/ConfigSync.php w/core/modules/config/lib/Drupal/config/Form/ConfigSync.php index 61160b8..7876ef6 100644 --- c/core/modules/config/lib/Drupal/config/Form/ConfigSync.php +++ w/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; @@ -81,7 +76,7 @@ class ConfigSync extends FormBase { * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher * Event dispatcher. * @param \Drupal\Core\Config\ConfigFactory $config_factory - * Configuration object factory. + * The config factory. * @param \Drupal\Core\Entity\EntityManager $entity_manager * Entity manager. * @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator diff --git c/core/modules/forum/lib/Drupal/forum/Form/Overview.php w/core/modules/forum/lib/Drupal/forum/Form/Overview.php index 968d259..47b2140 100644 --- c/core/modules/forum/lib/Drupal/forum/Form/Overview.php +++ w/core/modules/forum/lib/Drupal/forum/Form/Overview.php @@ -29,15 +29,13 @@ class Overview extends OverviewTerms { /** * Constructs a \Drupal\forum\Form\OverviewForm object. * - * @param \Drupal\Core\Config\ConfigFactory $config_factory - * The factory for configuration objects. * @param \Drupal\Core\Entity\EntityManager $entity_manager * The entity manager service. * @param \Drupal\Core\Extension\ModuleHandlerInteface $module_handler * The module handler service. */ - public function __construct(ConfigFactory $config_factory, EntityManager $entity_manager, ModuleHandlerInterface $module_handler) { - parent::__construct($config_factory, $module_handler); + public function __construct(EntityManager $entity_manager, ModuleHandlerInterface $module_handler) { + parent::__construct($module_handler); $this->entityManager = $entity_manager; } @@ -46,7 +44,6 @@ public function __construct(ConfigFactory $config_factory, EntityManager $entity */ public static function create(ContainerInterface $container) { return new static( - $container->get('config.factory'), $container->get('entity.manager'), $container->get('module_handler') ); @@ -63,7 +60,7 @@ public function getFormID() { * {@inheritdoc} */ public function buildForm(array $form, array &$form_state) { - $forum_config = $this->configFactory->get('forum.settings'); + $forum_config = $this->config('forum.settings'); $vid = $forum_config->get('vocabulary'); $vocabulary = $this->entityManager->getStorageController('taxonomy_vocabulary')->load($vid); if (!$vocabulary) { diff --git c/core/modules/system/tests/modules/form_test/lib/Drupal/form_test/FormTestArgumentsObject.php w/core/modules/system/tests/modules/form_test/lib/Drupal/form_test/FormTestArgumentsObject.php index 46c078b..e07479a 100644 --- c/core/modules/system/tests/modules/form_test/lib/Drupal/form_test/FormTestArgumentsObject.php +++ w/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('form_test.object') ->set('bananas', $form_state['values']['bananas']) ->save(); } diff --git c/core/modules/system/tests/modules/form_test/lib/Drupal/form_test/FormTestControllerObject.php w/core/modules/system/tests/modules/form_test/lib/Drupal/form_test/FormTestControllerObject.php index 05d2dd5..e0d0d13 100644 --- c/core/modules/system/tests/modules/form_test/lib/Drupal/form_test/FormTestControllerObject.php +++ w/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('form_test.object') ->set('bananas', $form_state['values']['bananas']) ->save(); } diff --git c/core/modules/system/tests/modules/form_test/lib/Drupal/form_test/FormTestObject.php w/core/modules/system/tests/modules/form_test/lib/Drupal/form_test/FormTestObject.php index 4798b44..e431ba5 100644 --- c/core/modules/system/tests/modules/form_test/lib/Drupal/form_test/FormTestObject.php +++ w/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('form_test.object') ->set('bananas', $form_state['values']['bananas']) ->save(); } diff --git c/core/modules/system/tests/modules/form_test/lib/Drupal/form_test/FormTestServiceObject.php w/core/modules/system/tests/modules/form_test/lib/Drupal/form_test/FormTestServiceObject.php index a1495c5..2539de9 100644 --- c/core/modules/system/tests/modules/form_test/lib/Drupal/form_test/FormTestServiceObject.php +++ w/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('form_test.object') ->set('bananas', $form_state['values']['bananas']) ->save(); } diff --git c/core/modules/taxonomy/lib/Drupal/taxonomy/Form/OverviewTerms.php w/core/modules/taxonomy/lib/Drupal/taxonomy/Form/OverviewTerms.php index d7959dd..3852ea8 100644 --- c/core/modules/taxonomy/lib/Drupal/taxonomy/Form/OverviewTerms.php +++ w/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 $configFactory; - - /** * 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->configFactory = $config_factory; + 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->configFactory->get('taxonomy.settings')->get('terms_per_page_admin'); + $page_increment = $this->config('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 c/core/modules/user/lib/Drupal/user/Form/UserLoginForm.php w/core/modules/user/lib/Drupal/user/Form/UserLoginForm.php index f2a3940..6ae96bc 100644 --- c/core/modules/user/lib/Drupal/user/Form/UserLoginForm.php +++ w/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('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('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('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'));