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:
- The index variables in the foreach statement are never used in their code blocks
- The result of the EntityInterface::getEntityTypeId() and EntityInterface::getId() methods already return strings so there's no need to cast them.
- 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.