Thumbnail navigation doesn't work with reference fields

Created on 7 August 2023, over 1 year ago
Updated 3 April 2024, 8 months ago

Problem/Motivation

I have Photo and Photo Gallery content types slide. Photo entities have several fields such as description, credits caption etc. Images are referenced via media reference field. Photo Gallery content type uses entity reference fields to reference Photo entities. Flex Slider field formatter works as expected on its own, when displaying referenced Photos, but when I set slider navigation to use Thumbnails - it fails with the following error:
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).

Apparently that line of code expects rendered HTML while instead is has a variables array.

Steps to reproduce

I have the following setup:
- Drupal 10.0.x
- Flex Slider 2.x-dev with Drupal 10 compatibility patches
- Ordinary content type configuration as described above

So in order to reproduce the issue - create two content types: one to represent a Photo and another to represent a Photo Gallery. Use entity reference field to reference Photo items from within Photo Gallery. Configure FlexSlider preset to use Thumbnail navigation. Use field formatter to render Photos in the Photo Gallery.

πŸ› Bug report
Status

Active

Version

2.0

Component

Fields Integration

Created by

πŸ‡ΊπŸ‡ΈUnited States euk

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

Comments & Activities

  • Issue created by @euk
  • πŸ‡¨πŸ‡΄Colombia camilo.escobar

    I could reproduce this issue when using the "FlexSlider (Entity view)" field formatter on an entity reference field.
    In such a case, the $variables['item'] that arrives to template_preprocess_flexslider_list_item(&$variables) is the render array of the referenced entity, something like the following:

    array:6 [β–Ό
      "#paragraph" => Drupal\paragraphs\Entity\Paragraph {#4178 β–Ά}
      "#view_mode" => "default"
      "#cache" => array:3 [β–Ά]
      "#theme" => "paragraph"
      "#weight" => 0
      "#pre_render" => array:1 [β–Ά]
    ]
    

    In other cases, as for example when the FlexSlider is selected as the display option from a View, $variables['item'] here will contain HTML markup (I assume it undergoes the rendering pipeline in an earlier state).

    Hence, when the entity render array above is attempted to be passed to preg_match():

    if (!preg_match("/<img.+?src=[\"'](.+?)[\"'].+?>/", $item, $src)) {
      preg_match("/<img.+?srcset=[\"'](.+?)[\"'].+?>/", $item, $src);
    }
    

    PHP triggers the error:
    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).

    I assume this could be "easily solved" if we first render the array to get its resulting markup, before it undergoes the preg_match(), something like this:

    if (is_array($item)) {
      $item = \Drupal::service('renderer')->renderRoot($item);
    }
    

    At least it would avoid the preg_match(): Argument #2 error.

    However, I feel that grabbing the URL of the thumbnail image from the src attribute of a <img /> tag detected via a regular expression is not the best way to provide support for thumbnail images. It is like a very odd and random way to pick the thumbnails up.

    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.

  • πŸ‡ΊπŸ‡ΈUnited States euk

    It's been a while since I looked at that, but if we are to pull a thumbnail image - why not from the originally referenced media? with some option to choose what image style to use...

  • Bumped to major because media thumbnails are broken too.

    Maybe the work from the referenced issue should be rolled together.

Production build 0.71.5 2024