Error on PHP8.1

Created on 17 July 2022, over 2 years ago
Updated 20 April 2024, 8 months ago

Problem/Motivation

I installed the module on a PHP8.1 system and got the following when I tried to access some content with the field active.

TypeError: preg_match(): Argument #2 ($subject) must be of type string, array given in preg_match() (line 155 of /home/sitename/public_html/modules/contrib/flexslider/templates/flexslider.theme.inc)

Suggestions?

🐛 Bug report
Status

Needs review

Version

2.0

Component

Code

Created by

🇺🇸United States Christopher Riley

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇨🇴Colombia camilo.escobar

    @Christopher Riley, although you reported this issue almost two years ago and no update has been provided, I'm wondering if the problem was reproducible in your case because you were using the "FlexSlider (Entity view)" field formatter on an entity reference field. If so, then maybe this issue could be marked as a duplicate of 🐛 Thumbnail navigation doesn't work with reference fields Active .

  • First commit to issue fork.
  • Pipeline finished with Failed
    10 months ago
    Total: 31s
    #110079
  • 🇮🇳India himanshu_jhaloya Indore

    Created Patch. Please Review

  • 🇨🇴Colombia camilo.escobar

    Thanks @himanshu_jhaloya.

    Your patch would indeed help to avoid the error TypeError: preg_match(): Argument #2 ($subject) must be of type string, array given in preg_match() (line 155 of /home/sitename/public_html/modules/contrib/flexslider/templates/flexslider.theme.inc), since you are explicitly checking that $item is a string.

    Per my debugging, I have seen that $item is indeed a string (HTML markup) in the following scenarios:

    1. When rendering a view as a FlexSlider, via the "FlexSlider" format
    2. When rendering an Image entity field as FlexSlider, using the "FlexSlider" field formatter

    In cases like that, $item would be an HTML string like this:

    ^ Drupal\Core\Render\Markup {#4238 ▼
      #string: """
        <!-- THEME DEBUG -->
        <!-- THEME HOOK: 'image_formatter' -->
        <!-- BEGIN OUTPUT from 'core/modules/image/templates/image-formatter.html.twig' -->
        
        <!-- THEME DEBUG -->
        <!-- THEME HOOK: 'image' -->
        <!-- BEGIN OUTPUT from 'themes/contrib/bootstrap/templates/system/image.html.twig' -->
        <img loading="lazy" src="/sites/default/files/2024-03/my-image.jpg" width="2828" height="4000" alt="." typeof="foaf:Image" class="img-respons ▶
        
        <!-- END OUTPUT from 'themes/contrib/bootstrap/templates/system/image.html.twig' -->
        <!-- END OUTPUT from 'core/modules/image/templates/image-formatter.html.twig' -->
        
        """
    }
    

    In scenarios like that, I dare say that the preg_match(): Argument #2 problem does not occur, since $item is definitely a string.

    However, when you are rendering an entity reference field using the "FlexSlider (Entity view)" field formatter, $item is not a string but a render array instead, like this one:

    ^ array:6 [▼
      "#paragraph" => Drupal\paragraphs\Entity\Paragraph {#4471 ▶}
      "#view_mode" => "default"
      "#cache" => array:3 [▶]
      "#theme" => "paragraph"
      "#weight" => 0
      "#pre_render" => array:1 [▶]
    ]
    

    So far, this is the scenario that led me to reproduce the error and the reason I asked in my comment above if @Christopher Riley was also using that formatter.

    If so, then I think the same discussion is taking place here: 🐛 Thumbnail navigation doesn't work with reference fields Active , with an additional proposal rau

    As a desired enhancement, I'd like to propose that the "FlexSlider (Entity view)" Field Formatter has a new configuration option to select the field from the entity in question from where the thumbnail image must be taken. And the same for the FlexSlider View's display

    I think your patch is good to prevent people using the module from falling into the preg_match error, if they are using the "FlexSlider (Entity view)" field formatter, while continuing to discuss options for supporting thumbnails on sliders created from entity reference fields.

  • 🇨🇴Colombia camilo.escobar

    @himanshu_jhaloya,

    Update on your patch: It only fixes the problem on line 155, but then line 166 will fail and cause the same error:

    TypeError: preg_match(): Argument #2 ($subject) must be of type string, array given in preg_match() (line 166 of modules/contrib/flexslider/templates/flexslider.theme.inc).
    

    Attaching adjusted patch.

  • 🇮🇳India himanshu_jhaloya Indore

    Yes I saw @camilo.escobar thanks for the update

  • I was seeing this, but only for flexsliders that use media instead of files directly:

    TypeError: preg_match(): Argument #2 ($subject) must be of type string, array given in preg_match() (line 155 of modules/contrib/flexslider/templates/flexslider.theme.inc).

  • #7 removes the error, but breaks layout builder compatibility. Flexsliders with LB don't render with the patch.

  • #7 removes the error, but breaks layout builder compatibility. Flexsliders with LB don't render with the patch.

    Something like this works:

        $src = [];
        if (!preg_match("/<img.+?src=[\"'](.+?)[\"'].+?>/", (string) $item, $src)) {
          preg_match("/<img.+?srcset=[\"'](.+?)[\"'].+?>/", (string) $item, $src);
        }
  • Status changed to Needs review 8 months ago
Production build 0.71.5 2024