Call to a member function isDisplayed() on null error

Created on 28 October 2022, over 1 year ago
Updated 10 December 2023, 7 months ago

Problem/Motivation

The Juicebox field formatter may be applicable to invalid types. This condition leads to fatal errors.

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

Steps to reproduce

  1. Enable juicebox, media, and layout builder on a Standard installation
  2. Create a content type
  3. Enable layout builder for the content type
  4. Add an media entity reference field to the content type
  5. Allow images and remote videos as allowed bundles on the entity reference field
  6. Add a piece of remote video content.
  7. Add the image field to the default layout for the content type
  8. Observe that the site dies with a fatal PHP error, indicated in the problem/motivation. This is because the Juicebox field formatter assumes that referenced media is of certain bundles, but that requirement is not enforced.

Proposed Resolution

Make the Juicebox field formatter only applicable to valid types in order to prevent site builders from incorrectly assigning the juicebox field formatter to invalid field types. Create a status report that warns users about existing invalid field configurations and directs them to documentation on how to resolve the situation.

🐛 Bug report
Status

Fixed

Version

4.0

Component

Code

Created by

🇮🇪Ireland stella

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

Merge Requests

Comments & Activities

Not all content is available!

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

  • 🇺🇸United States fkelly

    Since we may have a new maintainer or maintainers soon, I will just add a reminder that this should be committed. I don't see any way that I can rtbc this or I would.

    There are a few loose ends that should be cleaned up once this project is being maintained again.

  • Status changed to RTBC over 1 year ago
  • 🇺🇸United States fkelly

    Just found where to rtbc this under issue metadata. So I did. I have been running this modified code on both test and production systems since it was first proposed.

  • 🇮🇳India newswatch Delhi/Bangalore

    I suddenly got this error.

    But the reason had nothing to do with the plugin itself.

    The formatter for a taxonomy term in Manage Display had taken Juicebox Gallery by default. I hadn't noticed it.

    Changing the formatter to Label obviously solved the problem.

  • 🇺🇸United States fkelly

    Just noticed that this request is for 8.x-3-dev. This won't automatically update the alpha2 release which is what Drupal8 and 9 users should be using now. Just checked the 4.0 code and the bug is in there too. Which is natural since that derives from 8.x-3.x-dev.

    Maybe Luke can review and apply the patch to all 3 if appropriate. I've been running a manually applied version of the patch on both my older (now defunct) 8.x system and now on my 10.0.7 test system.

  • 🇺🇸United States Luke.Leber Pennsylvania

    I have a feeling that there are some unseen factors at play here.

    We might have a better time at implementing a method in the formatter that prevents this from happening in the first place. This will straight up remove the formatter from site builders' options so that this condition can't exist in the first place for folks to even see :-).

      /**
       * {@inheritdoc}
       *
       * Ensure that only media types sourced through images are referenceable.
       */
      public static function isApplicable(FieldDefinitionInterface $field_definition) {
        $applicable = parent::isApplicable($field_definition);
        if ($applicable) {
          if ($field_definition->getSetting('target_type') === 'media') {
            $handler_settings = $field_definition->getSetting('handler_settings');
    
            // If no target bundles are set, assume all are allowed.
            $allowed_bundles = $handler_settings['target_bundles'] ?? array_keys(\Drupal::service('entity_type.bundle.info')->getBundleInfo('media'));
    
            // Filter out all valid bundles.
            $invalid_bundles = array_filter($allowed_bundles, static function($allowed_bundle) {
              return MediaType::load($allowed_bundle)->getSource()->getPluginId() !== 'image';
            });
    
            // Only apply to this field if there are no invalid bundles!
            $applicable = empty($invalid_bundles);
          }
        }
    
        return $applicable;
      }
    

    Furthermore, we should probably consider hardening the implementation a bit. field_media_image is site builder controlled -- not a guarantee! The Media API has us covered though.

          if ($item->getPluginId() === 'field_item:entity_reference') {
            /** @var \Drupal\media\MediaInterface $media_entity */
            $media_entity = $item->entity;
    
            // Load the media type of the entity.
            $media_entity_type = MediaType::load($media_entity->bundle());
            
            // Dynamically figure out what the source field is configured to be.
            $source_field = $media_entity->getSource()->getSourceFieldDefinition($media_entity_type);
    
            $item = $media_entity->get($source_field->getName())->first();
          }
    

    should be a bit more robust.

    Acceptance test criteria should probably be that the Juicebox formatter would only appear as an option for site builders when...

    1. A field target is 'image'
    2. A field target is 'file'
    3. A field target is 'entity_reference' AND the entity reference is to 'media' AND the bundles that content editors are allowed to choose all use the 'image' media source type

    Does that make sense?

  • Status changed to Needs work 11 months ago
  • 🇺🇸United States Luke.Leber Pennsylvania

    I'm setting this back to NW on #9 - the UX with the patch as presented means that sites with invalid configuration wouldn't crash, but would probably instead render empty div elements -- which might go un-noticed for quite a while.

    I think that preventing invalid configuration is worth the additional complexity in this case. It's hard to tell what other bugs invalid configuration could incur -- perhaps even security issues in some cases!

  • 🇺🇸United States Luke.Leber Pennsylvania
  • 🇺🇸United States Luke.Leber Pennsylvania

    This new test should prove that there's a problem...and form a base for new Media integrations tests.

  • 🇺🇸United States Luke.Leber Pennsylvania
  • Open on Drupal.org →
    Core: 10.1.x + Environment: PHP 8.1 & MySQL 8
    last update 7 months ago
    Waiting for branch to pass
  • Open in Jenkins → Open on Drupal.org →
    Core: 10.1.x + Environment: PHP 8.1 & MySQL 8
    last update 7 months ago
    11 pass, 1 fail
  • 🇺🇸United States Luke.Leber Pennsylvania

    This should make it impossible to configure new sites incorrectly, however we should also add a warning for site owners that have incorrect configurations present in active storage.

  • Open in Jenkins → Open on Drupal.org →
    Core: 10.1.x + Environment: PHP 8.1 & MySQL 8
    last update 7 months ago
    12 pass
  • 🇺🇸United States Luke.Leber Pennsylvania

    Added a requirements check and attached a draft change record for site builders: https://www.drupal.org/node/3403257

  • Open in Jenkins → Open on Drupal.org →
    Core: 10.1.x + Environment: PHP 8.1 & MySQL 8
    last update 7 months ago
    12 pass
  • Status changed to Needs review 7 months ago
  • Open on Drupal.org →
    Core: 10.1.x + Environment: PHP 8.1 & MySQL 8
    last update 7 months ago
    Waiting for branch to pass
  • 🇺🇸United States Luke.Leber Pennsylvania
  • 🇺🇸United States Luke.Leber Pennsylvania
  • Open in Jenkins → Open on Drupal.org →
    Core: 10.1.x + Environment: PHP 8.1 & MySQL 8
    last update 7 months ago
    12 pass
  • 🇺🇸United States Luke.Leber Pennsylvania

    Resolve coding standards violations that were in scope for this changeset.

  • Open in Jenkins → Open on Drupal.org →
    Core: 10.1.x + Environment: PHP 8.1 & MySQL 8
    last update 7 months ago
    CI aborted
  • 🇺🇸United States Luke.Leber Pennsylvania

    Must be getting tired. Here's the correct patch...heh. #18 had an improperly named test module (from another issue in Juicebox :-) ).

  • Open in Jenkins → Open on Drupal.org →
    Core: 10.1.x + Environment: PHP 8.1 & MySQL 8
    last update 7 months ago
    12 pass
  • 🇺🇸United States Luke.Leber Pennsylvania

    ...and one more change to rename the test case to something more appropriate.

  • Open in Jenkins → Open on Drupal.org →
    Core: 10.1.x + Environment: PHP 8.1 & MySQL 8
    last update 7 months ago
    13 pass
  • 🇺🇸United States Luke.Leber Pennsylvania

    Reroll.

    • Luke.Leber authored a975a7f2 on 4.0.x
      Issue #3318067 by Luke.Leber, fkelly12054@gmail.com, stella, newswatch:...
  • Status changed to Fixed 7 months ago
  • 🇺🇸United States Luke.Leber Pennsylvania

    Committed and pushed to 4.0.x - thanks all.

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

Production build 0.69.0 2024