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
•
18 Apr 2024
LogicException: Form errors cannot be set after form validation has finished
When receiving a logic error such asLogicException: Form errors cannot be set after form validation has finished. in Drupal\Core\Form\FormState->setErrorByName() (line 1104 of /var/www/html/project/core/lib/Drupal/Core/Form/FormState.php).This error "LogicException: Form errors cannot be set...
Andrew Fletcher
•
09 Apr 2024
Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException
Adding a new service to a custom module and the following error greeted me:The website encountered an unexpected error. Try again later.
Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException: You have requested a non-existent service "summaries.reference_node". in...
Andrew Fletcher
•
05 Apr 2024
Drupal 9 to 10 upgrade an example of the actual steps taken
Continuing on from the Drupal 9 to Drupal 10 upgrade outline written earlier... Upgrading Drupal 9 to Drupal 10. However, the point of difference is this article works through the actual action steps of the upgrade.Planning and testing is critical for the Drupal to version 10 process to...