Issue #2077513 by tim.plunkett, jibran, ParisLiakos, msonnabaum: Refactor ControllerBase to be more consistent with FormBase.
parent
6f76e45a9e
commit
c4dd26c022
|
@ -7,7 +7,6 @@
|
|||
|
||||
namespace Drupal\Core\Controller;
|
||||
|
||||
use Symfony\Component\DependencyInjection\ContainerAware;
|
||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||
|
||||
/**
|
||||
|
@ -29,7 +28,70 @@ use Symfony\Component\HttpFoundation\RedirectResponse;
|
|||
*
|
||||
* @see \Drupal\Core\DependencyInjection\ContainerInjectionInterface
|
||||
*/
|
||||
abstract class ControllerBase extends ContainerAware {
|
||||
abstract class ControllerBase {
|
||||
|
||||
/**
|
||||
* The entity manager.
|
||||
*
|
||||
* @var \Drupal\Core\Entity\EntityManager
|
||||
*/
|
||||
protected $entityManager;
|
||||
|
||||
/**
|
||||
* The language manager.
|
||||
*
|
||||
* @var \Drupal\Core\Language\LanguageManager
|
||||
*/
|
||||
protected $languageManager;
|
||||
|
||||
/**
|
||||
* The translation manager.
|
||||
*
|
||||
* @var \Drupal\Core\StringTranslation\TranslationInterface
|
||||
*/
|
||||
protected $translationManager;
|
||||
|
||||
/**
|
||||
* The configuration factory.
|
||||
*
|
||||
* @var \Drupal\Core\Config\Config
|
||||
*/
|
||||
protected $configFactory;
|
||||
|
||||
/**
|
||||
* The key-value storage.
|
||||
*
|
||||
* @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface
|
||||
*/
|
||||
protected $keyValue;
|
||||
|
||||
/**
|
||||
* The url generator.
|
||||
*
|
||||
* @var \Drupal\Core\Routing\UrlGeneratorInterface
|
||||
*/
|
||||
protected $urlGenerator;
|
||||
|
||||
/**
|
||||
* The current user service.
|
||||
*
|
||||
* @var \Drupal\Core\Session\AccountInterface
|
||||
*/
|
||||
protected $currentUser;
|
||||
|
||||
/**
|
||||
* The state service.
|
||||
*
|
||||
* @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface
|
||||
*/
|
||||
protected $stateService;
|
||||
|
||||
/**
|
||||
* The module handler.
|
||||
*
|
||||
* @var \Drupal\Core\Extension\ModuleHandlerInterface
|
||||
*/
|
||||
protected $moduleHandler;
|
||||
|
||||
/**
|
||||
* Retrieves the entity manager service.
|
||||
|
@ -38,7 +100,10 @@ abstract class ControllerBase extends ContainerAware {
|
|||
* The entity manager service.
|
||||
*/
|
||||
protected function entityManager() {
|
||||
return $this->container->get('entity.manager');
|
||||
if (!$this->entityManager) {
|
||||
$this->entityManager = $this->container()->get('entity.manager');
|
||||
}
|
||||
return $this->entityManager;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -52,7 +117,7 @@ abstract class ControllerBase extends ContainerAware {
|
|||
* The cache object associated with the specified bin.
|
||||
*/
|
||||
protected function cache($bin = 'cache') {
|
||||
return $this->container->get('cache.' . $bin);
|
||||
return $this->container()->get('cache.' . $bin);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -72,7 +137,10 @@ abstract class ControllerBase extends ContainerAware {
|
|||
* A configuration object.
|
||||
*/
|
||||
protected function config($name) {
|
||||
return $this->container->get('config.factory')->get($name);
|
||||
if (!$this->configFactory) {
|
||||
$this->configFactory = $this->container()->get('config.factory')->get($name);
|
||||
}
|
||||
return $this->configFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -84,7 +152,10 @@ abstract class ControllerBase extends ContainerAware {
|
|||
* @return \Drupal\Core\KeyValueStore\KeyValueStoreInterface
|
||||
*/
|
||||
protected function keyValue($collection) {
|
||||
return $this->container->get('keyvalue')->get($collection);
|
||||
if (!$this->keyValue) {
|
||||
$this->keyValue = $this->container()->get('keyvalue')->get($collection);
|
||||
}
|
||||
return $this->keyValue;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -99,7 +170,10 @@ abstract class ControllerBase extends ContainerAware {
|
|||
* @return \Drupal\Core\KeyValueStore\KeyValueStoreInterface
|
||||
*/
|
||||
protected function state() {
|
||||
return $this->container->get('state');
|
||||
if (!$this->stateService) {
|
||||
$this->stateService = $this->container()->get('state');
|
||||
}
|
||||
return $this->stateService;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -108,7 +182,10 @@ abstract class ControllerBase extends ContainerAware {
|
|||
* @return \Drupal\Core\Extension\ModuleHandlerInterface
|
||||
*/
|
||||
protected function moduleHandler() {
|
||||
return $this->container->get('module_handler');
|
||||
if (!$this->moduleHandler) {
|
||||
$this->moduleHandler = $this->container()->get('module_handler');
|
||||
}
|
||||
return $this->moduleHandler;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -118,20 +195,23 @@ abstract class ControllerBase extends ContainerAware {
|
|||
* The URL generator service.
|
||||
*/
|
||||
protected function urlGenerator() {
|
||||
return $this->container->get('url_generator');
|
||||
if (!$this->urlGenerator) {
|
||||
$this->urlGenerator = $this->container()->get('url_generator');
|
||||
}
|
||||
return $this->urlGenerator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders a link to a route given a route name and its parameters.
|
||||
*
|
||||
* @see \Drupal\Core\Utility\LinkGeneratorInterface::generate() for details
|
||||
* @see \Drupal\Core\Utility\LinkGeneratorInterface::generate() for details
|
||||
* on the arguments, usage, and possible exceptions.
|
||||
*
|
||||
* @return string
|
||||
* An HTML string containing a link to the given route and parameters.
|
||||
*/
|
||||
public function l($text, $route_name, array $parameters = array(), array $options = array()) {
|
||||
return $this->container->get('link_generator')->generate($text, $route_name, $parameters, $options);
|
||||
return $this->container()->get('link_generator')->generate($text, $route_name, $parameters, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -141,7 +221,10 @@ abstract class ControllerBase extends ContainerAware {
|
|||
* The current user.
|
||||
*/
|
||||
protected function currentUser() {
|
||||
return $this->container->get('current_user');
|
||||
if (!$this->currentUser) {
|
||||
$this->currentUser = $this->container()->get('current_user');
|
||||
}
|
||||
return $this->currentUser;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -150,7 +233,20 @@ abstract class ControllerBase extends ContainerAware {
|
|||
* See the t() documentation for details.
|
||||
*/
|
||||
protected function t($string, array $args = array(), array $options = array()) {
|
||||
return $this->container->get('string_translation')->translate($string, $args, $options);
|
||||
return $this->translationManager()->translate($string, $args, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the translation manager.
|
||||
*
|
||||
* @return \Drupal\Core\StringTranslation\TranslationInterface
|
||||
* The translation manager.
|
||||
*/
|
||||
protected function translationManager() {
|
||||
if (!$this->translationManager) {
|
||||
$this->translationManager = $this->container()->get('string_translation');
|
||||
}
|
||||
return $this->translationManager;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -160,7 +256,20 @@ abstract class ControllerBase extends ContainerAware {
|
|||
* The language manager.
|
||||
*/
|
||||
protected function languageManager() {
|
||||
return $this->container->get('language_manager');
|
||||
if (!$this->languageManager) {
|
||||
$this->languageManager = $this->container()->get('language_manager');
|
||||
}
|
||||
return $this->languageManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the service container.
|
||||
*
|
||||
* @return \Symfony\Component\DependencyInjection\ContainerInterface $container
|
||||
* The service container.
|
||||
*/
|
||||
protected function container() {
|
||||
return \Drupal::getContainer();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -176,7 +285,7 @@ abstract class ControllerBase extends ContainerAware {
|
|||
* A redirect response object that may be returned by the controller.
|
||||
*/
|
||||
public function redirect($route_name, array $route_parameters = array(), $status = 302) {
|
||||
$url = $this->container->get('url_generator')->generate($route_name, $route_parameters, TRUE);
|
||||
$url = $this->urlGenerator()->generate($route_name, $route_parameters, TRUE);
|
||||
return new RedirectResponse($url, $status);
|
||||
}
|
||||
|
||||
|
@ -190,7 +299,7 @@ abstract class ControllerBase extends ContainerAware {
|
|||
* The generated URL for the given route.
|
||||
*/
|
||||
public function url($route_name, $route_parameters = array(), $options = array()) {
|
||||
return $this->container->get('url_generator')->generateFromRoute($route_name, $route_parameters, $options);
|
||||
return $this->urlGenerator()->generateFromRoute($route_name, $route_parameters, $options);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -219,7 +219,7 @@ class EntityListController implements EntityListControllerInterface, EntityContr
|
|||
* See the t() documentation for details.
|
||||
*/
|
||||
protected function t($string, array $args = array(), array $options = array()) {
|
||||
return $this->getTranslationManager()->translate($string, $args, $options);
|
||||
return $this->translationManager()->translate($string, $args, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -228,7 +228,7 @@ class EntityListController implements EntityListControllerInterface, EntityContr
|
|||
* @return \Drupal\Core\StringTranslation\TranslationInterface
|
||||
* The translation manager.
|
||||
*/
|
||||
protected function getTranslationManager() {
|
||||
protected function translationManager() {
|
||||
if (!$this->translationManager) {
|
||||
$this->translationManager = \Drupal::translation();
|
||||
}
|
||||
|
|
|
@ -60,7 +60,7 @@ abstract class FormBase extends DependencySerialization implements FormInterface
|
|||
* See the t() documentation for details.
|
||||
*/
|
||||
protected function t($string, array $args = array(), array $options = array()) {
|
||||
return $this->getTranslationManager()->translate($string, $args, $options);
|
||||
return $this->translationManager()->translate($string, $args, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -73,7 +73,7 @@ abstract class FormBase extends DependencySerialization implements FormInterface
|
|||
* The generated URL for the given route.
|
||||
*/
|
||||
public function url($route_name, $route_parameters = array(), $options = array()) {
|
||||
return $this->getUrlGenerator()->generateFromRoute($route_name, $route_parameters, $options);
|
||||
return $this->urlGenerator()->generateFromRoute($route_name, $route_parameters, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -82,9 +82,9 @@ abstract class FormBase extends DependencySerialization implements FormInterface
|
|||
* @return \Drupal\Core\StringTranslation\TranslationInterface
|
||||
* The translation manager.
|
||||
*/
|
||||
protected function getTranslationManager() {
|
||||
protected function translationManager() {
|
||||
if (!$this->translationManager) {
|
||||
$this->translationManager = \Drupal::translation();
|
||||
$this->translationManager = $this->container()->get('string_translation');
|
||||
}
|
||||
return $this->translationManager;
|
||||
}
|
||||
|
@ -111,7 +111,7 @@ abstract class FormBase extends DependencySerialization implements FormInterface
|
|||
*/
|
||||
protected function getRequest() {
|
||||
if (!$this->request) {
|
||||
$this->request = \Drupal::request();
|
||||
$this->request = $this->container()->get('request');
|
||||
}
|
||||
return $this->request;
|
||||
}
|
||||
|
@ -132,7 +132,7 @@ abstract class FormBase extends DependencySerialization implements FormInterface
|
|||
* @return \Drupal\Core\Session\AccountInterface
|
||||
* The current user.
|
||||
*/
|
||||
protected function getCurrentUser() {
|
||||
protected function currentUser() {
|
||||
return $this->getRequest()->attributes->get('_account');
|
||||
}
|
||||
|
||||
|
@ -142,7 +142,7 @@ abstract class FormBase extends DependencySerialization implements FormInterface
|
|||
* @return \Drupal\Core\Routing\UrlGeneratorInterface
|
||||
* The URL generator.
|
||||
*/
|
||||
protected function getUrlGenerator() {
|
||||
protected function urlGenerator() {
|
||||
if (!$this->urlGenerator) {
|
||||
$this->urlGenerator = \Drupal::urlGenerator();
|
||||
}
|
||||
|
@ -159,4 +159,14 @@ abstract class FormBase extends DependencySerialization implements FormInterface
|
|||
$this->urlGenerator = $url_generator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the service container.
|
||||
*
|
||||
* @return \Symfony\Component\DependencyInjection\ContainerInterface $container
|
||||
* The service container.
|
||||
*/
|
||||
protected function container() {
|
||||
return \Drupal::getContainer();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -139,7 +139,7 @@ class BlockFormController extends EntityFormController {
|
|||
// this entire visibility settings section probably needs a separate user
|
||||
// interface in the near future.
|
||||
$visibility = $entity->get('visibility');
|
||||
$access = $this->getCurrentUser()->hasPermission('use PHP for settings');
|
||||
$access = $this->currentUser()->hasPermission('use PHP for settings');
|
||||
if (!empty($visibility['path']['visibility']) && $visibility['path']['visibility'] == BLOCK_VISIBILITY_PHP && !$access) {
|
||||
$form['visibility']['path']['visibility'] = array(
|
||||
'#type' => 'value',
|
||||
|
|
|
@ -44,12 +44,10 @@ class ConfigController implements ContainerInjectionInterface {
|
|||
* {@inheritdoc}
|
||||
*/
|
||||
public static function create(ContainerInterface $container) {
|
||||
$file_download = new FileDownloadController();
|
||||
$file_download->setContainer($container);
|
||||
return new static(
|
||||
$container->get('config.storage'),
|
||||
$container->get('config.storage.staging'),
|
||||
$file_download
|
||||
new FileDownloadController()
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
@ -51,8 +51,8 @@ class LocaleController extends ControllerBase {
|
|||
*/
|
||||
public function translatePage() {
|
||||
return array(
|
||||
'filter' => drupal_get_form(TranslateFilterForm::create($this->container)),
|
||||
'form' => drupal_get_form(TranslateEditForm::create($this->container)),
|
||||
'filter' => drupal_get_form(TranslateFilterForm::create($this->container())),
|
||||
'form' => drupal_get_form(TranslateEditForm::create($this->container())),
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
@ -102,7 +102,7 @@ class ModulesListConfirmForm extends ConfirmFormBase implements ContainerInjecti
|
|||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildForm(array $form, array &$form_state) {
|
||||
$account = $this->getCurrentUser()->id();
|
||||
$account = $this->currentUser()->id();
|
||||
$this->modules = $this->keyValueExpirable->get($account);
|
||||
|
||||
// Redirect to the modules list page if the key value store is empty.
|
||||
|
@ -140,7 +140,7 @@ class ModulesListConfirmForm extends ConfirmFormBase implements ContainerInjecti
|
|||
*/
|
||||
public function submitForm(array &$form, array &$form_state) {
|
||||
// Remove the key value store entry.
|
||||
$account = $this->getCurrentUser()->id();
|
||||
$account = $this->currentUser()->id();
|
||||
$this->keyValueExpirable->delete($account);
|
||||
|
||||
// Gets list of modules prior to install process.
|
||||
|
|
|
@ -395,7 +395,7 @@ class ModulesListForm extends FormBase {
|
|||
// dependencies that are not enabled yet, redirect to the confirmation form.
|
||||
if (!empty($modules['dependencies']) || !empty($modules['missing'])) {
|
||||
// Write the list of changed module states into a key value store.
|
||||
$account = $this->getCurrentUser()->id();
|
||||
$account = $this->currentUser()->id();
|
||||
$this->keyValueExpirable->setWithExpire($account, $modules, 60);
|
||||
|
||||
// Redirect to the confirmation form.
|
||||
|
|
|
@ -103,7 +103,7 @@ class ModulesUninstallConfirmForm extends ConfirmFormBase implements ContainerIn
|
|||
*/
|
||||
public function buildForm(array $form, array &$form_state) {
|
||||
// Retrieve the list of modules from the key value store.
|
||||
$account = $this->getCurrentUser()->id();
|
||||
$account = $this->currentUser()->id();
|
||||
$this->modules = $this->keyValueExpirable->get($account);
|
||||
|
||||
// Prevent this page from showing when the module list is empty.
|
||||
|
@ -128,7 +128,7 @@ class ModulesUninstallConfirmForm extends ConfirmFormBase implements ContainerIn
|
|||
*/
|
||||
public function submitForm(array &$form, array &$form_state) {
|
||||
// Clear the key value store entry.
|
||||
$account = $this->getCurrentUser()->id();
|
||||
$account = $this->currentUser()->id();
|
||||
$this->keyValueExpirable->delete($account);
|
||||
|
||||
// Uninstall the modules.
|
||||
|
|
|
@ -140,7 +140,7 @@ class ModulesUninstallForm extends FormBase {
|
|||
// Save all the values in an expirable key value store.
|
||||
$modules = $form_state['values']['uninstall'];
|
||||
$uninstall = array_keys(array_filter($modules));
|
||||
$account = $this->getCurrentUser()->id();
|
||||
$account = $this->currentUser()->id();
|
||||
$this->keyValueExpirable->setWithExpire($account, $uninstall, 60);
|
||||
|
||||
// Redirect to the confirm form.
|
||||
|
|
|
@ -42,7 +42,7 @@ class ErrorTestController extends ControllerBase {
|
|||
*/
|
||||
public function triggerPDOException() {
|
||||
define('SIMPLETEST_COLLECT_ERRORS', FALSE);
|
||||
$this->container->get('database')->query('SELECT * FROM bananas_are_awesome');
|
||||
$this->container()->get('database')->query('SELECT * FROM bananas_are_awesome');
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -84,7 +84,7 @@ class UserPasswordForm extends FormBase {
|
|||
),
|
||||
);
|
||||
// Allow logged in users to request this also.
|
||||
$user = $this->getCurrentUser();
|
||||
$user = $this->currentUser();
|
||||
if ($user->isAuthenticated()) {
|
||||
$form['name']['#type'] = 'value';
|
||||
$form['name']['#value'] = $user->getEmail();
|
||||
|
|
|
@ -26,7 +26,7 @@ class ProfileFormController extends AccountFormController {
|
|||
$account = $this->entity;
|
||||
|
||||
// The user doing the editing.
|
||||
$user = $this->getCurrentUser();
|
||||
$user = $this->currentUser();
|
||||
$element['delete']['#type'] = 'submit';
|
||||
$element['delete']['#value'] = $this->t('Cancel account');
|
||||
$element['delete']['#submit'] = array(array($this, 'editCancelSubmit'));
|
||||
|
|
Loading…
Reference in New Issue