Andrew Fletcher published: 1 July 2021 (updated) 15 March 2022 2 minutes read
I'm receiving an error with a path of Drupal\path_alias\AliasManager
Upgrading to Drupal 9, I was running a test to see which blocks and/or modules would fail. And there were plenty... like this AliasManager error
TypeError: Argument 4 passed to Drupal\{module}\Plugin\Block\{custom_block}Block::__construct() must be an instance of Drupal\Core\Path\AliasManager, instance of Drupal\path_alias\AliasManager given, called in /app/web/modules/custom/{module}/src/Plugin/Block/{custom_block}Block.php on line 103 in Drupal\{module}\Plugin\Block\{custom_block}Block->__construct() (line 82 of /app/web/modules/custom/{module}/src/Plugin/Block/{custom_block}Block.php)
The code this error came from is
/** * Constructs a new {module}Block object. * * @param array $configuration * A configuration array containing information about the plugin instance. * @param string $plugin_id * The plugin_id for the plugin instance. * @param string $plugin_definition * The plugin implementation definition. * @param Drupal\Core\Path\AliasManager $path_alias_manager * The path alias manager. * @param \Drupal\Core\Path\PathMatcher $path_matcher * The path matcher. * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack * The request stack object. * @param \Drupal\Core\Path\CurrentPathStack $path_current * The current path stack. * @param \Drupal\Core\Entity\EntityStorageInterface $header_images * The header images entity storage. * * @internal param \Drupal\Core\Config\Config $configured_images */ public function __construct(array $configuration, $plugin_id, $plugin_definition, AliasManager $path_alias_manager, PathMatcher $path_matcher, RequestStack $request_stack, CurrentPathStack $path_current, EntityStorageInterface $images) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->pathAliasManager = $path_alias_manager; $this->pathMatcher = $path_matcher; $this->requestStack = $request_stack; $this->pathCurrent = $path_current; $this->headerImages = $images; } /** * {@inheritdoc} */ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { return new static( $configuration, $plugin_id, $plugin_definition, $container->get('path_alias.manager'), $container->get('path.matcher'), $container->get('request_stack'), $container->get('path.current'), $container->get('entity_type.manager')->getStorage('image') ); }
The error was in using the old method to call the use and params.
Pre Drupal 9 method
use Drupal\Core\Path\AliasManager; @param Drupal\Core\Path\AliasManager $path_alias_manager
New Drupal 9 method
use Drupal\path_alias\AliasManager; @param Drupal\path_alias\AliasManager $path_alias_manager
Related articles
Andrew Fletcher
•
04 Apr 2025
Managing .gitignore changes
When working with Git, the .gitignore file plays a critical role in controlling which files and folders are tracked by version control. Yet, many developers are unsure when changes to .gitignore take effect and how to manage files that are already being tracked. This uncertainty can lead to...
Andrew Fletcher
•
26 Mar 2025
How to fix the ‘Undefined function t’ error in Drupal 10 or 11 code
Upgrading to Drupal 10.4+ you might have noticed a warning in their code editor stating “Undefined function ‘t’”. While Drupal’s `t()` function remains valid in procedural code, some language analysis tools — such as Intelephense — do not automatically recognise Drupal’s global functions. This...
Andrew Fletcher
•
17 Mar 2025
Upgrading to PHP 8.4 challenges with Drupal contrib modules
The upgrade from PHP 8.3.14 to PHP 8.4.4 presents challenges for Drupal 10.4 websites, particularly when dealing with contributed modules. While Drupal core operates seamlessly, various contrib modules have not yet been updated to accommodate changes introduced in PHP 8.4.x. This has resulted in...