Reset langcode everywhere

A site I worked on had a mix of langcode und and en in content, paragraphs, and revisions. At some point, something got mismatched and corrupted. I wrote this script to reset everything to en.

 

<?php

/**
 * This fixes problems with mismatched langodes in the database
 * between nodes/paragraphs/revisions by setting all langcodes to en.
 *
 * It loops through all database tables, checks for a langcode column,
 * and sets all langcode values in the table to en.
 */

$tables = \Drupal::database()->query("show tables");

foreach ($tables->fetchCol() as $table) {
  $columns = \Drupal::database()->query("describe {$table}")->fetchCol();

  if (in_array('langcode', $columns)) {
    $update = \Drupal::database()->query("update {$table} set langcode = 'en'")->execute();
  }
}