4

I have this content-type that i created titled: "Engineering".

1500 hundred nodes later management wants me to rename it : "Staff". I can easily rename the label but how do i rename the machine name? Does anyone have any success renaming a content-type machine name that is loaded with content/revision and relationships?

It is sometimes impossible to predict these kind of changes. How do we rename the machine name a a content-type.

i have tried the instructions from this article below : https://medium.com/@philw_/how-to-change-a-content-type-s-machine-name-in-drupal-8-33fc3c0520dc with no success.

Error: bundle name cannot be changed.

I will be trying the following:

export the database out. find and replace the old content-type machine name with the new name I will also rename any tables named after the content type in the process re-import the db I will see how that works out.

5
  • 1
    The question is, is it really necessary to rename the machine name? Rename the label, management happy, move on.
    – leymannx
    Commented Jun 20, 2018 at 21:09
  • 4
    If you simply change the label no one is going to know the difference. Renaming machine names is a pain in the ass. You can’t predict changes to machine names because you shouldn’t have to worry about it. The content model is the model until the end. So, just change the label.
    – Kevin
    Commented Jun 20, 2018 at 21:14
  • 3
    The whole point of using IDs (machine name) is so that you can change the label, and not have to rewrite all your code.
    – Jaypan
    Commented Jun 20, 2018 at 21:41
  • Guys, is it safe to say that we should probably use generic name for our content types such as ct1, ct2, ct100 and so on and change the label to be whatever. It could make it less confusing in the long run since i have custom modules and theme files that depends on these machine names. It will become a management nightmare for the next guys that come after me... FYI: I did change the label and everything works fine i just have to change a couple of theme filenames and check some modules but it feels dirty. I will try the DB route and update this answer...
    – winteck
    Commented Jun 20, 2018 at 21:41
  • 1
    Absolutely not. The machine names should be logical and legible and clear at the time the content models are conceived. They can't "just change" because someone in management now thinks they're some genius. NEVER sacrifice clarity for this. And don't walk into the nightmare of trying to change and migrate machine names - its simply not worth it NOR is it necessary to do. Whats more clear? Code all over the module and theme space that looks at "engineering" content type to do EFQs or preprocessing, or code that makes references to stuff like "ct100" which means absolutely nothing?
    – Kevin
    Commented Jun 20, 2018 at 22:57

3 Answers 3

3

I disagree with most of the comments above simply stating: don't change the bundle machine name. These don't answer the poster's question.

This was a simple task in Drupal 7; but due to the added complexities in Drupal 8 it was decided to attempt to remove this ability. See this issue for history:

#2172843 Remove ability to update entity bundle machine names.

That being said, if you do a database search/replace on the machine name this should do what you are looking for - BUT beware, some of those machine names are embedded in serialized data (e.g. key_value table) and you will need to modify the json string size if the machine name you are replacing is a different length than the new name.

0

The Content Type Clone module allows you to copy a content type to a new content type (with a new name), and optionally copy or move nodes from the old type to the new one:

https://www.drupal.org/project/content_type_clone

-2
  1. Leave the machine_name intact
  2. Create path alias wherever outdated machine_name is exposed to the end user via the URL.
  3. Define custom const in a custom module - name it after "new" machine_name and set the value to the old machine_name. Use new const in the code as a replacement for old, incorrect machine_name.
1
  • I like to the "Const" method, that will at least give me a place to look for reference.
    – winteck
    Commented Jun 20, 2018 at 22:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.