0

Error:

The website encountered an unexpected error. Please try again later. TypeError: Argument passed to Drupal{module name}\Theme{class name}::__construct() must implement interface Drupal\path_alias\AliasManagerInterface, instance of Drupal\Core\Path\AliasManager

Getting error in the construct method, please suggest a solution to rectify the error.

Try to modify the "AliasManagerInterface" as "use Drupal\Core\Path\AliasManagerInterface" deprecated and we are now using "use Drupal\path_alias\AliasManagerInterface" but actually getting error inside construct method as mentioned below.

code:

 use Drupal\path_alias\AliasManagerInterface;



  /**
   * Protected pathAlias variable.
   *
   * @var Drupal\path_alias\AliasManagerInterface
   */
  protected $pathAlias;


  public function __construct(AliasManagerInterface $pathAlias) {
    
    $this->pathAlias = $pathAlias;

  }
0

1 Answer 1

4

You must be on Drupal 8.8, or Drupal 8.9.

Drupal8, until 8.7, declared the path.alias_manager service, that referred to Drupal\Core\Path\AliasManager. In Drupal 8.8, that service was deprecated, to be removed in Drupal 9.x. A new service was introduced as a replacement, the path_alias.manager service, which refers to Drupal\path_alias\AliasManager.

The dependency injection from the original question is expecting an object of implementing the interface Drupal\path_alias\AliasManagerInterface, and the path_alias.manager service does implement this interface. However, the error says that the dependency injection is not receiving an object that implements Drupal\path_alias\AliasManagerInterface, it is incorrectly receiving an object of type Drupal\Core\Path\AliasManager, giving you the error.

The original question does not show how the dependency is being generated, but in Drupal the two most common methods are creating a service and declaring dependencies in the *.services.yml file for the module, or in the create() method of the class. Whichever method was used, is still using the old path.alias_manager service, and therefore passing Drupal\Core\Path\AliasManager to the class instead of Drupal\path_alias\AliasManager. The solution is to change service being injected from path.alias_manager to path_alias.manager.

Further reading: https://www.drupal.org/node/3092086

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.