1

I have:

services:
  entity.query.external:
    class: Drupal\m_entities\Entity\Query\External\QueryFactory
    arguments: ['@plugin.manager.my_entity_storage_client', '@my_entity.storage_client.response_decoder_factory', '@http_client', '@entity.manager']
    tags:
      - { name: backend_overridable }
  my_entity.storage_client.response_decoder_factory:
    class: Drupal\m_entities\ResponseDecoderFactory
    tags:
      - { name: service_collector, tag: my_entity_response_decoder, call: addDecoder }
  plugin.manager.my_entity_storage_client:
    class: Drupal\m_entities\MyEntityStorageClientManager
    parent: default_plugin_manager
  entity.my_entities.routes:
    class: Drupal\m_entities\Routing\MyEntityRoutes
    arguments: ['@entity.manager']
  access_check.my_entities.add:
    class: Drupal\m_entities\Access\MyEntityAddAccessCheck
    arguments: ['@entity.manager']
    tags:
      - { name: access_check, applies_to: _my_entity_add_access }

but my service provider is not loaded in below code:

ModifyServiceDefinitionsPass

$providers = $kernel->getServiceProviders('app'); // <-- not loaded in here
foreach ($providers as $provider) {
  if ($provider instanceof ServiceModifierInterface) {
    $provider->alter($container);
  }
}

how to make a service provider loaded?

2

services.yml files are not service providers.

ServiceProviders are to dynamically add or modify service definitions, a yml file is just content that's parsed and merged together with everything else.

If that's not loaded then my guess is that you named it incorrectly, but you didn't include the filename, please add that including the other files you have, specifically the info.yml file.

1

To add a service provider, you have to create a class in your module respecting a defined naming convention :

Drupal\<module_name>\<Camelized module name>ServiceProvider

So for a module named my_module, the class you have to create would be

namespace Drupal\my_module;

use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\DependencyInjection\ServiceModifierInterface;

class MyModuleServiceProvider implements ServiceModifierInterface {

  /**
   * Modifies existing service definitions.
   *
   * @param ContainerBuilder $container
   *   The ContainerBuilder whose service definitions can be altered.
   */
  public function alter(ContainerBuilder $container) {
    // TODO: Implement alter() method.
  }
}

You can find the documentation here : https://www.drupal.org/docs/8/api/services-and-dependency-injection/altering-existing-services-providing-dynamic-services

And the related logic here : https://github.com/drupal/drupal/blob/8.5.0/core/lib/Drupal/Core/DrupalKernel.php#L592-L599

<?
// Load each module's serviceProvider class.
foreach ($module_filenames as $module => $filename) {
  $camelized = ContainerBuilder::camelize($module);
  $name = "{$camelized}ServiceProvider";
  $class = "Drupal\\{$module}\\{$name}";
  if (class_exists($class)) {
    $this->serviceProviderClasses['app'][$module] = $class;
  }
  $filename = dirname($filename) . "/$module.services.yml";
  if (file_exists($filename)) {
    $this->serviceYamls['app'][$module] = $filename;
  }
}

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.