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 : 

 

  1. use Drupal\Core\Database\Database;
  2.  
  3. /**
  4.  * Update field_communiques_texte_wysiwig type
  5.  */
  6. function my_module_update_8008() {
  7.   // Set up date to add the format column
  8.   $table = 'field_communiques_texte_wysiwig';
  9.   $column = 'format';
  10.   $field = [
  11.     'type' => 'varchar_ascii',
  12.     'length' => 255,
  13.   ];
  14.   $schema = Database::getConnection()->schema();
  15.  
  16.   // Update the data table
  17.   $schema->addField('node__' . $table, $table . '_' . $column, $field);
  18.   $schema->addIndex('node__' . $table, $table . '_' . $column, [$table . '_' . $column], [
  19.     'fields' => [$table . '_' . $column => $field],
  20.   ]);
  21.   // The revision table
  22.   $schema->addField('node_revision__' . $table, $table . '_' . $column, $field);
  23.   $schema->addIndex('node_revision__' . $table, $table . '_' . $column, [$table . '_' . $column], [
  24.     'fields' => [$table . '_' . $column => $field],
  25.   ]);
  26.  
  27.   // Force the current configuration to be exactly like the YAML,
  28.   // so that the subsequent import does not detect a change
  29.   $config = \Drupal::configFactory()
  30.     ->getEditable('field.storage.node.field_communiques_texte_wysiwig');
  31.   $depends = $config->get('dependencies');
  32.   $depends['module'][] = 'text';
  33.   $config->set('dependencies', $depends);
  34.   $config->set('type', 'text_long');
  35.   $config->set('settings', []);
  36.   $config->set('module', 'text');
  37.   $config->save();
  38.  
  39.   // Current node field configurations
  40.   $field_manager = \Drupal::getContainer()->get('entity_field.manager');
  41.   // Because the manager was already loaded before the above config was forced,
  42.   // it will return the old configuration that was cached
  43.   $field_manager->clearCachedFieldDefinitions();
  44.   $field_storage_configs = $field_manager->getFieldStorageDefinitions('node');
  45.  
  46.   // Get the last installed manager, this is the gatekeeper that determines if
  47.   // an update is needed or can be done
  48.   $last_installed_repo = \Drupal::getContainer()
  49.     ->get('entity.last_installed_schema.repository');
  50.  
  51.   // Get the last installed configurations for node fields
  52.   // These are iterative objects and need to stored as such, not just simple arrays,
  53.   // so reusing the previously set configs is not an option
  54.   $last_installed_configs = $last_installed_repo->getLastInstalledFieldStorageDefinitions('node');
  55.  
  56.   // Force the last installed config to be the current for the field
  57.   $last_installed_configs['field_communiques_texte_wysiwig'] = $field_storage_configs['field_communiques_texte_wysiwig'];
  58.   $last_installed_repo->setLastInstalledFieldStorageDefinitions('node', $last_installed_configs);
  59. }
Drupal

Ajouter un commentaire

Image CAPTCHA
Saisir les caractères affichés dans l'image.