Problem/Motivation

The form hooks for entity type and bundles can cause a naming collision if the bundle is named 'type'.

Example:

After installing a new Drupal 8.8.x, I created a content type with the machine name type
When I tried to add content to the above content type I received WSOD.

In the error log, this is the message

Error: Call to undefined method Drupal\node\Entity\Node::getThirdPartySetting() in menu_ui_form_node_type_form_alter() (line 376 of /srv/bindings/3bbcd18b06c44304aea5aca03069c666/code/web/core/modules/menu_ui/menu_ui.module) #0 /srv/bindings/3bbcd18b06c44304aea5aca03069c666/code/web/core/lib/Drupal/Core/Extension/ModuleHandler.php(539): menu_ui_form_node_type_form_alter(Array, Object(Drupal\Core\Form\FormState), 'node_type_form') #1 /srv/bindings/3bbcd18b06c44304aea5aca03069c666/code/web/core/lib/Drupal/Core/Form/FormBuilder.php(835): Drupal\Core\Extension\ModuleHandler->alter('form', Array, Object(Drupal\Core\Form\FormState), 'node_type_form') #2 /srv/bindings/3bbcd18b06c44304aea5aca03069c666/code/web/modules/contrib/devel/webprofiler/src/Form/FormBuilderWrapper.php(29): Drupal\Core\Form\FormBuilder->prepareForm('node_type_form', Array, Object(Drupal\Core\Form\FormState)) #3 /srv/bindings/3bbcd18b06c44304aea5aca03069c666/code/web/core/lib/Drupal/Core/Form/FormBuilder.php(277): Drupal\webprofiler\Form\FormBuilderWrapper->prepareForm('node_type_form', Array, Object(Drupal\Core\Form\FormState)) #4 /srv/bindings/3bbcd18b06c44304aea5aca03069c666/code/web/core/lib/Drupal/Core/Entity/EntityFormBuilder.php(61): Drupal\Core\Form\FormBuilder->buildForm('node_type_form', Object(Drupal\Core\Form\FormState)) #5 /srv/bindings/3bbcd18b06c44304aea5aca03069c666/code/web/core/modules/node/src/Controller/NodeController.php(129): Drupal\Core\Entity\EntityFormBuilder->getForm(Object(Drupal\node\Entity\Node)) #6 [internal function]: Drupal\node\Controller\NodeController->add(Object(Drupal\node\Entity\NodeType)) #7 /srv/bindings/3bbcd18b06c44304aea5aca03069c666/code/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(123): call_user_func_array(Array, Array) #8 /srv/bindings/3bbcd18b06c44304aea5aca03069c666/code/web/core/lib/Drupal/Core/Render/Renderer.php(582): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() #9 /srv/bindings/3bbcd18b06c44304aea5aca03069c666/code/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(124): Drupal\Core\Render\Renderer->executeInRenderContext(Object(Drupal\Core\Render\RenderContext), Object(Closure)) #10 /srv/bindings/3bbcd18b06c44304aea5aca03069c666/code/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(97): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) #11 /srv/bindings/3bbcd18b06c44304aea5aca03069c666/code/vendor/symfony/http-kernel/HttpKernel.php(151): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() #12 /srv/bindings/3bbcd18b06c44304aea5aca03069c666/code/vendor/symfony/http-kernel/HttpKernel.php(68): Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object(Symfony\Component\HttpFoundation\Request), 1) #13 /srv/bindings/3bbcd18b06c44304aea5aca03069c666/code/web/core/lib/Drupal/Core/StackMiddleware/Session.php(57): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #14 /srv/bindings/3bbcd18b06c44304aea5aca03069c666/code/web/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(47): Drupal\Core\StackMiddleware\Session->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #15 /srv/bindings/3bbcd18b06c44304aea5aca03069c666/code/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(106): Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #16 /srv/bindings/3bbcd18b06c44304aea5aca03069c666/code/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(85): Drupal\page_cache\StackMiddleware\PageCache->pass(Object(Symfony\Component\HttpFoundation\Request), 1, true) #17 /srv/bindings/3bbcd18b06c44304aea5aca03069c666/code/web/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(47): Drupal\page_cache\StackMiddleware\PageCache->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #18 /srv/bindings/3bbcd18b06c44304aea5aca03069c666/code/web/modules/contrib/devel/webprofiler/src/StackMiddleware/WebprofilerMiddleware.php(38): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #19 /srv/bindings/3bbcd18b06c44304aea5aca03069c666/code/web/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(52): Drupal\webprofiler\StackMiddleware\WebprofilerMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #20 /srv/bindings/3bbcd18b06c44304aea5aca03069c666/code/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #21 /srv/bindings/3bbcd18b06c44304aea5aca03069c666/code/web/core/lib/Drupal/Core/DrupalKernel.php(693): Stack\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #22 /srv/bindings/3bbcd18b06c44304aea5aca03069c666/code/web/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request)) #23 {main}.

Proposed resolution

Steps to replicate

  1. Install a new Drupal 8.8.x
  2. Create content type with machine name type
  3. Create new content of the content type type

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Comments

sjshaal created an issue. See original summary.

sjshaal’s picture

shaal’s picture

I can confirm that bug exist.
I was able to replicate it by following the steps suggested.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

johnwebdev’s picture

Component: menu_ui.module » entity system

This is happening because of naming collision.

For entity forms the form Id is constructed by:

Entity type Id
Bundle (if applicable)
Operation (if operation is default, it does not append)

Menu UI module alters the node type (content type) form, and since your content type is called 'type' that hook will kick in.

This could also potentially happen to Media, if you were to add a bundle called 'type'.

johnwebdev’s picture

Issue summary: View changes
berdir’s picture

Well, this is ugly.

There's not much we can do about the form id conflict, so the only non-crazy-complex solution might be to disallow creating an entity type with that name?

fcobbaert’s picture

I have added a check to disallow the creation of a content type named 'type'. As johndevman suggested this could also be an issue if you were to create a media type, I checked this but it doesn't seem to be an issue there.

berdir’s picture

Well, it's not an immediate error, but the conflict exists there as well.

johnwebdev’s picture

I am wondering if doing this change on the entity form is enough, if entity bundles may be created through i.e. JSON:API or REST this would be bypassed(?)

Maybe a entity constraint?

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

cleverhoods’s picture

As mentioned by @johnwebdev above, the issue was a name collision.

Added a check to make sure the code operates with the expected instance.

  /** @var \Drupal\node\NodeTypeInterface $type */
  $type = $form_state->getFormObject()->getEntity();
  if (!$type instanceof NodeTypeInterface) {
    return;
  }
anandhi karnan’s picture

I have tested the provided patch #18 and can confirm that it resolves the issue as described. The problem no longer occurs in Drupal 11.x
Thank you for the patch!

tolstoydotcom’s picture

Isn't this a case of "it hurts when I do that/so stop doing that"? I don't know if it's still an issue, but having a theme and a module with the same machine name also resulted in conflicts. Is there a check for that now? Not everything can be checked.

Maybe this could just be put into documentation.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.