5

I'm trying to load a service into my Drupal module, but even after checking twice whether I did something wrong, I can't seem to find any error in my code as I did everything as the documentation said and Drupal still gives me a non-existent service error.

Here's the my services.yml file:

services:
  rb_weather.wunderground:
    class: Drupal\rb_weather\WundergroundWeather

Here's the config form code (the part loading the service):

class RBWeatherSettingsForm extends ConfigFormBase
{
/**
 * @var WeatherInterface
 */
private $wunderground;

public function __construct(ConfigFactoryInterface $config_factory,    WeatherInterface $wunderground)
{
    parent::__construct($config_factory);

    $this->wunderground = $wunderground;
}

public static function create(ContainerInterface $container)
{
    return new static(
        $container->get('config.factory'),
        $container->get('rb_weather.wunderground')
    );
}

I have included the classes using the "use" command as PhpStorm doesn't complain about that, however, Drupal does not want to recognize it.

My service classes as well as the services.yml file are placed under rb_weather/services folder.

Thanks a lot.

4

Not certain, but it sounds like your files may not be in the correct places based on your description.

The services.yml should be in the root of your module, not in a sub folder.

Also, your service classes should be in a /src/ folder. It would be okay to have them in a /src/services folder if you want, but module namespaces in drupal start in the module's /src/ folder, not in the root; the reason for this is because drupal uses the PSR-4 naming conventions, which specify that structure. You can find more info about how drupal modules should be structured here: https://www.drupal.org/node/2156625

  • Weirdly enough, moving the services.yml file solved the issue. However, formerly I had the services.yml file in the root folder of my module and I had moved it into the services folder only after drush started complaining that it should be placed there upon clearing the cache. – crzpiot Sep 30 '16 at 17:16

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.