You are here

public function YamlFormToWebformMigrateManager::migrate in YAML Form 8

Migrate the YAML Form module's configuration and data to the Webform module.

Return value

array An associative array containing status messages.

Overrides YamlFormToWebformMigrateManagerInterface::migrate

File

modules/yamlform_to_webform/src/YamlFormToWebformMigrateManager.php, line 182

Class

YamlFormToWebformMigrateManager
Defines the YAML Form to Webform migrate manager.

Namespace

Drupal\yamlform_to_webform

Code

public function migrate() {

  // Store maintenance_mode setting so we can restore it when done.
  $maintenance_mode = \Drupal::state()
    ->get('system.maintenance_mode');

  // Force site into maintenance_mode.
  \Drupal::state()
    ->set('system.maintenance_mode', TRUE);

  // Move uploaded files from 'yamlform' to 'webform' directory.
  $stream_wrappers = \Drupal::service('stream_wrapper_manager')
    ->getNames(StreamWrapperInterface::WRITE_VISIBLE);
  foreach ($stream_wrappers as $uri_schema => $stream_wrapper) {
    if (file_exists("{$uri_schema}://webform")) {
      file_unmanaged_delete_recursive("{$uri_schema}://webform");
    }
    if (file_exists("{$uri_schema}://yamlform")) {
      file_unmanaged_move("{$uri_schema}://yamlform", "{$uri_schema}://webform", FILE_EXISTS_REPLACE);
    }
  }

  // Manually fix the YAML Form node module's configuration.
  if (\Drupal::moduleHandler()
    ->moduleExists('yamlform_node')) {
    $this->configFactory
      ->getEditable('node.type.yamlform')
      ->set('name', 'Webform')
      ->set('type', 'webform')
      ->set('description', 'A basic page with a webform attached.')
      ->save();
    $this->configFactory
      ->getEditable('field.field.node.yamlform.yamlform')
      ->set('label', 'Webform')
      ->save();
  }

  // Manually uninstall the yamlform_to_webform.module.
  $config = $this->configFactory
    ->getEditable('core.extension');
  $config
    ->clear('module.yamlform_to_webform');
  $config
    ->save();
  $this->connection
    ->query("DELETE FROM {key_value} WHERE name='yamlform_to_webform'");

  // Set webform module schema to 8006.
  $this->connection
    ->query("UPDATE {key_value} SET value=:value WHERE collection = 'system.schema' AND name='yamlform'", [
    ':value' => 'i:8006;',
  ]);

  // Reset webform sub module schemas to 8000.
  $this->connection
    ->query("UPDATE {key_value} SET value=:value WHERE collection = 'system.schema' AND name LIKE 'yamlform_%'", [
    ':value' => 'i:8000;',
  ]);

  // Rename database tables, indexes, and columns.
  $messages = [];
  $tables = $this
    ->getTables();
  foreach ($tables as $table_name) {
    $messages += $this
      ->renameColumns($table_name);
    $messages += $this
      ->renameIndexes($table_name);
    $messages += $this
      ->renameTable($table_name);
  }
  ksort($messages);

  // Restore maintenance_mode state.
  \Drupal::state()
    ->set('system.maintenance_mode', $maintenance_mode);
  return $messages;
}