I am getting the following error:

Warning: Missing argument 1 for Drupal\custom_forms\Service\CustomFormsService::__construct(), called in /drupal/docroot/core/lib/Drupal/Component/DependencyInjection/Container.php on line 264 and defined in Drupal\custom_forms\Service\CustomFormsService->__construct() (line 13 of modules/custom_forms/src/Service/CustomFormsService.php).

And my code (service.php) is:

public function __construct($request) {
$this->cityname = \Drupal::request()->get('cityname');

if($this->cityname != ''){
  $result = \Drupal::database()->select('city_municipality','cm')
    ->condition('city', $this->cityname)
    $result = json_encode($result);
    print $result; 


           class: Drupal\custom_forms\Service\CustomFormsService

But the form functionalities are working fine.

Can anyone help me out on this?

  • Is this a service class or a form class? If it's a service class, what does your custom_forms.services.yml file look like? If it's a form class, do you have a function named create() in the class? If so, please show it.
    – Jaypan
    Feb 7 '17 at 11:59
  • custom_forms.services.yml services: custom_forms.custom_forms_service: class: Drupal\custom_forms\Service\CustomFormsService
    – ancy
    Feb 7 '17 at 12:01

You need to add an argument to your services.yml for the class to be injected:

    class: Drupal\custom_forms\Service\CustomFormsService
      - @request_stack

Note that you should make a few changes to your service as well:

First, add an type hint for the argument:

public function __construct(\Symfony\Component\HttpFoundation\RequestStack $requestStack) {

Next, you will use the $requestStack instead of Drupal::request():

$this->cityname = $requestStack->getCurrentRequest()->query->get('cityname');
  • I am getting the following error :Recoverable fatal error: Argument 1 passed to Drupal\custom_forms\Service\CustomFormsService::__construct() must be an instance of Symfony\Component\HttpFoundation\RequestStack, none given, called in /drupal/docroot/core/lib/Drupal/Component/DependencyInjection/Container.php on line 264 and defined in Drupal\custom_forms\Service\CustomFormsService->__construct() (line 14 of modules/custom_forms/src/Service/CustomFormsService.php).
    – ancy
    Feb 7 '17 at 12:19
  • You need to clear your cache.
    – Jaypan
    Feb 7 '17 at 12:22
  • I am getting the following error while clearing my cache..Fatal error: Call to undefined method Symfony\Component\HttpFoundation\RequestStack::get() in /drupal/docroot/modules/custom_forms/src/Service/CustomFormsService.php on line 16
    – ancy
    Feb 7 '17 at 12:28
  • Sorry, I think I had an error. I've updated the code. I should probably test before posting!
    – Jaypan
    Feb 7 '17 at 12:33
  • 1
    That has nothing to do with the current topic. So please mark this one as solved, and open a new topic for that error.
    – Jaypan
    Feb 7 '17 at 12:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.