Skip to main content
94 votes
Accepted

How to get $_POST and $_GET parameters

From the change record: $name = $_POST['name']; // form param becomes $name = \Drupal::request()->request->get('name'); // form param Incidentally, for GET vars, you would use: $query = \...
Clive's user avatar
  • 167k
44 votes

How to get $_POST and $_GET parameters

In a controller get the request with a type-hinted argument Request $request: <?php namespace Drupal\mymodule\Controller; use Drupal\Core\Controller\ControllerBase; use Symfony\Component\...
4uk4's user avatar
  • 100k
26 votes

Correct way to call a service in a controller

There are 3 questions here: How to create a controller? How to create a service? How to use services in a controller? 1. Create a controller 1.1 Add a controller class - you did it. For others ...
zendka's user avatar
  • 421
23 votes

How to get $_POST and $_GET parameters

Inject the RequestStack into your controller. The current request contains the query attribute that in turn contains the GET parameters. request contains the POST parameters. <?php namespace ...
Tim's user avatar
  • 1,397
15 votes
Accepted

How to add Pager to a custom page?

There are some examples on drupal.org of adding a pager to the page, if you want to use the one that core uses: https://api.drupal.org/api/drupal/core%21includes%21pager.inc/function/...
Kevin's user avatar
  • 27.9k
11 votes
Accepted

How to check whether a username already exists?

I guess you can use a simple EntityQuery. $ids = \Drupal::entityQuery('user') ->condition('name', 'USERNAME_TO_CHECK') ->range(0, 1) ->execute(); if(!empty($ids)...
Drupal de Comptoir's user avatar
11 votes

How to check whether a username already exists?

Instead of custom one-off checks, you should use the validation API. Before saving, call $user->validate(). That tells you if anything with the values isn't OK, there is also a limit on what ...
Berdir's user avatar
  • 83.3k
10 votes
Accepted

How to alter breadcrumbs from my controller in my module?

To customize your Breadcrumbs you need to take full control of the Breadcrumb for that particular route with your own Breadcrumb Builder service. To do this, you need to create an entry in your ...
Decipher's user avatar
  • 3,722
10 votes

Cache controller with json response

The controller from your question runs without exception: public function demo() { $build = [ '#cache' => [ 'contexts' => ['ip'], ], ]; return (new CacheableJsonResponse(['...
4uk4's user avatar
  • 100k
10 votes
Accepted

Is there a "best practice" for overriding a contributed module's controller?

To avoid conflicts with other modules you could alter the controller content in a kernel VIEW event similar to a form alter hook: src/EventSubscriber/ControllerAlterSubscriber.php: <?php namespace ...
4uk4's user avatar
  • 100k
9 votes
Accepted

Take over display of a content type node route

You'll need to alter the entity.node.canonical route to do this. You can do this via a RouteSubscriber::alterRoutes() in a custom module to have the route driven by your custom logic. In src/Routing/...
Shawn Conn's user avatar
  • 26.9k
9 votes
Accepted

How can I add a custom form to the output of a controller class?

I think you want to return a render array, instead of markup. Because the from needs to stay as a render array, your best bet it to bring your content content to the same format instead of the other ...
Dave Reid's user avatar
  • 4,008
8 votes

Create route that returns XML Feed

Kevin's answer caused my Twig template's <?xml ...> tag to be commented out. I had to add to make the page render it as XML. $response->headers->set('Content-Type', 'text/xml'); Full ...
Chris Happy's user avatar
  • 1,897
7 votes
Accepted

Create route that returns XML Feed

Can you give more details ? For example for an aggregator RSS : In your test.routing.yml : test.rss: path: '/rss' defaults: _title: 'rss' _controller: '\Drupal\test\Controller\...
Kevin's user avatar
  • 2,556
7 votes
Accepted

How to inject the database connection into a custom service?

You can't inject Drupal\Core\Database\Database, it's a container of static functions, constants, & variables for core database functions. If you'd like to inject the Connection object, that is be ...
Shawn Conn's user avatar
  • 26.9k
6 votes

How to inject the database connection into a custom service?

As commented on another answer, the answer here is probably, you can't. The database API isn't fully converted to use services. I tried to do it for quite some time, in https://www.drupal.org/node/...
Berdir's user avatar
  • 83.3k
6 votes
Accepted

How do I define a route and controller for the home page?

This is a classic case of trying to do something in the non-Drupal way -- conceptually, defining a custom route for your home page route makes sense, and it's possible in other frameworks, but Drupal ...
Lincoln Bergeson's user avatar
5 votes
Accepted

I cannot use drupal_set_message() in a custom controller module

You might want to try the following code. return [ '#theme' => 'ctr', '#myvar' => 'myvalue', ]; The module needs also to implement hook_theme(). /** * Implements hook_theme(). */ function ...
Wim Mostrey's user avatar
  • 2,827
5 votes

Call AJAX callback on page load

Thought I would share this as a slightly more complete solution, this would be your full javascript file: (function ($, Drupal) { Drupal.behaviors.myModuleName = { attach: function (context, ...
Duncanmoo's user avatar
  • 2,951
5 votes

How to get $_POST and $_GET parameters

If you are using Content-Type: application/json please use : $post_data = json_decode( $this->request->getContent(),TRUE);
Denis Viunyk's user avatar
5 votes

Render node from Controller

Of course as soon as I posted this I figured an answer. This looks a little ugly, but I guess this is the way to go: public function content() { $nodes = $this->getStudents(); foreach ($nodes ...
icicleking's user avatar
5 votes
Accepted

How to deal with Bubbleable Metadata when there's no rendering context and no render array

Yes, when rendering email data you have to take care that you don't bubble up metadata to the render context of the main response. There is a third option, and in most cases the best option: The ...
4uk4's user avatar
  • 100k
4 votes

How to get $_POST and $_GET parameters

You can check the Request object like \Drupal::request()->getMethod(); it will returns GET or POST or whatever. If you are working inside an object, remember to DI the Request service.
ruloweb's user avatar
  • 181
4 votes

How to inject the database connection into a custom service?

At this moment I'm using this code to inject the database service: *.services.yml services: onlyone: class: Drupal\onlyone\OnlyOne arguments: ['@entity_type.manager', '@database', '@...
Adrian Cid Almaguer's user avatar
4 votes

Call AJAX callback on page load

Funny how those tutorials did not mention Drupal behaviors, which will trigger once the page is loaded. This is a good tutorial on them (Drupal 7, but same principles apply). Basically all you should ...
Neograph734's user avatar
  • 8,419
4 votes

How do I redirect users to an external URL?

Define a controller and redirect to external URL. Use TrustedRedirectResponse class in case you know that you want to redirect to an external URL. Example below: use Drupal\Core\Controller\...
Shreya Shetty's user avatar
4 votes
Accepted

Render custom html below a custom form

You have to use the power of render arrays. For your example, I would use something like that : $form_class = 'Drupal\xyz\Forms\XyzActionsForm'; $form = [ 'form' => $this->formBuilder->...
guilhom's user avatar
  • 631
4 votes
Accepted

How to render a custom entity in a controller?

Controllers should return a render array (or a custom response), you almost never need to render anything yourself outside a template. Just replace this: $build = $view_builder->view($entity); $...
Clive's user avatar
  • 167k
4 votes
Accepted

Routing for controller with multilingual enabled

The language is detected and the prefix removed before routing. So you have a static route without parameter and in the controller you can get the prefix from the language manager: $langcode = $...
4uk4's user avatar
  • 100k
4 votes
Accepted

Edit a config variable of a custom module in controller

With the ConfigFormBase, you need to implement the editableConfigNames() method. E.g. /** * {@inheritdoc} */ protected function getEditableConfigNames() { return [ 'example....
CG Monroe's user avatar
  • 3,056

Only top scored, non community-wiki answers of a minimum length are eligible