Quantcast
Channel: Issues for Drupal core
Viewing all articles
Browse latest Browse all 292409

Fix PHP deprecation message: "trim(): Passing null to parameter #1 ($string) of type string is deprecated in _responsive_image_build_source_attributes()" when responsive_image module is used

$
0
0

Problem/Motivation

When a NULL value is set in the mediaQuery property of a [my_theme/module].breakpoints.yml file, and a responsive image is rendered for that breakpoint, a deprecation message is thrown:

Deprecated function: trim(): Passing null to parameter #1 ($string) of type string is deprecated in _responsive_image_build_source_attributes() (line 414 of core/modules/responsive_image/responsive_image.module).

Deprecation warning

Steps to reproduce

  1. Install a minimal Drupal 11
  2. Enable the "Responsive image" module
  3. Configure the article content type image to be displayed as a responsive image in /admin/structure/types/manage/article/display
  4. Tweak the file core/modules/responsive_image/responsive_image.breakpoints.yml and replace the empty string in the mediaQuery property to an empty value
  5. (PHPStorm complained after doing this, but I couldn't find the schema it is validating the YML file against)

  6. Add an article with a image and view its page
  7. The deprecation message is shown. If it isn't, clear caches and reload

Proposed resolution

Change Breakpoint::getMediaQuery() so it returns always a trimmed string, even if there is a NULL in the mediaQuery property of breakpoints definition YML file.

Remaining tasks

Implement or explain why not to implement the second part of #11.
Possibly tests depending on the answer to the above.
Title update, see #34

User interface changes

The warning is no longer shown.

Introduced terminology

None

API changes

Breakpoint::getMediaQuery() now returns always a trimmed string.

Data model changes

None

Release notes snippet

NA

Original report by [username]

Deprecated function: trim(): Passing null to parameter #1 ($string) of type string is deprecated in _responsive_image_build_source_attributes() (line 414 of core/modules/responsive_image/responsive_image.module).
·         _responsive_image_build_source_attributes(Array, Object, Array) (Line: 173)
·         template_preprocess_responsive_image(Array, 'responsive_image', Array) (Line: 287)
·         Drupal\Core\Theme\ThemeManager->render('responsive_image', Array) (Line: 422)
·         Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 201)
·         Drupal\Core\Render\Renderer->render(Array) (Line: 479)
·         Drupal\Core\Template\TwigExtension->escapeFilter(Object, Array, 'html', NULL, 1) (Line: 50)
·         __TwigTemplate_b0521e86aaa2ccb2263d138013ee9ad9->doDisplay(Array, Array) (Line: 405)
·         Twig\Template->displayWithErrorHandling(Array, Array) (Line: 378)
·         Twig\Template->display(Array) (Line: 390)
·         Twig\Template->render(Array) (Line: 55)
·         twig_render_template('core/themes/stable/templates/field/responsive-image-formatter.html.twig', Array) (Line: 384)
·         Drupal\Core\Theme\ThemeManager->render('responsive_image_formatter', Array) (Line: 422)
·         Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 201)
·         Drupal\Core\Render\Renderer->render(Array) (Line: 479)
·         Drupal\Core\Template\TwigExtension->escapeFilter(Object, Array, 'html', NULL, 1) (Line: 44)
·         __TwigTemplate_7f89780e69a1d2ac98482da07a3293ff->doDisplay(Array, Array) (Line: 405)
·         Twig\Template->displayWithErrorHandling(Array, Array) (Line: 378)
·         Twig\Template->display(Array) (Line: 390)
·         Twig\Template->render(Array) (Line: 55)
·         twig_render_template('themes/custom/klesia_extranet/templates/field/field.html.twig', Array) (Line: 384)
·         Drupal\Core\Theme\ThemeManager->render('field', Array) (Line: 422)
·         Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 201)
·         Drupal\Core\Render\Renderer->render(Array) (Line: 479)
·         Drupal\Core\Template\TwigExtension->escapeFilter(Object, Array, 'html', NULL, 1) (Line: 44)
·         __TwigTemplate_f09ca4698c8f32f6a8ab2822b02b331c->doDisplay(Array, Array) (Line: 405)
·         Twig\Template->displayWithErrorHandling(Array, Array) (Line: 378)
·         Twig\Template->display(Array) (Line: 390)
·         Twig\Template->render(Array) (Line: 55)
·         twig_render_template('themes/custom/klesia_extranet/templates/user/user.html.twig', Array) (Line: 384)
·         Drupal\Core\Theme\ThemeManager->render('user', Array) (Line: 422)
·         Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 201)
·         Drupal\Core\Render\Renderer->render(Array, ) (Line: 241)
·         Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 564)
·         Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 242)
·         Drupal\Core\Render\MainContent\HtmlRenderer->prepare(Array, Object, Object) (Line: 132)
·         Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object) (Line: 90)
·         Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object)
·         call_user_func(Array, Object, 'kernel.view', Object) (Line: 142)
·         Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch(Object, 'kernel.view') (Line: 163)
·         Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 80)
·         Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 58)
·         Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
·         Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 48)
·         Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
·         Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
·         Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 708)
·         Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
Deprecated function: trim(): Passing null to parameter #1 ($string) of type string is deprecated in _responsive_image_build_source_attributes() (line 414 of core/modules/responsive_image/responsive_image.module).

Workaround:
If your theme.breakpoints.yml file has an empty value for a mediaQuery, just add empty quotes and this warning will go away.

So this:

theme.xsmall:
  label: xsmall
  mediaQuery:
  weight: 0
  multipliers:
    - 1x
    - 2x

Changed to this:

theme.xsmall:
  label: xsmall
  mediaQuery: ""
  weight: 0
  multipliers:
    - 1x
    - 2x

Viewing all articles
Browse latest Browse all 292409

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>