diff --git a/core/modules/user/lib/Drupal/user/AccountFormController.php b/core/modules/user/lib/Drupal/user/AccountFormController.php index 0672b1e..a897625 100644 --- a/core/modules/user/lib/Drupal/user/AccountFormController.php +++ b/core/modules/user/lib/Drupal/user/AccountFormController.php @@ -9,6 +9,7 @@ use Drupal\Core\Entity\EntityFormControllerNG; use Drupal\Core\Language\Language; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Form controller for the user account forms. @@ -16,6 +17,28 @@ abstract class AccountFormController extends EntityFormControllerNG { /** + * + * @var \Drupal\Core\Entity\Query\QueryInterface + */ + protected $entityQuery; + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('entity.query')->get('user') + ); + } + + /** + * {@inheritdoc} + */ + public function __construct(QueryInterface $entity_query) { + $this->entityQuery = $entity_query; + } + + /** * {@inheritdoc} */ public function form(array $form, array &$form_state) { @@ -258,7 +281,7 @@ public function validate(array $form, array &$form_state) { // Cast the user ID as an integer. It might have been set to NULL, which // could lead to unexpected results. else { - $name_taken = (bool) \Drupal::entityQuery('user') + $name_taken = (bool) $this->entityQuery ->condition('uid', (int) $account->id(), '<>') ->condition('name', db_like($form_state['values']['name']), 'LIKE') ->range(0, 1) @@ -274,7 +297,7 @@ public function validate(array $form, array &$form_state) { $mail = $form_state['values']['mail']; if (!empty($mail)) { - $mail_taken = (bool) \Drupal::entityQuery('user') + $mail_taken = (bool) $this->entityQuery ->condition('uid', (int) $account->id(), '<>') ->condition('mail', db_like($mail), 'LIKE') ->range(0, 1) diff --git a/core/modules/user/lib/Drupal/user/Plugin/Block/UserNewBlock.php b/core/modules/user/lib/Drupal/user/Plugin/Block/UserNewBlock.php index 8dbf988..daf4a8f 100644 --- a/core/modules/user/lib/Drupal/user/Plugin/Block/UserNewBlock.php +++ b/core/modules/user/lib/Drupal/user/Plugin/Block/UserNewBlock.php @@ -10,6 +10,8 @@ use Drupal\block\BlockBase; use Drupal\block\Annotation\Block; use Drupal\Core\Annotation\Translation; +use Drupal\Core\Plugin\ContainerFactoryPluginInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Provides a "Who's new" block. @@ -19,7 +21,33 @@ * admin_label = @Translation("Who's new") * ) */ -class UserNewBlock extends BlockBase { +class UserNewBlock extends BlockBase implements ContainerFactoryPluginInterface { + + /** + * + * @var \Drupal\Core\Entity\Query\QueryInterface + */ + protected $entityQuery; + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, array $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('entity.query')->get('user') + ); + } + + /** + * {@inheritdoc} + */ + public function __construct(array $configuration, $plugin_id, array $plugin_definition, QueryInterface $entity_query) { + parent::__construct($configuration, $plugin_id, $plugin_definition); + $this->entityQuery = $entity_query; + } /** * {@inheritdoc} @@ -65,7 +93,7 @@ public function blockSubmit($form, &$form_state) { */ public function build() { // Retrieve a list of new users who have accessed the site successfully. - $uids = \Drupal::entityQuery('user') + $uids = $this->entityQuery ->condition('status', 0, '<>') ->condition('access', 0, '<>') ->sort('created', 'DESC') diff --git a/core/modules/user/lib/Drupal/user/Plugin/Block/UserOnlineBlock.php b/core/modules/user/lib/Drupal/user/Plugin/Block/UserOnlineBlock.php index 4256610..978b29a 100644 --- a/core/modules/user/lib/Drupal/user/Plugin/Block/UserOnlineBlock.php +++ b/core/modules/user/lib/Drupal/user/Plugin/Block/UserOnlineBlock.php @@ -10,6 +10,9 @@ use Drupal\block\BlockBase; use Drupal\block\Annotation\Block; use Drupal\Core\Annotation\Translation; +use Drupal\Core\Entity\Query\QueryInterface; +use Drupal\Core\Plugin\ContainerFactoryPluginInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Provides a "Who's online" block. @@ -22,7 +25,33 @@ * admin_label = @Translation("Who's online") * ) */ -class UserOnlineBlock extends BlockBase { +class UserOnlineBlock extends BlockBase implements ContainerFactoryPluginInterface { + + /** + * + * @var \Drupal\Core\Entity\Query\QueryInterface + */ + protected $entityQuery; + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, array $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('entity.query')->get('user') + ); + } + + /** + * {@inheritdoc} + */ + public function __construct(array $configuration, $plugin_id, array $plugin_definition, QueryInterface $entity_query) { + parent::__construct($configuration, $plugin_id, $plugin_definition); + $this->entityQuery = $entity_query; + } /** * {@inheritdoc} @@ -95,7 +124,7 @@ public function build() { // Display a list of currently online users. $max_users = $this->configuration['max_list_count']; if ($authenticated_count && $max_users) { - $uids = \Drupal::entityQuery('user') + $uids = $this->entityQuery ->condition('access', $interval, '>=') ->condition('uid', 0, '>') ->sort('access', 'DESC') diff --git a/core/modules/user/lib/Drupal/user/ProfileFormController.php b/core/modules/user/lib/Drupal/user/ProfileFormController.php index ee4f8c3..d11fac6 100644 --- a/core/modules/user/lib/Drupal/user/ProfileFormController.php +++ b/core/modules/user/lib/Drupal/user/ProfileFormController.php @@ -19,6 +19,13 @@ class ProfileFormController extends AccountFormController { /** * {@inheritdoc} */ + public function __construct(QueryInterface $entity_query) { + parent::__construct($entity_query); + } + + /** + * {@inheritdoc} + */ protected function actions(array $form, array &$form_state) { $element = parent::actions($form, $form_state); diff --git a/core/modules/user/lib/Drupal/user/RegisterFormController.php b/core/modules/user/lib/Drupal/user/RegisterFormController.php index 6ace140..3bf2d45 100644 --- a/core/modules/user/lib/Drupal/user/RegisterFormController.php +++ b/core/modules/user/lib/Drupal/user/RegisterFormController.php @@ -7,6 +7,7 @@ namespace Drupal\user; +use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\RedirectResponse; /** @@ -15,6 +16,13 @@ class RegisterFormController extends AccountFormController { /** + * {@inheritdoc} + */ + public function __construct(QueryInterface $entity_query) { + parent::__construct($entity_query); + } + + /** * Overrides Drupal\Core\Entity\EntityFormController::form(). */ public function form(array $form, array &$form_state) { diff --git a/core/modules/user/lib/Drupal/user/UserAutocomplete.php b/core/modules/user/lib/Drupal/user/UserAutocomplete.php index c92710f..2e3e97e 100644 --- a/core/modules/user/lib/Drupal/user/UserAutocomplete.php +++ b/core/modules/user/lib/Drupal/user/UserAutocomplete.php @@ -9,6 +9,8 @@ use Drupal\Core\Config\ConfigFactory; use Drupal\Core\Database\Connection; +use Drupal\Core\Entity\EntityManager; +use Drupal\Core\Entity\Query\QueryFactory; use Drupal\Component\Utility\String; /** @@ -31,6 +33,18 @@ class UserAutocomplete { protected $configFactory; /** + * + * @var \Drupal\Core\Entity\Query\QueryInterface + */ + protected $entityQuery; + + /** + * + * @var \Drupal\Core\Entity\EntityManager + */ + protected $entityManager; + + /** * Constructs a UserAutocomplete object. * * @param \Drupal\Core\Database\Connection $connection @@ -38,9 +52,11 @@ class UserAutocomplete { * @param \Drupal\Core\Config\ConfigFactory $config_factory * The config factory. */ - public function __construct(Connection $connection, ConfigFactory $config_factory) { + public function __construct(Connection $connection, ConfigFactory $config_factory, QueryFactory $query_factory, EntityManager $entity_manager) { $this->connection = $connection; $this->configFactory = $config_factory; + $this->entityQuery = $query_factory->get('user'); + $this->entityManager = $entity_manager; } /** @@ -67,12 +83,12 @@ public function getMatches($string, $include_anonymous = FALSE) { } } - $uids = \Drupal::entityQuery('user') + $uids = $this->entityQuery ->condition('name', $string, 'STARTS_WITH') ->range(0, 10) ->execute(); - $controller = \Drupal::entityManager()->getStorageController('user'); + $controller = $this->entityManager->getStorageController('user'); foreach ($controller->loadMultiple($uids) as $account) { $matches[$account->getUsername()] = String::checkPlain($account->getUsername()); } diff --git a/core/modules/user/user.services.yml b/core/modules/user/user.services.yml index 6fb7d47..164471b 100644 --- a/core/modules/user/user.services.yml +++ b/core/modules/user/user.services.yml @@ -20,7 +20,7 @@ services: arguments: ['@database'] user.autocomplete: class: Drupal\user\UserAutocomplete - arguments: ['@database', '@config.factory'] + arguments: ['@database', '@config.factory', '@entity.query', '@entity.manager'] user_maintenance_mode_subscriber: class: Drupal\user\EventSubscriber\MaintenanceModeSubscriber tags: