Skip to main content

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