D10 fix for JuiceboxFormatter

Created on 11 May 2023, over 1 year ago
Updated 9 August 2023, over 1 year ago

PHPCS issue

PHPCS with standard=DrupalPractice option reports dependency injection errors in the JuiceBoxFormatter.php program. The patch file I have uploaded goes as far as I can in remedying this situation but still does not remove the dependency injection "problem." Despite the warning message from PHPCS, the program seems to work correctly with Drupal 10. Still we want to clean this up so this patch is submitted purely for review. In lines 370 and 378 of the patched program the variables $file_url_generator and and $module_extension_list, when var_dump 'd seem to report correct object values. But this still leaves the dependency injection warning to be dealt with.

I have looked at similar programs that use code such as:

/** @var \Drupal\Core\File\FileUrlGeneratorInterface $file_url_generator */
$file_url_generator = \Drupal::service('file_url_generator');

and they seem to work without causing dependency injection warnings in PHPCS. Based on comments from our Senior Maintainer I suspect something about a constructor but don't know what to do next.

📌 Task
Status

Closed: outdated

Version

4.0

Component

Code

Created by

🇺🇸United States fkelly

Live updates comments and jobs are added and updated live.
  • Coding standards

    It involves compliance with, or the content of coding standards. Requires broad community agreement.

Sign in to follow issues

Comments & Activities

  • Issue created by @fkelly
  • 🇮🇹Italy apaderno Brescia, 🇮🇹
  • 🇺🇸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
  • 🇺🇸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".

Production build 0.71.5 2024