Score:0

Contrib module service overriding not working

cn flag

I am using the ‘Path Redirect Import module along with the Redirect module. I am trying to override its service ‘path_redirect_import.importer’ To do this, I have added a service provider and a custom class that extends the service class that I want to override. however, the overridden function is not getting called. Followed this tutorial:

Any suggestions?

  1. inside my custom module's src folder, I created MyModuleServiceProvider.php, which contains the following:

    namespace Drupal\my_module;

     use Drupal\Core\DependencyInjection\ContainerBuilder;
     use Drupal\Core\DependencyInjection\ServiceModifierInterface;
     use Drupal\Core\DependencyInjection\ServiceProviderBase;
    
    
     class MyModuleServiceProvider extends ServiceProviderBase implements ServiceModifierInterface {
    
       public function alter(ContainerBuilder $container) {
    
         if ($container->hasDefinition('path_redirect_import.importer')) {
           $definition = $container->getDefinition('path_redirect_import.importer');
           $definition->setClass('Drupal\my_module\MyTestProv');
         }
    
       }
    
     }
    

in the MyTestProv.php

namespace Drupal\my_module;


use Drupal\path_redirect_import\ImporterService;

/**
 * Class ImporterService.
 *
 * @package Drupal\path_redirect_import
 */
class MyTestProv extends ImporterService {
  public static function import($file, array $options) {
    // If I put something here nothing happens
    \Drupal::messenger()->addMessage(t('Lorem ipsum.'));
  }
}
id flag
This code is different to what you posted in Slack. The first problem is that if the module namespace is my_module then the ServiceModifierInterface implementation must be named MyModuleServiceProvider, not UelCoreServiceProvider.
Aurora avatar
cn flag
I wanted to sound a bit generic here. Fixed the class name here. Is there any other issue apart from that?
id flag
Are you sure a flash message is the best way to validate this is called? Is this your actual code?
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.