Call to a member function isDisplayed() on null

Created on 10 April 2025, 16 days ago

Problem/Motivation

Error: Call to a member function isDisplayed() on null in Drupal\juicebox\Plugin\Field\FieldFormatter\JuiceboxFieldFormatter->buildGallery() (line 357 of modules/contrib/juicebox/src/Plugin/Field/FieldFormatter/JuiceboxFieldFormatter.php).

Steps to reproduce

Proposed resolution

My image media type doesn't have the field_media_image field but field_image so this is due to the field_mediate_image field hardcoded in the buildGallery function in the file /src/Plugin/Field/FieldFormatter/JuiceboxFieldFormatter.php

We should get the image field dynamically instead of hardcoding the field_media_image field.

Remaining tasks

User interface changes

API changes

Data model changes

πŸ› Bug report
Status

Active

Version

4.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States hungdo

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

Comments & Activities

  • Issue created by @hungdo
  • πŸ‡ΊπŸ‡ΈUnited States hungdo

    Attaching the patch file.

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

    Thanks for the patch. I applied most of this to my ddev based local site that is derived from 4.0.0-alpha2. Previously I had applied some changes to alpha2 based on PHPstorm suggestions. I had also run PHPCS locally in an effort to resolve "Resolve all coding standard violations" issue #3351683. At one point I had PHPCS running without errors but then got bogged down with other priorities.

    The patch seems to apply okay. On my dev site I can run a Juicebox Gallery with it. If you've worked with PHPstorm you'll know that it makes a zillion other suggestions for tweaks to the code. Such tweaks can, on occasion cause problems within Drupal.

    The full text of the revised function as implemented now on my dev system is:

     protected function buildGallery(JuiceboxGalleryInterface $gallery, FieldItemListInterface $items): void
      {
        // Get settings.
        $settings = $this->getSettings();
        // Iterate over items and extract image data.
        foreach ($items as $item) {
          if ($item->getPluginId() === 'field_item:entity_reference')
            foreach ($item->entity->getFields() as $field_name => $field)
              if ($field->getFieldDefinition()->getType() === 'image' && (str_contains($field_name, 'field_'))) {
                $item = $item->entity->{$field_name}[0];
            break;
          }
          if ($item->isDisplayed() && !empty($item->target_id)) {
            // Set the image title. If we have an incompatible file and are
            // configured to show a link, set the title text as the link.
            $src_data = $this->juicebox->styleImageSrcData($item->entity, $settings['image_style'], $item->entity, $settings['thumb_style'], $settings);
            if (!$src_data['juicebox_compatible'] && $settings['incompatible_file_action'] == 'show_icon_and_link') {
              $anchor = !empty($item->description) ? $item->description : $item->entity->get('filename')->value;
              $title = $this->linkGenerator->generate($anchor, Url::fromUri($src_data['linkURL']));
            }
            else {
              $title = $this->getFieldText($item, $settings['title_source']);
            }
            // Set the image caption.
            $caption = $this->getFieldText($item, $settings['caption_source']);
            // Add this image to the gallery.
            $gallery->addImage($src_data, $title, $caption);
          }
        }

    You are welcome to test this on your own sites after manually applying the patch. Please let us know if it resolves the issues you have reported. To really move beyond alpha2 we need to have a release run through the test suite and fix up any remaining coding discrepancies.

    Problem is that maintainer time for this contrib project is limited and, at least in my case expertise is limited too. I am also focusing most of my remaining Drupal time on coming up with an alternative approach to galleries using the Solo theme and paragraphs based modules. This will give an option that does not rely on non-open source and minimally support JS code.

Production build 0.71.5 2024