Problem/Motivation

Steps to reproduce:

First part

  1. Spin up a fresh D8 HEAD site
  2. Create a new content type. Call it Test
  3. Along with the default body field, create a new field, by re-using the field_image field. Call it Image.
  4. Save settings. Your content type is ready.
  5. Go to /admin/reports/fields and confirm that body and field_image are used in the Test content type.
  6. Create a piece of content with the Test content type (/node/add/test).
  7. Go to /admin/structure/types/manage/test and rename the machine name from test to testing
  8. At /admin/reports/fields you can observe that our content type Test is no longer being displayed, but we still have a comma and what resembles a NULL value instead of our content type's name.

Second part

  1. Go to /admin/structure/types/manage/testing/fields/add-field
  2. Create a new field of type email, and give it the machine name field_email and label Email.
  3. Save settings.
  4. Go to /admin/structure/types/manage/testing and change the machine name again, but this time from testing to tested.
  5. Go to /admin/reports/fields and confirm the field_email field doesn't seem to be used anywhere.
  6. Go to /admin/structure/types/manage/tested/fields and delete field_email
  7. Go back to /admin/reports/fields and confirm our field_email field is still there but a) we never stored anything in it b) it's not attached to any content type anymore c) we have no obvious way to tell what it's doing here since we've deleted it anyway and nothing was ever stored in it.

Proposed resolution

Prevent site builders from changing a machine name. This is simply a stopgap to what could results in potentially dozens of useless fields and related issues with the data model we might not have anticipated yet.

Remaining tasks

Discuss options, update issue summary accordingly.

User interface changes

  • Site builders are no longer able to edit a content type's machine name
  • There's a warning or information box to alert site builders about picking a good machine name (show examples?) because they won't be able to change it afterwards.

API changes

None in this issue. Addressing API changes would be a follow-up.

Data model changes

None in this issue. Addressing data model changes would be a follow-up.

CommentFileSizeAuthor
#7 2575039-bundle_rename.patch2.83 KBfgm

Comments

anavarre created an issue. See original summary.

anavarre’s picture

Issue summary: View changes
catch’s picture

Priority: Major » Critical
Issue tags: +D8 upgrade path

Drupal 7 used to have https://api.drupal.org/api/drupal/modules%21field%21field.attach.inc/fun...

I have no idea what the 8.x equivalent is, but even if we have it, we're clearly not implementing it.

Bumping to critical since this is a clear data-loss issue. We can prevent the rename for now as a quick fix, then handle renames properly and remove the validation later IMO.

catch’s picture

Also need to check if this is an issue for taxonomy vocabularies or other entity types in core that have a bundle config entity.

fgm’s picture

Assigned: Unassigned » fgm

Working on this.

fgm’s picture

This hook was removed in #1374116: Move bundle CRUD API out of Field API where it was renamed hook_entity_bundle_rename(), not hook_entity_rename_bundle() as said on the issue.

fgm’s picture

Assigned: fgm » Unassigned
Status: Active » Needs review
StatusFileSize
new2.83 KB

The problem is that this hook is triggered by an entity::postSave(), so it happens too late to prevent the change, and a complete fix will have to be extensive, possibly involving an API change, so let's just do it in the entity type forms.

We have 4 entity types in core where this happens, not counting tests: node, taxonomy_term, shortcut, and block_content, with bundle edit forms. The latter two already have a form-based protection, so I replicated the same policy on the two former ones. Let's see how the tests fare.

fgm’s picture

Component: field system » entity system

Adjusting component.

Status: Needs review » Needs work

The last submitted patch, 7: 2575039-bundle_rename.patch, failed testing.

dawehner’s picture

Issue tags: +Need tests

Personally renaming bundles is a vulcano. It would require adaption all over the place.

Personally this is a feature that can be provided by contrib modules.

catch’s picture

We ought to be able to detect the change in save somewhere, and throw an exception there?

The last submitted patch, 7: 2575039-bundle_rename.patch, failed testing.

jhedstrom’s picture

--- a/core/modules/node/src/NodeTypeForm.php
+++ b/core/modules/node/src/NodeTypeForm.php

--- a/core/modules/shortcut/src/ShortcutSetForm.php
+++ b/core/modules/shortcut/src/ShortcutSetForm.php

--- a/core/modules/taxonomy/src/VocabularyForm.php
+++ b/core/modules/taxonomy/src/VocabularyForm.php

I haven't looked too far into how this might work in EntityForm (which all 3 of these extend), but it would be really great if we could generically dis-allow changing the machine name for any entity type with existing content (simply disallowing it at all after creation would be fine too, but would perhaps be frustrating for devs prone to typos :)

Edit: EntityForm is probably too generic, but perhaps a trait the given entity type forms could use?

The Book module tests for the ability to change machine name (as if that were a feature?), so a change notice will be needed I suppose:

  function testBookNodeTypeChange() {
    $this->drupalLogin($this->adminUser);
    // Change the name, machine name and description.
    $edit = array(
      'name' => 'Bar',
      'type' => 'bar',
    );
wim leers’s picture

Issue tags: -Need tests +Needs tests

We ought to be able to detect the change in save somewhere, and throw an exception there?

My thoughts exactly.

dawehner’s picture

So I think we want to have two things:

  • Something like BundleConfigEntityFormBase (Adds validation + the #disabled flag by default)
  • Maybe also add a similar base class for config entities itself and validate the renaming of the entity KEY there.

The current patch should at least document for the #disabled flags, why they are setup in this particular way.

fgm’s picture

I've been updating the path to catch the change in the storage layer, to avoid depending only on UI, as suggested by @catch. It doesn't prevent doing it in the UI to avoid exposing an otherwise unavailable rename operation, of course.

Question is: what to do about BookTest, for instance, which explicitly tests renaming a node bundle : do we just drop the test ?

catch’s picture

I think we can just drop the test for now yes, that hunk could move to the follow-up issue to add back the feature.

catch’s picture

Status: Needs work » Closed (duplicate)

Duplicate of #2172843: Remove ability to update entity bundle machine names which has unfortunately been around for a long time with a patch.