Error: Call to a member function getItemDefinition() on null in Drupal\lightning_media\Plugin\EntityBrowser\Widget\FileUpload->isAllowedType()

Created on 19 June 2023, about 1 year ago
Updated 26 March 2024, 3 months ago

Problem/Motivation

Happens when uploading files. Adding a patch with null safe operator to prevent this.

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

🐛 Bug report
Status

Needs review

Version

5.0

Component

Code

Created by

🇺🇸United States EthanT Sarasota, Florida

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

Comments & Activities

  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 7.4 & MySQL 5.7
    last update about 1 year ago
    25 pass, 8 fail
  • Issue created by @EthanT
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 7.4 & MySQL 5.7
    last update about 1 year ago
    25 pass, 8 fail
  • Status changed to Needs work about 1 year ago
  • 🇮🇳India supriya1992

    Hello EthanT,
    Could you add the step to reproduce the issue? I was looking into this but couldn't able to reproduce it.

  • Status changed to Needs review 12 months ago
  • 🇮🇳India Ajeet Tiwari

    Please review the patch.

  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 7.4 & MySQL 5.7
    last update 12 months ago
    32 pass
  • 🇮🇳India Ajeet Tiwari

    Sorry patch did not get uploaded due to some technical glitch.

  • 🇨🇿Czech Republic Jiskra

    Hi @ajeet-tiwari
    The patch is not working for me as also row
    $item_class = $item->getSourceFieldDefinition($media_type)
    returns null

  • I am having the same issue when trying to add media. The patch did not work for me either. Here is the full error:

    The website encountered an unexpected error. Please try again later.
    
    Error: Call to a member function getItemDefinition() on null in Drupal\lightning_media\Plugin\EntityBrowser\Widget\FileUpload->isAllowedType() (line 252 of modules/contrib/lightning_media/src/Plugin/EntityBrowser/Widget/FileUpload.php).
    array_filter(Array, Array) (Line: 243)
    Drupal\lightning_media\Plugin\EntityBrowser\Widget\EntityFormProxy->getAllowedTypes() (Line: 305)
    Drupal\lightning_media\Plugin\EntityBrowser\Widget\EntityFormProxy->access() (Line: 309)
    Drupal\entity_browser\Entity\EntityBrowser->Drupal\entity_browser\Entity\{closure}('044d2af7-314b-4830-8b6d-64896bbb861e')
    array_filter(Array, Object) (Line: 310)
    Drupal\entity_browser\Entity\EntityBrowser->getFirstWidget() (Line: 271)
    Drupal\entity_browser\Form\EntityBrowserForm->getCurrentWidget(Object) (Line: 160)
    Drupal\entity_browser\Form\EntityBrowserForm->buildForm(Array, Object)
    call_user_func_array(Array, Array) (Line: 536)
    Drupal\Core\Form\FormBuilder->retrieveForm('entity_browser_media_browser_form', Object) (Line: 283)
    Drupal\Core\Form\FormBuilder->buildForm(Object, Object) (Line: 104)
    Drupal\autosave_form\Form\AutosaveFormBuilder->buildForm(Object, Object) (Line: 73)
    Drupal\Core\Controller\FormController->getContentResult(Object, Object)
    call_user_func_array(Array, Array) (Line: 123)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 592)
    Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 181)
    Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 76)
    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: 106)
    Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
    Drupal\page_cache\StackMiddleware\PageCache->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: 51)
    Drupal\Core\StackMiddleware\StackedHttpKernel->handle(Object, 1, 1) (Line: 704)
    Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
  • I have never done a patch before on an issue before but this works for me:

        if ($is_allowed) {
          $source_field_definition = $media_type->getSource()->getSourceFieldDefinition($media_type);
          if ($source_field_definition){
            if ($source_field_definition->getItemDefinition()->getClass()) {
              $item_class = $source_field_definition->getItemDefinition()->getClass();
              $is_allowed = is_a($item_class, FileItem::class, TRUE);
            }
          }
        }
    
Production build 0.69.0 2024