Error: Call to a member function getEntityTypeId() on null

Created on 7 November 2019, about 5 years ago
Updated 26 November 2024, 2 months ago

Problem/Motivation

After upgrading to the latest tagged release (2.14) we are experiencing the following fatal error when viewing the translation job form:

he website encountered an unexpected error. Please try again later.
Error: Call to a member function getEntityTypeId() on null in Drupal\lingotek\Form\LingotekJobManagementContentEntitiesForm->getRows() (line 163 of modules/contrib/lingotek/src/Form/LingotekJobManagementContentEntitiesForm.php).
Drupal\lingotek\Form\LingotekJobManagementContentEntitiesForm->getRows(Array) (Line: 326)
Drupal\lingotek\Form\LingotekManagementFormBase->buildForm(Array, Object) (Line: 88)
Drupal\lingotek\Form\LingotekJobManagementContentEntitiesForm->buildForm(Array, Object, 'japanese-project')
call_user_func_array(Array, Array) (Line: 519)
Drupal\Core\Form\FormBuilder->retrieveForm('lingotek_job_content_entities_management', Object) (Line: 276)
Drupal\Core\Form\FormBuilder->buildForm(Object, Object) (Line: 93)
Drupal\Core\Controller\FormController->getContentResult(Object, Object)
call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 582)
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: 151)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 68)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
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: 49)
Asm89\Stack\Cors->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 52)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 693)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

Here's the offending method:

  protected function getRows($entity_list) {
    $counter = 1;
    $rows = [];
    foreach ($entity_list as $entity_type_id => $entities) {
      foreach ($entities as $entity_id => $entity) {
        $rowId = (string) $entity->getEntityTypeId() . ':' . (String) $entity->id();
        $rows[$rowId] = $this->getRow($entity);
        $counter += 1;
      }
    }
    return $rows;
  }

There's a couple issues with this code:

  1. The index variables in the foreach statement are never used in their code blocks
  2. The result of the EntityInterface::getEntityTypeId() and EntityInterface::getId() methods already return strings so there's no need to cast them.
  3. There is an assumption that the entities in the $entities array are indeed entity objects, but the list of entities are gathered from the Lingotek metadata which is configuration that could include malformed data.

Proposed resolution

Patch will be included but we should add a guard clause in the inner most foreach block to check the instance of $entity if it's EntityInterface. Using the proposed patch has fixed our jobs page.

Remaining tasks

Community review is needed.

๐Ÿ› Bug report
Status

Needs work

Version

2.0

Component

Code

Created by

๐Ÿ‡บ๐Ÿ‡ธUnited States nathandentzau Rhode Island

Live updates comments and jobs are added and updated live.
  • Needs reroll

    The patch will have to be re-rolled with new suggestions/changes described in the comments in the issue.

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.

Production build 0.71.5 2024