Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Implements hook_install().
- */
- function skg_global_install() {
- // For allow run updates after module install.
- drupal_set_installed_schema_version('skg_global', '7000');
- }
- /**
- * Move sizes from list field to taxonomy vocabulary.
- */
- function skg_global_update_7001(&$sandbox) {
- if (!isset($sandbox['progress'])) {
- $fields = field_info_fields();
- $sizes_list_1 = list_allowed_values($fields['field_size']);
- $sizes_list_2 = list_allowed_values($fields['field_size2']);
- $sizes = $sizes_list_1 + $sizes_list_2;
- $size_vocabulary = new stdClass();
- $size_vocabulary->name = t('Sizes');
- $size_vocabulary->machine_name = 'sizes';
- $size_vocabulary->description = t('Available size options');
- taxonomy_vocabulary_save($size_vocabulary);
- $sandbox['sizes'] = $sizes;
- $sandbox['max'] = count($sizes);
- $sandbox['progress'] = 0;
- $sandbox['size_vocabulary'] = $size_vocabulary;
- }
- $size_part = array_slice($sandbox['sizes'], $sandbox['progress'], 5);
- foreach ($size_part as $size) {
- $term = new stdClass();
- $term->name = trim($size);
- $term->weight = $sandbox['progress'];
- $term->vid = $sandbox['size_vocabulary']->vid;
- taxonomy_term_save($term);
- $sandbox['progress']++;
- }
- $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
- return t('Migrate size data to taxonomy vocabulary @progress out of @max.', ['@progress' => $sandbox['progress'], '@max' => $sandbox['max']]);
- }
- /**
- * Delete old size field and backup enter data.
- */
- function skg_global_update_7002() {
- $old_size_data = array();
- $query = new EntityFieldQuery();
- $all_product_bundles = array_keys(field_info_bundles('commerce_product'));
- // Backup enter size data.
- $all_products_raw = $query->entityCondition('entity_type', 'commerce_product')->execute();
- $all_products_ids = array_keys($all_products_raw['commerce_product']);
- $products = commerce_product_load_multiple($all_products_ids);
- foreach ($products as $product) {
- $product_entity_wrapper = entity_metadata_wrapper('commerce_product', $product);
- if ($size = $product_entity_wrapper->field_size->value()) {
- $old_size_data[$product_entity_wrapper->getIdentifier()] = $size;
- }
- }
- variable_set('skg_sizes_backup', serialize($old_size_data));
- // Delete old size field from product instances.
- foreach ($all_product_bundles as $bundle) {
- if ($instance = field_info_instance('commerce_product', 'field_size', $bundle)) {
- field_delete_instance($instance, TRUE);
- field_purge_batch(1);
- }
- }
- // Delete old size field from system.
- field_delete_field('field_size');
- field_purge_batch(1);
- }
- /**
- * Create taxonomy sizes field and attach it to commerce products.
- */
- function skg_global_update_7003() {
- $all_product_bundles = array_keys(field_info_bundles('commerce_product'));
- // Create taxonomy size field.
- $taxonomy_size_field = array(
- 'field_name' => 'field_size',
- 'type' => 'taxonomy_term_reference',
- 'settings' => array(
- 'allowed_values' => array(
- array(
- 'vocabulary' => 'sizes',
- 'parent' => 0,
- ),
- ),
- ),
- );
- field_create_field($taxonomy_size_field);
- // Attach taxonomy size field to product bundle.
- foreach ($all_product_bundles as $product_bundle_name) {
- $instance = array(
- 'field_name' => 'field_size',
- 'entity_type' => 'commerce_product',
- 'label' => t('Size'),
- 'bundle' => $product_bundle_name,
- 'required' => FALSE,
- 'widget' => array(
- 'type' => 'options_select',
- 'weight' => 4.1,
- ),
- 'commerce_bpc' => array(
- 'show_field' => 1,
- ),
- 'commerce_cart_settings' => array(
- 'attribute_field' => 1,
- 'attribute_widget' => 'select',
- 'attribute_widget_title' => t('Size'),
- 'combined_field' => 1,
- ),
- 'display' => array(
- 'default' => array(
- 'type' => 'taxonomy_term_reference_link',
- 'weight' => 0.1,
- ),
- 'teaser' => array(
- 'type' => 'taxonomy_term_reference_link',
- 'weight' => 10,
- ),
- ),
- );
- field_create_instance($instance);
- }
- }
- /**
- * Migrate old data to taxonomy size field.
- */
- function skg_global_update_7009(&$sandbox) {
- if (!isset($sandbox['progress'])) {
- $prepared_size_terms = [];
- $sizes_vocabulary = taxonomy_vocabulary_machine_name_load('sizes');
- $sizes_terms = entity_load('taxonomy_term', FALSE, array('vid' => $sizes_vocabulary->vid));
- // Prepared size term to simple key => value format.
- foreach ($sizes_terms as $tid => $size_term) {
- $prepared_size_terms[$tid] = $size_term->name;
- }
- $sandbox['product_size_data'] = unserialize(variable_get('skg_sizes_backup'));
- $sandbox['max'] = count($sandbox['product_size_data']);
- $sandbox['terms'] = $prepared_size_terms;
- $sandbox['progress'] = 0;
- }
- $size_products_data = array_slice($sandbox['product_size_data'], $sandbox['progress'], 25, TRUE);
- foreach ($size_products_data as $product_id => $size) {
- $product_entity_wrapper = entity_metadata_wrapper('commerce_product', $product_id);
- $match_tid = array_search(trim($size), $sandbox['terms']);
- if (!empty($match_tid)) {
- $product_entity_wrapper->field_size = $match_tid;
- $product_entity_wrapper->save();
- }
- $sandbox['progress']++;
- }
- $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
- // Delete temp migration data.
- if ($sandbox['#finished'] == 1) {
- variable_del('skg_sizes_backup');
- }
- return t('Migrate size data @progress out of @max.', ['@progress' => $sandbox['progress'], '@max' => $sandbox['max']]);
- }
- /**
- * Migrate 'Size 2' field from list to taxonomy type.
- */
- function skg_global_update_7010() {
- $all_product_bundles = array_keys(field_info_bundles('commerce_product'));
- // Delete old size2 field from product instances.
- foreach ($all_product_bundles as $bundle) {
- if ($instance = field_info_instance('commerce_product', 'field_size2', $bundle)) {
- field_delete_instance($instance, TRUE);
- field_purge_batch(1);
- }
- }
- // Delete old size2 field from system.
- field_delete_field('field_size2');
- field_purge_batch(1);
- // Create taxonomy 'Size 2' field.
- $taxonomy_size_field = array(
- 'field_name' => 'field_size2',
- 'type' => 'taxonomy_term_reference',
- 'settings' => array(
- 'allowed_values' => array(
- array(
- 'vocabulary' => 'sizes',
- 'parent' => 0,
- ),
- ),
- ),
- );
- field_create_field($taxonomy_size_field);
- // Attach taxonomy size2 field to product bundle.
- foreach ($all_product_bundles as $product_bundle_name) {
- $instance = array(
- 'field_name' => 'field_size2',
- 'entity_type' => 'commerce_product',
- 'label' => t('Size 2'),
- 'bundle' => $product_bundle_name,
- 'required' => FALSE,
- 'widget' => array(
- 'type' => 'options_select',
- 'weight' => 4.2,
- ),
- 'commerce_bpc' => array(
- 'show_field' => 1,
- ),
- 'display' => array(
- 'default' => array(
- 'type' => 'taxonomy_term_reference_link',
- 'weight' => 0.2,
- ),
- 'teaser' => array(
- 'type' => 'taxonomy_term_reference_link',
- 'weight' => 10,
- ),
- ),
- );
- field_create_instance($instance);
- }
- }
Add Comment
Please, Sign In to add comment