Commit 5e9ac4dd authored by webchick's avatar webchick

Issue #2087279 by larowlan, dawehner, disasm, tim.plunkett: Add a config() method to FormBase.

parent 874690a3
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
namespace Drupal\Core\Form; namespace Drupal\Core\Form;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface; use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\DependencyInjection\DependencySerialization; use Drupal\Core\DependencyInjection\DependencySerialization;
use Drupal\Core\Routing\UrlGeneratorInterface; use Drupal\Core\Routing\UrlGeneratorInterface;
...@@ -40,6 +41,13 @@ abstract class FormBase extends DependencySerialization implements FormInterface ...@@ -40,6 +41,13 @@ abstract class FormBase extends DependencySerialization implements FormInterface
*/ */
protected $urlGenerator; protected $urlGenerator;
/**
* The config factory.
*
* @var \Drupal\Core\Config\ConfigFactory
*/
protected $configFactory;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
...@@ -89,6 +97,29 @@ protected function translationManager() { ...@@ -89,6 +97,29 @@ protected function translationManager() {
return $this->translationManager; return $this->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. * Sets the translation manager for this form.
* *
...@@ -103,6 +134,20 @@ public function setTranslationManager(TranslationInterface $translation_manager) ...@@ -103,6 +134,20 @@ public function setTranslationManager(TranslationInterface $translation_manager)
return $this; return $this;
} }
/**
* 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. * Gets the request object.
* *
......
...@@ -10,7 +10,6 @@ ...@@ -10,7 +10,6 @@
use Drupal\aggregator\FeedInterface; use Drupal\aggregator\FeedInterface;
use Drupal\aggregator\ItemStorageControllerInterface; use Drupal\aggregator\ItemStorageControllerInterface;
use Drupal\Component\Utility\String; use Drupal\Component\Utility\String;
use Drupal\Core\Config\Config;
use Drupal\Core\Database\Connection; use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityRenderControllerInterface; use Drupal\Core\Entity\EntityRenderControllerInterface;
use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormBase;
...@@ -63,15 +62,13 @@ abstract class AggregatorCategorizeFormBase extends FormBase { ...@@ -63,15 +62,13 @@ abstract class AggregatorCategorizeFormBase extends FormBase {
* The item render controller. * The item render controller.
* @param \Drupal\Core\Database\Connection $database * @param \Drupal\Core\Database\Connection $database
* The database connection. * The database connection.
* @param \Drupal\Core\Config\Config $config
* The aggregator config.
* @param \Drupal\aggregator\ItemStorageControllerInterface $aggregator_item_storage * @param \Drupal\aggregator\ItemStorageControllerInterface $aggregator_item_storage
* The aggregator item storage controller. * 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->aggregatorItemRenderer = $aggregator_item_renderer;
$this->database = $database; $this->database = $database;
$this->config = $config; $this->config = $this->config('aggregator.settings');
$this->aggregatorItemStorage = $aggregator_item_storage; $this->aggregatorItemStorage = $aggregator_item_storage;
} }
...@@ -82,7 +79,6 @@ public static function create(ContainerInterface $container) { ...@@ -82,7 +79,6 @@ public static function create(ContainerInterface $container) {
return new static( return new static(
$container->get('plugin.manager.entity')->getRenderController('aggregator_item'), $container->get('plugin.manager.entity')->getRenderController('aggregator_item'),
$container->get('database'), $container->get('database'),
$container->get('config.factory')->get('aggregator.settings'),
$container->get('plugin.manager.entity')->getStorageController('aggregator_item') $container->get('plugin.manager.entity')->getStorageController('aggregator_item')
); );
} }
......
...@@ -52,11 +52,6 @@ class ConfigSync extends FormBase { ...@@ -52,11 +52,6 @@ class ConfigSync extends FormBase {
*/ */
protected $eventDispatcher; protected $eventDispatcher;
/**
* @var \Drupal\Core\Config\ConfigFactory;
*/
protected $configFactory;
/** /**
* @var \Drupal\Core\Entity\EntityManager; * @var \Drupal\Core\Entity\EntityManager;
*/ */
...@@ -81,7 +76,7 @@ class ConfigSync extends FormBase { ...@@ -81,7 +76,7 @@ class ConfigSync extends FormBase {
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
* Event dispatcher. * Event dispatcher.
* @param \Drupal\Core\Config\ConfigFactory $config_factory * @param \Drupal\Core\Config\ConfigFactory $config_factory
* Configuration object factory. * The config factory.
* @param \Drupal\Core\Entity\EntityManager $entity_manager * @param \Drupal\Core\Entity\EntityManager $entity_manager
* Entity manager. * Entity manager.
* @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator * @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator
......
...@@ -29,15 +29,13 @@ class Overview extends OverviewTerms { ...@@ -29,15 +29,13 @@ class Overview extends OverviewTerms {
/** /**
* Constructs a \Drupal\forum\Form\OverviewForm object. * 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 * @param \Drupal\Core\Entity\EntityManager $entity_manager
* The entity manager service. * The entity manager service.
* @param \Drupal\Core\Extension\ModuleHandlerInteface $module_handler * @param \Drupal\Core\Extension\ModuleHandlerInteface $module_handler
* The module handler service. * The module handler service.
*/ */
public function __construct(ConfigFactory $config_factory, EntityManager $entity_manager, ModuleHandlerInterface $module_handler) { public function __construct(EntityManager $entity_manager, ModuleHandlerInterface $module_handler) {
parent::__construct($config_factory, $module_handler); parent::__construct($module_handler);
$this->entityManager = $entity_manager; $this->entityManager = $entity_manager;
} }
...@@ -46,7 +44,6 @@ public function __construct(ConfigFactory $config_factory, EntityManager $entity ...@@ -46,7 +44,6 @@ public function __construct(ConfigFactory $config_factory, EntityManager $entity
*/ */
public static function create(ContainerInterface $container) { public static function create(ContainerInterface $container) {
return new static( return new static(
$container->get('config.factory'),
$container->get('entity.manager'), $container->get('entity.manager'),
$container->get('module_handler') $container->get('module_handler')
); );
...@@ -63,7 +60,7 @@ public function getFormID() { ...@@ -63,7 +60,7 @@ public function getFormID() {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function buildForm(array $form, array &$form_state) { 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'); $vid = $forum_config->get('vocabulary');
$vocabulary = $this->entityManager->getStorageController('taxonomy_vocabulary')->load($vid); $vocabulary = $this->entityManager->getStorageController('taxonomy_vocabulary')->load($vid);
if (!$vocabulary) { if (!$vocabulary) {
......
...@@ -71,4 +71,15 @@ public function submitForm(array &$form, array &$form_state) { ...@@ -71,4 +71,15 @@ public function submitForm(array &$form, array &$form_state) {
drupal_set_message($this->t('The configuration options have been saved.')); drupal_set_message($this->t('The configuration options have been saved.'));
} }
/**
* {@inheritdoc}
*/
protected function config($name) {
if (!$this->configFactory) {
$container = $this->container();
$this->configFactory = $container->get('config.factory');
$this->configFactory->enterContext($container->get('config.context.free'));
}
return $this->configFactory->get($name);
}
} }
...@@ -54,7 +54,7 @@ public function validateForm(array &$form, array &$form_state) { ...@@ -54,7 +54,7 @@ public function validateForm(array &$form, array &$form_state) {
*/ */
public function submitForm(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_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']) ->set('bananas', $form_state['values']['bananas'])
->save(); ->save();
} }
......
...@@ -7,7 +7,6 @@ ...@@ -7,7 +7,6 @@
namespace Drupal\form_test; namespace Drupal\form_test;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormBase;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
...@@ -16,23 +15,6 @@ ...@@ -16,23 +15,6 @@
*/ */
class FormTestControllerObject extends FormBase { 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} * {@inheritdoc}
*/ */
...@@ -45,9 +27,7 @@ public function getFormID() { ...@@ -45,9 +27,7 @@ public function getFormID() {
*/ */
public static function create(ContainerInterface $container) { public static function create(ContainerInterface $container) {
drupal_set_message(t('The FormTestControllerObject::create() method was used for this form.')); drupal_set_message(t('The FormTestControllerObject::create() method was used for this form.'));
return new static( return new static();
$container->get('config.factory')
);
} }
/** /**
...@@ -84,7 +64,7 @@ public function validateForm(array &$form, array &$form_state) { ...@@ -84,7 +64,7 @@ public function validateForm(array &$form, array &$form_state) {
*/ */
public function submitForm(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.')); 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']) ->set('bananas', $form_state['values']['bananas'])
->save(); ->save();
} }
......
...@@ -55,7 +55,7 @@ public function validateForm(array &$form, array &$form_state) { ...@@ -55,7 +55,7 @@ public function validateForm(array &$form, array &$form_state) {
*/ */
public function submitForm(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_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']) ->set('bananas', $form_state['values']['bananas'])
->save(); ->save();
} }
......
...@@ -53,7 +53,7 @@ public function validateForm(array &$form, array &$form_state) { ...@@ -53,7 +53,7 @@ public function validateForm(array &$form, array &$form_state) {
*/ */
public function submitForm(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_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']) ->set('bananas', $form_state['values']['bananas'])
->save(); ->save();
} }
......
...@@ -8,7 +8,6 @@ ...@@ -8,7 +8,6 @@
namespace Drupal\taxonomy\Form; namespace Drupal\taxonomy\Form;
use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormBase;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\taxonomy\VocabularyInterface; use Drupal\taxonomy\VocabularyInterface;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
...@@ -18,13 +17,6 @@ ...@@ -18,13 +17,6 @@
*/ */
class OverviewTerms extends FormBase { class OverviewTerms extends FormBase {
/**
* Taxonomy config.
*
* @var \Drupal\Core\Config\Config
*/
protected $configFactory;
/** /**
* The module handler service. * The module handler service.
* *
...@@ -35,13 +27,10 @@ class OverviewTerms extends FormBase { ...@@ -35,13 +27,10 @@ class OverviewTerms extends FormBase {
/** /**
* Constructs an OverviewTerms object. * Constructs an OverviewTerms object.
* *
* @param \Drupal\Core\Config\ConfigFactory $config_factory
* The config factory service.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler service. * The module handler service.
*/ */
public function __construct(ConfigFactory $config_factory, ModuleHandlerInterface $module_handler) { public function __construct(ModuleHandlerInterface $module_handler) {
$this->configFactory = $config_factory;
$this->moduleHandler = $module_handler; $this->moduleHandler = $module_handler;
} }
...@@ -50,7 +39,6 @@ public function __construct(ConfigFactory $config_factory, ModuleHandlerInterfac ...@@ -50,7 +39,6 @@ public function __construct(ConfigFactory $config_factory, ModuleHandlerInterfac
*/ */
public static function create(ContainerInterface $container) { public static function create(ContainerInterface $container) {
return new static( return new static(
$container->get('config.factory'),
$container->get('module_handler') $container->get('module_handler')
); );
} }
...@@ -87,7 +75,7 @@ public function buildForm(array $form, array &$form_state, VocabularyInterface $ ...@@ -87,7 +75,7 @@ public function buildForm(array $form, array &$form_state, VocabularyInterface $
$page = $this->getRequest()->query->get('page') ?: 0; $page = $this->getRequest()->query->get('page') ?: 0;
// Number of terms per page. // 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. // Elements shown on this page.
$page_entries = 0; $page_entries = 0;
// Elements at the root level before this page. // Elements at the root level before this page.
......
...@@ -7,7 +7,6 @@ ...@@ -7,7 +7,6 @@
namespace Drupal\user\Form; namespace Drupal\user\Form;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Flood\FloodInterface; use Drupal\Core\Flood\FloodInterface;
use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormBase;
use Drupal\user\UserStorageControllerInterface; use Drupal\user\UserStorageControllerInterface;
...@@ -18,13 +17,6 @@ ...@@ -18,13 +17,6 @@
*/ */
class UserLoginForm extends FormBase { class UserLoginForm extends FormBase {
/**
* The config factory.
*
* @var \Drupal\Core\Config\ConfigFactory
*/
protected $configFactory;
/** /**
* The flood service. * The flood service.
* *
...@@ -42,15 +34,12 @@ class UserLoginForm extends FormBase { ...@@ -42,15 +34,12 @@ class UserLoginForm extends FormBase {
/** /**
* Constructs a new UserLoginForm. * Constructs a new UserLoginForm.
* *
* @param \Drupal\Core\Config\ConfigFactory $config_factory
* The config factory.
* @param \Drupal\Core\Flood\FloodInterface $flood * @param \Drupal\Core\Flood\FloodInterface $flood
* The flood service. * The flood service.
* @param \Drupal\user\UserStorageControllerInterface $user_storage * @param \Drupal\user\UserStorageControllerInterface $user_storage
* The user storage controller. * The user storage controller.
*/ */
public function __construct(ConfigFactory $config_factory, FloodInterface $flood, UserStorageControllerInterface $user_storage) { public function __construct(FloodInterface $flood, UserStorageControllerInterface $user_storage) {
$this->configFactory = $config_factory;
$this->flood = $flood; $this->flood = $flood;
$this->userStorage = $user_storage; $this->userStorage = $user_storage;
} }
...@@ -60,7 +49,6 @@ public function __construct(ConfigFactory $config_factory, FloodInterface $flood ...@@ -60,7 +49,6 @@ public function __construct(ConfigFactory $config_factory, FloodInterface $flood
*/ */
public static function create(ContainerInterface $container) { public static function create(ContainerInterface $container) {
return new static( return new static(
$container->get('config.factory'),
$container->get('flood'), $container->get('flood'),
$container->get('entity.manager')->getStorageController('user') $container->get('entity.manager')->getStorageController('user')
); );
...@@ -83,7 +71,7 @@ public function buildForm(array $form, array &$form_state) { ...@@ -83,7 +71,7 @@ public function buildForm(array $form, array &$form_state) {
'#title' => $this->t('Username'), '#title' => $this->t('Username'),
'#size' => 60, '#size' => 60,
'#maxlength' => USERNAME_MAX_LENGTH, '#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, '#required' => TRUE,
'#attributes' => array( '#attributes' => array(
'autocorrect' => 'off', 'autocorrect' => 'off',
...@@ -138,7 +126,7 @@ public function validateName(array &$form, array &$form_state) { ...@@ -138,7 +126,7 @@ public function validateName(array &$form, array &$form_state) {
*/ */
public function validateAuthentication(array &$form, array &$form_state) { public function validateAuthentication(array &$form, array &$form_state) {
$password = trim($form_state['values']['pass']); $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)) { if (!empty($form_state['values']['name']) && !empty($password)) {
// Do not allow any login from the current user's IP if the limit has been // 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 // reached. Default is 50 failed attempts allowed in one hour. This is
...@@ -184,7 +172,7 @@ public function validateAuthentication(array &$form, array &$form_state) { ...@@ -184,7 +172,7 @@ public function validateAuthentication(array &$form, array &$form_state) {
* This validation function should always be the last one. * This validation function should always be the last one.
*/ */
public function validateFinal(array &$form, array &$form_state) { 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'])) { if (empty($form_state['uid'])) {
// Always register an IP-based failed login event. // Always register an IP-based failed login event.
$this->flood->register('user.failed_login_ip', $flood_config->get('ip_window')); $this->flood->register('user.failed_login_ip', $flood_config->get('ip_window'));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment