Deprecate NodeStorage::updateType and remove type change handling in NodeType::postSave

Created on 1 March 2022, about 3 years ago
Updated 20 March 2025, 21 days ago

Problem/Motivation

@zrpnr and I stumbled across this enticing function: node_type_update_nodes

It promises to "[update] all nodes of one type to be of another type"! Amazing! And it's so simple too. With a single line of code, it calls a single method on the NodeStorage class:

  public function updateType($old_type, $new_type) {
    return $this->database->update($this->getBaseTable())
      ->fields(['type' => $new_type])
      ->condition('type', $old_type)
      ->execute();
  }

Wow. Do you mean that in one fell swoop I could change the bundle ID of every node in my installation to a new ID? That sounds safe.

:thinking_face: where is this magical method used? NodeType::postSave of course:

    if ($update && $this->getOriginalId() != $this->id()) {
      $update_count = node_type_update_nodes($this->getOriginalId(), $this->id());
      if ($update_count) {
        \Drupal::messenger()->addStatus(\Drupal::translation()->formatPlural($update_count,
          'Changed the content type of 1 post from %old-type to %type.',
          'Changed the content type of @count posts from %old-type to %type.',
          [
            '%old-type' => $this->getOriginalId(),
            '%type' => $this->id(),
          ]));
      }
    }

Huh, I've never noticed that in the UI before. Can I really edit the machine name of a content type? Shoot. Apparently not:

Who, whaaa.... why then?! Where did this mysterious uber-method come from? Apparently, from the who's who list of the Drupal community: https://git.drupalcode.org/project/drupal/-/blame/9.4.x/core/modules/nod...

16 years of modifications and refactoring unearthed this afternoon.

AFAICT, there's no real reason for it to exist. We should probably remove it. Lest some poor wandering soul stumble across this promising function... and bork their site beyond repair.

** :D Okay in all seriousness, this appears to be dead code. I can't imagine a reason why anyone would be using it. If they did, they would surely be in for a nasty surprise. At the risk of damning this issue to eternal limbo... it seems like an easy fix.

Proposed resolution

Delete this dangerous function from node.module and its counterpart: NodeStorage::updateType

API changes

This function and method would be gone.

Data model changes

None.

Release notes snippet

A foot gun has been removed.

πŸ“Œ Task
Status

Active

Version

11.0 πŸ”₯

Component

node system

Created by

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