- Issue created by @fkelly
- 🇺🇸United States fkelly
After intermittent work on this over the last couple of weeks, I am about at the end of my rope. Using PHPstorm's find in files feature, I can see that there are many other programs that use the same code to access the file_url_generator service. I've looked for a "constructor" that they may have in common or something else that they are doing prior to this "call" and don't see it. And as far as I can tell the only thing wrong is that PHPCS doesn't like the fact that the service is not properly being "injected". I've run PHPCS against some of the other programs that use the same code and it doesn't show any errors for them. I can dump (var_dump) the $file_url_generator variable after
/** @var \Drupal\Core\File\FileUrlGeneratorInterface $file_url_generator */ $file_url_generator = \Drupal::service('file_url_generator');
and you see a nice healthy object. And the program seems to work successfully.
In the interim I have updated both my test Wampserver site and my "production" site on shared hosting to Drupal 10.0.9. I have hundreds of Juicebox albums. I have some accessed through a view and I've even built some test ones completely within Drupal by inserting images into a purpose built content type. All seems fine. In the 6 weeks since we put out the Drupal 10 compliant version of Juicebox, we've only had one issue reported and the user resolved that himself.
The other pending patches should fix all other PHPCS issues. If not I will work on them further.
- 🇺🇸United States fkelly
Other contrib programs seem to avoid the PHPCS warning. One is simple_sitemap. In: modules/contrib/simple_sitemap/src/Plugin/simple_sitemap/UrlGenerator/EntityUrlGeneratorBase.php
They have a use statement:
use Drupal\Core\File\FileUrlGeneratorInterface;
whereas in JuiceboxFormatter we are "using"
use Drupal\Core\Routing\UrlGeneratorInterface;
Looking at the program in the routing directory, it does not appear to be the one we want. It doesn't for instance have a function for generateAbsoluteString(string $uri) ... which appears to be needed in JuiceboxFormatter. If we just go out and "get" the Drupal service directly from core.services.yml (i.e., not injecting it) we seem to get the right code but we cause PHPCS to issue a warning.
I think we may need some combination of the simple_sitemap approach (and not using the routing url) with the appropriate constructors to get the proper code "injected" without triggering PHPCS. It's a long way "round rosey's barn" when we just need to generate an Absolute string version of the url.
Other programs such as core/modules/image/src/Entity/ImageStyle.php seem to be able to avoid the PHPCS warning (I've run it against them) but I'm not yet seeing exactly how.
- Status changed to Closed: outdated
over 1 year ago 1:55am 9 August 2023 - 🇺🇸United States fkelly
This issue was outdated and made obsolete by issue:
https://www.drupal.org/project/juicebox/issues/3371019 📌 \Drupal calls should be avoided in classes, use dependency injection instead Fixed
where the dependency injection issue seems to be resolved. I am marking it "outdated".