Commit 9b8d3239 authored by webchick's avatar webchick

Issue #2112711 by tim.plunkett: Provide an easier mechanism for using drupal_get_form() directly.

parent ff8080db
......@@ -124,6 +124,16 @@
* The unique string identifying the desired form.
*/
function _drupal_form_id($form_arg, &$form_state) {
// If the $form_arg is the name of a class, instantiate it.
if (is_string($form_arg) && class_exists($form_arg)) {
if (in_array('Drupal\Core\DependencyInjection\ContainerInjectionInterface', class_implements($form_arg))) {
$form_arg = $form_arg::create(\Drupal::getContainer());
}
else {
$form_arg = new $form_arg();
}
}
// If the $form_arg implements \Drupal\Core\Form\FormInterface, add that as
// the callback object and determine the form ID.
if (is_object($form_arg) && $form_arg instanceof FormInterface) {
......
......@@ -7,14 +7,12 @@
namespace Drupal\action;
use Drupal\Component\Utility\String;
use Drupal\Core\Action\ActionManager;
use Drupal\Core\Entity\EntityControllerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Config\Entity\ConfigEntityListController;
use Drupal\Core\Entity\EntityStorageControllerInterface;
use \Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\action\Form\ActionAdminManageForm;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
......@@ -124,7 +122,7 @@ public function render() {
if (!$this->hasConfigurableActions) {
unset($build['action_table']['#header']['operations']);
}
$build['action_admin_manage_form'] = drupal_get_form(new ActionAdminManageForm($this->actionManager));
$build['action_admin_manage_form'] = drupal_get_form('Drupal\action\Form\ActionAdminManageForm');
return $build;
}
......
......@@ -5,8 +5,6 @@
* Admin page callbacks for the Comment module.
*/
use Drupal\comment\Form\ConfirmDeleteMultiple;
/**
* Page callback: Presents an administrative comment listing.
*
......@@ -24,7 +22,7 @@ function comment_admin($type = 'new') {
$edit = $request->request->all();
if (isset($edit['operation']) && ($edit['operation'] == 'delete') && isset($edit['comments']) && $edit['comments']) {
return drupal_get_form(ConfirmDeleteMultiple::create(\Drupal::getContainer()), $request);
return drupal_get_form('Drupal\comment\Form\ConfirmDeleteMultiple');
}
else {
return drupal_get_form('comment_admin_overview', $type);
......
......@@ -13,7 +13,6 @@
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
/**
* Provides the comment multiple delete confirmation form.
......@@ -83,7 +82,7 @@ public function getConfirmText() {
/**
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state, Request $request = NULL) {
public function buildForm(array $form, array &$form_state) {
$edit = $form_state['input'];
$form['comments'] = array(
......
......@@ -7,8 +7,6 @@
namespace Drupal\locale\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\locale\Form\TranslateEditForm;
use Drupal\locale\Form\TranslateFilterForm;
use Symfony\Component\HttpFoundation\RedirectResponse;
/**
......@@ -51,8 +49,8 @@ public function checkTranslation() {
*/
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('Drupal\locale\Form\TranslateFilterForm'),
'form' => drupal_get_form('Drupal\locale\Form\TranslateEditForm'),
);
}
......
......@@ -7,21 +7,12 @@
namespace Drupal\search\Form;
use Drupal\Core\Form\FormInterface;
use Drupal\Core\Form\FormBase;
use Symfony\Component\HttpFoundation\Request;
/**
* Builds the search form for the search block.
*/
class SearchBlockForm extends FormBase implements FormInterface {
/**
* The current request.
*
* @var \Symfony\Component\HttpFoundation\Request
*/
protected $request;
class SearchBlockForm extends FormBase {
/**
* {@inheritdoc}
......@@ -33,10 +24,7 @@ public function getFormId() {
/**
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state, Request $request = NULL) {
// Save the request variable for use in the submit method.
$this->request = $request;
public function buildForm(array $form, array &$form_state) {
$form['search_block_form'] = array(
'#type' => 'search',
'#title' => $this->t('Search'),
......@@ -58,8 +46,9 @@ public function submitForm(array &$form, array &$form_state) {
// The search form relies on control of the redirect destination for its
// functionality, so we override any static destination set in the request.
// See http://drupal.org/node/292565.
if ($this->request->query->has('destination')) {
$this->request->query->remove('destination');
$request = $this->getRequest();
if ($request->query->has('destination')) {
$request->query->remove('destination');
}
// Check to see if the form was submitted empty.
......
......@@ -8,10 +8,6 @@
namespace Drupal\search\Plugin\Block;
use Drupal\block\BlockBase;
use Drupal\block\Annotation\Block;
use Drupal\Core\Annotation\Translation;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\search\Form\SearchBlockForm;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
......@@ -23,23 +19,7 @@
* admin_label = @Translation("Search form")
* )
*/
class SearchBlock extends BlockBase implements ContainerFactoryPluginInterface {
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, array $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container->get('request'));
}
/**
* Constructs a SearchBlock object.
*/
public function __construct(array $configuration, $plugin_id, array $plugin_definition, Request $request) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->request = $request;
}
class SearchBlock extends BlockBase {
/**
* Overrides \Drupal\block\BlockBase::access().
......@@ -52,7 +32,7 @@ public function access() {
* {@inheritdoc}
*/
public function build() {
// Pass the current request to drupal_get_form for use in the buildForm.
return drupal_get_form(new SearchBlockForm(), $this->request);
return drupal_get_form('Drupal\search\Form\SearchBlockForm');
}
}
......@@ -8,16 +8,13 @@
namespace Drupal\user\Controller;
use Drupal\Component\Utility\Xss;
use Drupal\user\Form\UserLoginForm;
use Drupal\Core\Controller\ControllerBase;
use Drupal\user\UserInterface;
use Symfony\Component\DependencyInjection\ContainerAware;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
/**
* Controller routines for user routes.
*/
class UserController extends ContainerAware {
class UserController extends ControllerBase {
/**
* Returns the user page.
......@@ -25,20 +22,17 @@ class UserController extends ContainerAware {
* Displays user profile if user is logged in, or login form for anonymous
* users.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The request object.
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse|array
* Returns either a redirect to the user page or the render
* array of the login form.
*/
public function userPage(Request $request) {
global $user;
public function userPage() {
$user = $this->currentUser();
if ($user->id()) {
$response = new RedirectResponse(url('user/' . $user->id(), array('absolute' => TRUE)));
$response = $this->redirect('user.view', array('user' => $user->id()));
}
else {
$response = drupal_get_form(UserLoginForm::create($this->container), $request);
$response = drupal_get_form('Drupal\user\Form\UserLoginForm');
}
return $response;
}
......@@ -59,15 +53,12 @@ public function userTitle(UserInterface $user = NULL) {
/**
* Logs the current user out.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The current request.
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse
* A redirection to home page.
*/
public function logout(Request $request) {
public function logout() {
user_logout();
return new RedirectResponse(url('<front>', array('absolute' => TRUE)));
return $this->redirect('<front>');
}
/**
......
......@@ -8,12 +8,6 @@
namespace Drupal\user\Plugin\Block;
use Drupal\block\BlockBase;
use Drupal\block\Annotation\Block;
use Drupal\Core\Annotation\Translation;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\user\Form\UserLoginForm;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
/**
* Provides a 'User login' block.
......@@ -23,55 +17,7 @@
* admin_label = @Translation("User login")
* )
*/
class UserLoginBlock extends BlockBase implements ContainerFactoryPluginInterface {
/**
* The DI Container.
*
* @var \Symfony\Component\DependencyInjection\ContainerInterface
*/
protected $container;
/**
* The request object.
*
* @var \Symfony\Component\HttpFoundation\Request
*/
protected $request;
/**
* Constructs a new UserLoginBlock.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin ID for the plugin instance.
* @param array $plugin_definition
* The plugin implementation definition.
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container
* The DI Container.
* @param \Symfony\Component\HttpFoundation\Request $request
* The request object.
*/
public function __construct(array $configuration, $plugin_id, array $plugin_definition, ContainerInterface $container, Request $request) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->container = $container;
$this->request = $request;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, array $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container,
$container->get('request')
);
}
class UserLoginBlock extends BlockBase {
/**
* Overrides \Drupal\block\BlockBase::access().
......@@ -84,7 +30,7 @@ public function access() {
* {@inheritdoc}
*/
public function build() {
$form = drupal_get_form(UserLoginForm::create($this->container), $this->request);
$form = drupal_get_form('Drupal\user\Form\UserLoginForm');
unset($form['name']['#attributes']['autofocus']);
unset($form['name']['#description']);
unset($form['pass']['#description']);
......
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