Just a minor problem...

mymodule.twig.foobar:
    class: Drupal\mymodule\Template\TwigFoobarExtension
    tags:
      - { name: twig.extension }

If you create a tagged service, e.g. for a twig extension, and the entered namespace can't be resolved/file can't be found, you get a misleading exception:

exception 'Symfony\Component\DependencyInjection\Exception\LogicException' with message 'Service 'mymodule.twig.foobar' for consumer 'twig' does not [error]
implement Twig_ExtensionInterface.

if (!is_subclass_of($handler->getClass(), $interface)) {
  throw new LogicException("Service '$id' for consumer '$consumer_id' does not implement $interface.");
}

My suggestion:

Service '$id' for consumer '$consumer_id' does not implement $interface, or its namespace cannot be resolved.

This might save some time for anyone else.

Members fund testing for the Drupal project. Drupal Association Learn more

Comments

seppelM created an issue. See original summary.

cilefen’s picture

If the exception is in Symfony it must be fixed there.

seppelM’s picture

see line 130 of Drupal/Core/DependencyInjecetion/Compiler/TaggedHandlersPass (https://api.drupal.org/api/drupal/core!lib!Drupal!Core!DependencyInjecti...).
It's not part of the vendor components.

cilefen’s picture

Sorry, you are right. I was confused by the exception's namespace, Symfony\Component\DependencyInjection\Exception\LogicException.

cilefen’s picture

Status: Active » Needs review
Issue tags: +Quick fix
FileSize
1021 bytes

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

erickatbattle’s picture

Hello guys, I am working on trying to extend the twig function from a custom module and I am also getting the same issue mentioned above, is this something still being worked on?

Or would anybody know a solution.

I am getting the following error:

The website encountered an unexpected error. Please try again later.
Symfony\Component\DependencyInjection\Exception\LogicException: Service 'bml_site.twig.bml_site_extension' for consumer 'twig' does not implement Twig_ExtensionInterface. in Drupal\Core\DependencyInjection\Compiler\TaggedHandlersPass->process() (line 125 of [I REMOVED MY PATH]/core/lib/Drupal/Core/DependencyInjection/Compiler/TaggedHandlersPass.php).
Drupal\Core\DependencyInjection\Compiler\TaggedHandlersPass->process(Object) (Line: 107)
Symfony\Component\DependencyInjection\Compiler\Compiler->compile(Object) (Line: 593)
Symfony\Component\DependencyInjection\ContainerBuilder->compile() (Line: 1254)
Drupal\Core\DrupalKernel->compileContainer() (Line: 866)
Drupal\Core\DrupalKernel->initializeContainer() (Line: 40)
Drupal\Core\Update\UpdateKernel->initializeContainer() (Line: 461)
Drupal\Core\DrupalKernel->boot() (Line: 62)
Drupal\Core\Update\UpdateKernel->handle(Object) (Line: 19)

My services file looks like this:


services:
  bml_site.twig.bml_site_extension:
    class: Drupal\bml_site\Twig\BMLSiteExtension
    tags:
      - { name: twig.extension }

Thanks

cilefen’s picture

This issue is about a misleading error when the namespace can't be found. In your case, does Drupal\bml_site\Twig\BMLSiteExtension exist and does it implement Twig_ExtensionInterface or more likely, extend \Twig_Extension?

steve-spa-tech’s picture

Just a quick note, when this was just occurring for me I found that the src file name is case sensitive
i.e. my class def is "class Images extends \Twig_Extension", with a filename of "images.php" it would fail compilation, but "Images.php" would work. (probably not related to the core issue but might help newcomers like me to get some progress).
Steve.

dawehner’s picture

Maybe something which tells that either the class or the namespace is wrong would be IMHO more concrete. The current proposed error message seems to be really cryptic, at least for me.

pounard’s picture

There's a real bug here, most Symfony's compiler class will manually resolve class names before using it, for example you can see it there: https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Eve...

Drupal never does this, and as soon as you rely upon parameters for class names, Drupal will go into havoc, and this is a seriously blocking issue for contrib.

I am preparing a complete explicative bug report for this and a preliminary proof a concept patch, will link it here as soon as it's done.

pounard’s picture

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

lorenzs’s picture

Status: Needs review » Reviewed & tested by the community

As I lost some time on this I'd suggest putting it in while we wait for #2832018 to be completed. Tested.

xjm’s picture

Status: Reviewed & tested by the community » Needs review

I agree with @dawehner in #11; the current exception message doesn't go far enough to explain the problem. I agree with hotfixing this while #2832018: Compiler passes don't manually resolve class names of definition before using it is underway to provide better DX and debugging, and I think we could still backport it since exception messages are not translated and we're not changing what exceptions are thrown or where, but I wouldn't know what to do with "its namespace cannot be resolved". So let's add more detail that the class or namespace could be incorrect...

But actually, we should probably be throwing a different exception message earlier. Is this happening if $handler or $handler->getClass() are empty? If so, we could throw a different exception in that case rather than relying on an unclear "or".

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

berenddeboer’s picture

Just got hit with this too. Drupal 8 is already extremely hard to use, why can't somebody just add this easy patch?

cilefen’s picture

The reason is that the feedback in #17 hasn’t been seen to. Please upload a patch.

dawehner’s picture

Here is a test coverage, anyone wants to try to implement it?

Status: Needs review » Needs work

The last submitted patch, 21: 2652694-21.patch, failed testing. View results