Problem/Motivation

PHPStan (mglaman/phpstan-drupal) reports the following against SvgImageFieldFormatter:

Logger assigned from LoggerChannelFactory in a class using DependencySerializationTrait will break serialization. Inject a named logger channel service directly (e.g. @logger.channel.my_channel) instead.

loggerFromFactory.propertyAssignment

The formatter's constructor receives logger.factory and stores $factory->get('svg_image_field') in $this->logger. FormatterBase uses DependencySerializationTrait, which only swaps out properties that are container services (objects with a _serviceId). A channel obtained from the factory is not one, so if the formatter is ever serialized (for example in cached form state during AJAX rebuilds), the whole channel is serialized along with it, including its attached loggers, the request stack and the current user. That can fail outright or bloat the serialized data.

Steps to reproduce

Run PHPStan with mglaman/phpstan-drupal against the module, e.g. phpstan analyse src.

Proposed resolution

  • Add svg_image_field.services.yml defining logger.channel.svg_image_field (parent logger.channel_base, argument svg_image_field). This is the same channel the factory returns, so log output is unchanged.
  • Inject @logger.channel.svg_image_field in SvgImageFieldFormatter::create().
  • For backwards compatibility, keep accepting LoggerChannelFactoryInterface in the constructor with an E_USER_DEPRECATED notice (deprecated in 2.3.8, removed in 3.0.0). In that case, load the named channel from the container so serialization is still fixed.
  • Add an empty post-update hook so sites rebuild the container and register the new service. Without a rebuild, rendering SVG image fields would throw ServiceNotFoundException.

Remaining tasks

  • Review MR.
  • Update the deprecation message's "See" link to point at this issue (or a change record).

User interface changes

None.

API changes

The 8th constructor argument of SvgImageFieldFormatter is now LoggerChannelInterface|LoggerChannelFactoryInterface. Subclasses passing logger.factory continue to work but trigger a deprecation; they should pass @logger.channel.svg_image_field instead. New service: logger.channel.svg_image_field.

Data model changes

None. Sites must run database updates (or rebuild caches) after upgrading.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

mandclu created an issue. See original summary.

mandclu’s picture

Status: Active » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.