Convertir un texte long en texte long formaté

 

Pour convertir un champ texte long (ici: field_communiques_texte_wysiwig) en texte log formaté, il suffit de créer un .install dans un module contenant l'exemple ci-dessous : 

 

use Drupal\Core\Database\Database;
/**
 * Update field_communiques_texte_wysiwig type
 */
function my_module_update_8008() {
  // Set up date to add the format column
  $table = 'field_communiques_texte_wysiwig';
  $column = 'format';
  $field = [
    'type' => 'varchar_ascii',
    'length' => 255,
  ];
  $schema = Database::getConnection()->schema();
  // Update the data table
  $schema->addField('node__' . $table, $table . '_' . $column, $field);
  $schema->addIndex('node__' . $table, $table . '_' . $column, [$table . '_' . $column], [
    'fields' => [$table . '_' . $column => $field],
  ]);
  // The revision table
  $schema->addField('node_revision__' . $table, $table . '_' . $column, $field);
  $schema->addIndex('node_revision__' . $table, $table . '_' . $column, [$table . '_' . $column], [
    'fields' => [$table . '_' . $column => $field],
  ]);
  // Force the current configuration to be exactly like the YAML,
  // so that the subsequent import does not detect a change
  $config = \Drupal::configFactory()
    ->getEditable('field.storage.node.field_communiques_texte_wysiwig');
  $depends = $config->get('dependencies');
  $depends['module'][] = 'text';
  $config->set('dependencies', $depends);
  $config->set('type', 'text_long');
  $config->set('settings', []);
  $config->set('module', 'text');
  $config->save();
  // Current node field configurations
  $field_manager = \Drupal::getContainer()->get('entity_field.manager');
  // Because the manager was already loaded before the above config was forced,
  // it will return the old configuration that was cached
  $field_manager->clearCachedFieldDefinitions();
  $field_storage_configs = $field_manager->getFieldStorageDefinitions('node');
  // Get the last installed manager, this is the gatekeeper that determines if
  // an update is needed or can be done
  $last_installed_repo = \Drupal::getContainer()
    ->get('entity.last_installed_schema.repository');
  // Get the last installed configurations for node fields
  // These are iterative objects and need to stored as such, not just simple arrays,
  // so reusing the previously set configs is not an option
  $last_installed_configs = $last_installed_repo->getLastInstalledFieldStorageDefinitions('node');
  // Force the last installed config to be the current for the field
  $last_installed_configs['field_communiques_texte_wysiwig'] = $field_storage_configs['field_communiques_texte_wysiwig'];
  $last_installed_repo->setLastInstalledFieldStorageDefinitions('node', $last_installed_configs);
}