Error: Call to private method in entity_usage_form_alter()

Created on 25 January 2022, almost 3 years ago
Updated 24 July 2024, 6 months ago

Problem/Motivation

Entity usage module implements itself to ANY form object providing a getEntity method. However, it doesn't check whether the method is actually callable.

For instance, the Structured Data โ†’ module implements a private getEntity() method, which is completely normal.

However this private method would have Entity Usage module to fail:

The website encountered an unexpected error. Please try again later.
Error: Call to private method Drupal\structured_data\Form\PageJsonForm::getEntity() from context '' in entity_usage_form_alter() (line 82 of modules/contrib/entity_usage/entity_usage.module).

Steps to reproduce

  • Enable any module implementing a private or protected getEntity method.
  • Enable Entity Usage module
  • Open any page that Structured Data form is shown (route: /structured_data/page/json/{sd_route_name}/{sd_url}/{sd_bundle}/{sd_entity_id})
  • Observe the error

Proposed resolution

Replace method_exists() function with is_callable()

Error

The website encountered an unexpected error. Please try again later.
Error: Call to private method Drupal\structured_data\Form\PageJsonForm::getEntity() from context '' in entity_usage_form_alter() (line 82 of modules/contrib/entity_usage/entity_usage.module).
entity_usage_form_alter(Array, Object, 'structured_data_page_json_form') (Line: 539)
Drupal\Core\Extension\ModuleHandler->alter('form', Array, Object, 'structured_data_page_json_form') (Line: 835)
Drupal\Core\Form\FormBuilder->prepareForm('structured_data_page_json_form', Array, Object) (Line: 279)
Drupal\Core\Form\FormBuilder->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: 564)
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: 158)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 80)
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: 41)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 708)
Drupal\Core\DrupalKernel->handle(Object) (Line: 55)
๐Ÿ› Bug report
Status

Needs work

Component

Code

Created by

๐Ÿ‡ฉ๐Ÿ‡ฐDenmark osman Copenhagen

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.

Production build 0.71.5 2024