Problem/Motivation

While adding a new dependent field (not while editing existing field), after selecting the Make field dependent using views option, the expected form is not loading and we get


Error message
Oops, something went wrong. Check your browser's developer console for more details. 

In the browser's console, we get this message.

{
  "message": "\nAn AJAX HTTP error occurred.\nHTTP Result Code: 500\nDebugging information follows.\nPath: /admin/structure/types/manage/article/add-field/node/field_xxxx?js=true&_drupal_ajax=1&ajax_page_state%5Btheme%5D=claro&ajax_page_state%5Btheme_token%5D=KkpfwMiChINqUNOw1NTLmd4GpIO42s696NzLm-Y5SDc&ajax_page_state%5Blibraries%5D=eJx1kGFuwzAIhS9kJ2fYSSLs0JSOgIdx1e7089ZYUybtH-99z-iZRNtSqOCcjiFkBtN5tVaAJ4F7dEj1cDfWBByrP5lkC1nF8eENeOR_neiqnMB6yHBgyE53jP3x-8lfCVi3CW7wOPumJTV3lZO9Q76SYBTY8QQMK3qEUhAMJJ9h_wdjTxSV2lu8GPW-Jp3ePhrac7qo7S8ifQKmTwwXQl6XRmPR0P-CXlBgw-VH11Cvap6bj9jQ4TjRPE71R09YMxR8W3eS0CraWPA9T9RvXb8AvxOsww\nStatusText: error\nResponseText: The website encountered an unexpected error. Try again later.Error: Call to a member function getTargetEntityTypeId() on null in Drupal\\dependent_fields\\Plugin\\EntityReferenceSelection\\ViewsSelection->buildConfigurationForm() (line 492 of modules/contrib/dependent_fields/src/Plugin/EntityReferenceSelection/ViewsSelection.php). Drupal\\Core\\Field\\Plugin\\Field\\FieldType\\EntityReferenceItem->fieldSettingsForm(Array, Object) (Line: 188)\nDrupal\\field_ui\\Form\\FieldConfigEditForm->form(Array, Object) (Line: 108)\nDrupal\\Core\\Entity\\EntityForm->buildForm(Array, Object)\ncall_user_func_array(Array, Array) (Line: 554)\nDrupal\\Core\\Form\\FormBuilder->retrieveForm('field_config_edit_form', Object) (Line: 396)\nDrupal\\Core\\Form\\FormBuilder->rebuildForm('field_config_edit_form', Object, Array) (Line: 651)\nDrupal\\Core\\Form\\FormBuilder->processForm('field_config_edit_form', Array, Object) (Line: 347)\nDrupal\\Core\\Form\\FormBuilder->buildForm(Object, Object) (Line: 48)\nDrupal\\Core\\Entity\\EntityFormBuilder->getForm(Object, 'default', Array) (Line: 62)\nDrupal\\field_ui\\Controller\\FieldConfigAddController->fieldConfigAddConfigureForm('node', 'field_xxxx')\ncall_user_func_array(Array, Array) (Line: 123)\nDrupal\\Core\\EventSubscriber\\EarlyRenderingControllerWrapperSubscriber->{closure:Drupal\\Core\\EventSubscriber\\EarlyRenderingControllerWrapperSubscriber::wrapControllerExecutionInRenderContext():121}() (Line: 634)\nDrupal\\Core\\Render\\Renderer::{closure:Drupal\\Core\\Render\\Renderer::executeInRenderContext():634}()\nFiber->start() (Line: 635)\nDrupal\\Core\\Render\\Renderer->executeInRenderContext(Object, Object) (Line: 121)\nDrupal\\Core\\EventSubscriber\\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)\nDrupal\\Core\\EventSubscriber\\EarlyRenderingControllerWrapperSubscriber->{closure:Drupal\\Core\\EventSubscriber\\EarlyRenderingControllerWrapperSubscriber::onController():96}() (Line: 183)\nSymfony\\Component\\HttpKernel\\HttpKernel->handleRaw(Object, 1) (Line: 76)\nSymfony\\Component\\HttpKernel\\HttpKernel->handle(Object, 1, 1) (Line: 53)\nDrupal\\Core\\StackMiddleware\\Session->handle(Object, 1, 1) (Line: 48)\nDrupal\\Core\\StackMiddleware\\KernelPreHandle->handle(Object, 1, 1) (Line: 28)\nDrupal\\Core\\StackMiddleware\\ContentLength->handle(Object, 1, 1) (Line: 32)\nDrupal\\big_pipe\\StackMiddleware\\ContentLength->handle(Object, 1, 1) (Line: 118)\nDrupal\\page_cache\\StackMiddleware\\PageCache->pass(Object, 1, 1) (Line: 92)\nDrupal\\page_cache\\StackMiddleware\\PageCache->handle(Object, 1, 1) (Line: 48)\nDrupal\\Core\\StackMiddleware\\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)\nDrupal\\Core\\StackMiddleware\\NegotiationMiddleware->handle(Object, 1, 1) (Line: 53)\nDrupal\\Core\\StackMiddleware\\AjaxPageState->handle(Object, 1, 1) (Line: 54)\nDrupal\\Core\\StackMiddleware\\StackedHttpKernel->handle(Object, 1, 1) (Line: 745)\nDrupal\\Core\\DrupalKernel->handle(Object) (Line: 19)\n",
  "name": "AjaxError"
}

Steps to reproduce

1. Create an Entity Reference view. The error happens only if there is at least one Entity Reference view.
2. Then go to the content type's Manage Fields tab and add a new Reference field.
3. In the Config form of the new Reference Field, select Make field dependent using views option.
4. The expected form is not loaded and the browser's console shows error.

Proposed resolution

The issue seems to be coming from this line inside buildConfigurationForm function:

  $fields = $this->getBundleEditableFields($field_config->getTargetEntityTypeId(), $field_config->getTargetBundle());

The $field_config is null because the following is returning null.

  $field_config = $this->requestStack->getCurrentRequest()->get('field_config');

So, to fix this, we can use some alternative way of fetching $field_config. Since we are inside a Form, we can do this instead:

  $field_config = $form_state->getFormObject()->getEntity();
Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

sandeshyadav created an issue. See original summary.

sandeshyadav’s picture

Issue summary: View changes
Status: Active » Needs review

pcambra made their first commit to this issue’s fork.

pcambra’s picture

Status: Needs review » Fixed

Makes sense, thanks!

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

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