0

I'm attempting to create a custom plugin block in Drupal. When attempting to actually access the services that I've registered I continue to get the following exception:

NOTICE: PHP message: Error: Class 'Drupal\MyNamespace\MyRegisteredService' not found in /var/www/html/web/core/lib/Drupal/Component/DependencyInjection/Container.php on line 259 #0 /var/www/html/web/core/lib/Drupal/Component/DependencyInjection/Container.php(173): Drupal\Component\DependencyInjection\Container->createService(Array, 'foo....')

I have registered the services correctly, and have set up dependency injection correctly (I believe) it's just accessing the services that is not working.

My file structure currently looks like:

- web
  - modules
    - custom
      - foo
        - foo.services.yml
        - src
          - MyService.php
          - Plugin
            - Block
              - FooBlock.php

foo.services.yml looks like:

services:
  foo.my_service:
    class: Drupal\MyNamespace\MyRegisteredService
    autowire: true

FooBlock.php looks like (simply for dependency injection):

namespace Drupal\foo\Plugin\Block;

use Drupal\MyNamespace\MyRegisteredService
use Drupal\Console\Bootstrap\Drupal;
use Drupal\Core\Block\BlockBase;
use GuzzleHttp\Client;
use Http\Client\Exception;
use phpDocumentor\Reflection\Types\Array_;
use stdClass;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;


class FooBlock extends BlockBase implements ContainerFactoryPluginInterface {

  private $my_service;

  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('foo.my_service'),
    );
  }

  function __construct(
    array $configuration,
    $plugin_id,
    $plugin_definition,
    Drupal\MyNamespace\MyRegisteredService $my_registered_service,
  ) {
    $this->my_registered_service = $my_registered_service;
  }

MyService.php looks like:

<?php

namespace Drupal\MyNamespace;

class MyService
{
  static function say_hello()
  {
    return 'hello world';
  }
}

I'm not sure if the way that I'm trying to set this up is incorrect or if I haven't set up dependency injection correctly. Some things that I have tried with no success is changing the namespace and removing Drupal\ from it but this changed little.

I've also tried following several guides (such as this) on how set up services, with little luck.

Any help would be appreciated.

baikho
  • 5,203
  • 4
  • 40
  • 47
Ben
  • 733
  • 8
  • 25

1 Answers1

1

I am not sure about the actual name of your service or namespace path, but I can see a few inconsistencies in your placeholder references:

  • Please change all references of Drupal\MyNamespace\MyRegisteredService to Drupal\foo\MyRegisteredService where foo is the name of your module. This should be lower cased.

  • Secondly Drupal\MyNamespace\MyRegisteredService references a class name MyRegisteredService whereas the example class name you give is MyService. These should be equal, so either go with one or the other.

baikho
  • 5,203
  • 4
  • 40
  • 47
  • 1
    My issue was related to how the namespaces were defined, the class name was an error on myside renaming things. Change `Foo` to `foo` resolved the issues. – Ben Jul 28 '20 at 20:51