Commit 2581f124 authored by Yash Rode's avatar Yash Rode Committed by Bálint Nagy
Browse files

Issue #3274968 by yash.rode, huzooka: Migrate Chosen from D7 to D9

parent 794abad4
Loading
Loading
Loading
Loading
+50 −0
Original line number Diff line number Diff line
@@ -7,10 +7,13 @@
 * @see https://harvesthq.github.com/chosen/
 */

use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\migrate\Exception\RequirementsException;
use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;

/**
 * Define chosen library url.
@@ -150,3 +153,50 @@ function chosen_help($route_name, RouteMatchInterface $route_match) {
  }

}

/**
 * Implements hook_migration_plugins_alter().
 */
function chosen_migration_plugins_alter(array &$migrations) {
  // Check if the module is enabled on source site.
  try {
    $variable_source = \Drupal::service('plugin.manager.migration')
      ->createStubMigration([
        'id' => 'foo',
        'idMap' => ['plugin' => 'null'],
        'source' => [
          'plugin' => 'variable',
          'ignore_map' => TRUE,
        ],
        'destination' => ['plugin' => 'null'],
      ])
      ->getSourcePlugin();
    if (!$variable_source instanceof DrupalSqlBase) {
      return;
    }
    $variable_source->checkRequirements();
  }
  catch (PluginException $e) {
    // The 'variable' source plugin isn't available because Migrate Drupal
    // isn't enabled. There is nothing we can do.
    return;
  }
  catch (RequirementsException $e) {
    // The source database is not a Drupal 7 database.
    return;
  }
  $system_data = $variable_source->getSystemData();
  if (empty($system_data['module']['chosen']['status'])) {
    unset($migrations['d7_chosen_settings']);
    return;
  }
  $chosen_migration = array_filter(
    $migrations,
    function ($definition) {
      return $definition['id'] === 'd7_field_instance_widget_settings';
    }
  );
  foreach (array_keys($chosen_migration) as $plugin_id) {
    $migrations[$plugin_id]['process']['options/type'][] = ['plugin' => 'chosen'];
  }
}
+90 −0
Original line number Diff line number Diff line
id: d7_chosen_settings
label: Chosen settings
migration_tags:
  - Drupal 7
  - Configuration
source:
  plugin: variable
  variables:
    - chosen_minimum_single
    - chosen_minimum_multiple
    - chosen_disable_search_threshold
    - chosen_minimum_width
    - chosen_jquery_selector
    - chosen_search_contains
    - chosen_disable_search
    - chosen_include
    - chosen_allow_single_deselect
    - chosen_placeholder_text_multiple
    - chosen_placeholder_text_single
    - chosen_no_results_text
    - chosen_disabled_themes
  source_module: chosen
process:
  minimum_single:
    plugin: default_value
    source: chosen_minimum_single
    strict: true
    default_value: 20
  minimum_multiple:
    plugin: default_value
    source: chosen_minimum_multiple
    strict: true
    default_value: 20
  disable_search_threshold:
    plugin: default_value
    source: chosen_disable_search_threshold
    strict: true
    default_value: 0
  minimum_width:
    plugin: default_value
    source: chosen_minimum_width
    strict: true
    default_value: ''
  jquery_selector:
    plugin: default_value
    source: chosen_jquery_selector
    strict: true
    default_value: 'select:visible'
  search_contains:
    plugin: default_value
    source: chosen_search_contains
    strict: true
    default_value: FALSE
  disable_search:
    plugin: default_value
    source: chosen_disable_search
    strict: true
    default_value: FALSE
  chosen_include:
    plugin: default_value
    source: chosen_include
    strict: true
    default_value: 2
  allow_single_deselect:
    plugin: default_value
    source: chosen_allow_single_deselect
    strict: true
    default_value: FALSE
  placeholder_text_multiple:
    plugin: default_value
    source: chosen_placeholder_text_multiple
    strict: true
    default_value: 'Choose some options'
  placeholder_text_single:
    plugin: default_value
    source: chosen_placeholder_text_single
    strict: true
    default_value: 'No results match'
  no_results_text:
    plugin: default_value
    source: chosen_no_results_text
    strict: true
    default_value: 'No results match'
  disabled_themes:
    plugin: default_value
    source: chosen_disabled_themes
    default_value: []
destination:
  plugin: config
  config_name: 'chosen.settings'
+29 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\chosen\Plugin\migrate\process;

use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;

/**
 * Changes widget type based on source field's widget type.
 *
 * @MigrateProcessPlugin(
 *   id = "chosen",
 *   handle_multiples = TRUE
 * )
 */
class Chosen extends ProcessPluginBase {

  /**
   * {@inheritdoc}
   */
  public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
    if ($value !== 'options_select' || empty($row->getSourceProperty('widget/settings/apply_chosen'))) {
      return $value;
    }
    return 'chosen_select';
  }

}
+20 −0
Original line number Diff line number Diff line
<?php
// phpcs:ignoreFile
/**
 * @file
 * A database agnostic dump for testing purposes.
 *
 * This file was generated by the Drupal 9.2.6 db-tools.php script.
 */

use Drupal\Core\Database\Database;

$connection = Database::getConnection();

include 'drupal7' . DIRECTORY_SEPARATOR . 'field_config.php';
include 'drupal7' . DIRECTORY_SEPARATOR . 'field_config_instance.php';
include 'drupal7' . DIRECTORY_SEPARATOR . 'node.php';
include 'drupal7' . DIRECTORY_SEPARATOR . 'node_type.php';
include 'drupal7' . DIRECTORY_SEPARATOR . 'system.php';
include 'drupal7' . DIRECTORY_SEPARATOR . 'taxonomy_vocabulary.php';
include 'drupal7' . DIRECTORY_SEPARATOR . 'variable.php';
+153 −0
Original line number Diff line number Diff line
<?php
// phpcs:ignoreFile
/**
 * @file
 * A database agnostic dump for testing purposes.
 *
 * This file was generated by the Drupal 9.2.6 db-tools.php script.
 */

use Drupal\Core\Database\Database;

$connection = Database::getConnection();

$connection->schema()->createTable('field_config', array(
  'fields' => array(
    'id' => array(
      'type' => 'serial',
      'not null' => TRUE,
      'size' => 'normal',
    ),
    'field_name' => array(
      'type' => 'varchar',
      'not null' => TRUE,
      'length' => '32',
    ),
    'type' => array(
      'type' => 'varchar',
      'not null' => TRUE,
      'length' => '128',
    ),
    'module' => array(
      'type' => 'varchar',
      'not null' => TRUE,
      'length' => '128',
      'default' => '',
    ),
    'active' => array(
      'type' => 'int',
      'not null' => TRUE,
      'size' => 'tiny',
      'default' => '0',
    ),
    'storage_type' => array(
      'type' => 'varchar',
      'not null' => TRUE,
      'length' => '128',
    ),
    'storage_module' => array(
      'type' => 'varchar',
      'not null' => TRUE,
      'length' => '128',
      'default' => '',
    ),
    'storage_active' => array(
      'type' => 'int',
      'not null' => TRUE,
      'size' => 'tiny',
      'default' => '0',
    ),
    'locked' => array(
      'type' => 'int',
      'not null' => TRUE,
      'size' => 'tiny',
      'default' => '0',
    ),
    'data' => array(
      'type' => 'blob',
      'not null' => TRUE,
      'size' => 'big',
    ),
    'cardinality' => array(
      'type' => 'int',
      'not null' => TRUE,
      'size' => 'tiny',
      'default' => '0',
    ),
    'translatable' => array(
      'type' => 'int',
      'not null' => TRUE,
      'size' => 'tiny',
      'default' => '0',
    ),
    'deleted' => array(
      'type' => 'int',
      'not null' => TRUE,
      'size' => 'tiny',
      'default' => '0',
    ),
  ),
  'primary key' => array(
    'id',
  ),
  'indexes' => array(
    'field_name' => array(
      'field_name',
    ),
    'active' => array(
      'active',
    ),
    'storage_active' => array(
      'storage_active',
    ),
    'deleted' => array(
      'deleted',
    ),
    'module' => array(
      'module',
    ),
    'storage_module' => array(
      'storage_module',
    ),
    'type' => array(
      'type',
    ),
    'storage_type' => array(
      'storage_type',
    ),
  ),
  'mysql_character_set' => 'utf8mb3',
));

$connection->insert('field_config')
->fields(array(
  'id',
  'field_name',
  'type',
  'module',
  'active',
  'storage_type',
  'storage_module',
  'storage_active',
  'locked',
  'data',
  'cardinality',
  'translatable',
  'deleted',
))
->values(array(
  'id' => '15',
  'field_name' => 'field_car_type',
  'type' => 'list_text',
  'module' => 'list',
  'active' => '1',
  'storage_type' => 'field_sql_storage',
  'storage_module' => 'field_sql_storage',
  'storage_active' => '1',
  'locked' => '0',
  'data' => 'a:7:{s:12:"translatable";s:1:"0";s:12:"entity_types";a:0:{}s:8:"settings";a:2:{s:14:"allowed_values";a:3:{s:3:"red";s:3:"red";s:4:"blue";s:4:"blue";s:5:"green";s:5:"green";}s:23:"allowed_values_function";s:0:"";}s:7:"storage";a:5:{s:4:"type";s:17:"field_sql_storage";s:8:"settings";a:0:{}s:6:"module";s:17:"field_sql_storage";s:6:"active";s:1:"1";s:7:"details";a:1:{s:3:"sql";a:2:{s:18:"FIELD_LOAD_CURRENT";a:1:{s:25:"field_data_field_car_type";a:1:{s:5:"value";s:20:"field_car_type_value";}}s:19:"FIELD_LOAD_REVISION";a:1:{s:29:"field_revision_field_car_type";a:1:{s:5:"value";s:20:"field_car_type_value";}}}}}s:12:"foreign keys";a:0:{}s:7:"indexes";a:1:{s:5:"value";a:1:{i:0;s:5:"value";}}s:2:"id";s:2:"15";}',
  'cardinality' => '2',
  'translatable' => '0',
  'deleted' => '0',
))
->execute();
Loading