Guest User

Untitled

a guest
Oct 23rd, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.29 KB | None | 0 0
  1. /**
  2. * Implements hook_install().
  3. */
  4. function skg_global_install() {
  5. // For allow run updates after module install.
  6. drupal_set_installed_schema_version('skg_global', '7000');
  7. }
  8.  
  9. /**
  10. * Move sizes from list field to taxonomy vocabulary.
  11. */
  12. function skg_global_update_7001(&$sandbox) {
  13. if (!isset($sandbox['progress'])) {
  14. $fields = field_info_fields();
  15. $sizes_list_1 = list_allowed_values($fields['field_size']);
  16. $sizes_list_2 = list_allowed_values($fields['field_size2']);
  17. $sizes = $sizes_list_1 + $sizes_list_2;
  18.  
  19. $size_vocabulary = new stdClass();
  20. $size_vocabulary->name = t('Sizes');
  21. $size_vocabulary->machine_name = 'sizes';
  22. $size_vocabulary->description = t('Available size options');
  23. taxonomy_vocabulary_save($size_vocabulary);
  24.  
  25. $sandbox['sizes'] = $sizes;
  26. $sandbox['max'] = count($sizes);
  27. $sandbox['progress'] = 0;
  28. $sandbox['size_vocabulary'] = $size_vocabulary;
  29. }
  30.  
  31. $size_part = array_slice($sandbox['sizes'], $sandbox['progress'], 5);
  32. foreach ($size_part as $size) {
  33. $term = new stdClass();
  34. $term->name = trim($size);
  35. $term->weight = $sandbox['progress'];
  36. $term->vid = $sandbox['size_vocabulary']->vid;
  37. taxonomy_term_save($term);
  38. $sandbox['progress']++;
  39. }
  40.  
  41. $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
  42. return t('Migrate size data to taxonomy vocabulary @progress out of @max.', ['@progress' => $sandbox['progress'], '@max' => $sandbox['max']]);
  43. }
  44.  
  45. /**
  46. * Delete old size field and backup enter data.
  47. */
  48. function skg_global_update_7002() {
  49. $old_size_data = array();
  50. $query = new EntityFieldQuery();
  51. $all_product_bundles = array_keys(field_info_bundles('commerce_product'));
  52.  
  53. // Backup enter size data.
  54. $all_products_raw = $query->entityCondition('entity_type', 'commerce_product')->execute();
  55. $all_products_ids = array_keys($all_products_raw['commerce_product']);
  56. $products = commerce_product_load_multiple($all_products_ids);
  57.  
  58. foreach ($products as $product) {
  59. $product_entity_wrapper = entity_metadata_wrapper('commerce_product', $product);
  60.  
  61. if ($size = $product_entity_wrapper->field_size->value()) {
  62. $old_size_data[$product_entity_wrapper->getIdentifier()] = $size;
  63. }
  64. }
  65. variable_set('skg_sizes_backup', serialize($old_size_data));
  66.  
  67. // Delete old size field from product instances.
  68. foreach ($all_product_bundles as $bundle) {
  69. if ($instance = field_info_instance('commerce_product', 'field_size', $bundle)) {
  70. field_delete_instance($instance, TRUE);
  71. field_purge_batch(1);
  72. }
  73. }
  74.  
  75. // Delete old size field from system.
  76. field_delete_field('field_size');
  77. field_purge_batch(1);
  78. }
  79.  
  80. /**
  81. * Create taxonomy sizes field and attach it to commerce products.
  82. */
  83. function skg_global_update_7003() {
  84. $all_product_bundles = array_keys(field_info_bundles('commerce_product'));
  85.  
  86. // Create taxonomy size field.
  87. $taxonomy_size_field = array(
  88. 'field_name' => 'field_size',
  89. 'type' => 'taxonomy_term_reference',
  90. 'settings' => array(
  91. 'allowed_values' => array(
  92. array(
  93. 'vocabulary' => 'sizes',
  94. 'parent' => 0,
  95. ),
  96. ),
  97. ),
  98. );
  99. field_create_field($taxonomy_size_field);
  100.  
  101. // Attach taxonomy size field to product bundle.
  102. foreach ($all_product_bundles as $product_bundle_name) {
  103. $instance = array(
  104. 'field_name' => 'field_size',
  105. 'entity_type' => 'commerce_product',
  106. 'label' => t('Size'),
  107. 'bundle' => $product_bundle_name,
  108. 'required' => FALSE,
  109. 'widget' => array(
  110. 'type' => 'options_select',
  111. 'weight' => 4.1,
  112. ),
  113. 'commerce_bpc' => array(
  114. 'show_field' => 1,
  115. ),
  116. 'commerce_cart_settings' => array(
  117. 'attribute_field' => 1,
  118. 'attribute_widget' => 'select',
  119. 'attribute_widget_title' => t('Size'),
  120. 'combined_field' => 1,
  121. ),
  122. 'display' => array(
  123. 'default' => array(
  124. 'type' => 'taxonomy_term_reference_link',
  125. 'weight' => 0.1,
  126. ),
  127. 'teaser' => array(
  128. 'type' => 'taxonomy_term_reference_link',
  129. 'weight' => 10,
  130. ),
  131. ),
  132. );
  133. field_create_instance($instance);
  134. }
  135. }
  136.  
  137. /**
  138. * Migrate old data to taxonomy size field.
  139. */
  140. function skg_global_update_7009(&$sandbox) {
  141. if (!isset($sandbox['progress'])) {
  142. $prepared_size_terms = [];
  143. $sizes_vocabulary = taxonomy_vocabulary_machine_name_load('sizes');
  144. $sizes_terms = entity_load('taxonomy_term', FALSE, array('vid' => $sizes_vocabulary->vid));
  145.  
  146. // Prepared size term to simple key => value format.
  147. foreach ($sizes_terms as $tid => $size_term) {
  148. $prepared_size_terms[$tid] = $size_term->name;
  149. }
  150.  
  151. $sandbox['product_size_data'] = unserialize(variable_get('skg_sizes_backup'));
  152. $sandbox['max'] = count($sandbox['product_size_data']);
  153. $sandbox['terms'] = $prepared_size_terms;
  154. $sandbox['progress'] = 0;
  155. }
  156.  
  157. $size_products_data = array_slice($sandbox['product_size_data'], $sandbox['progress'], 25, TRUE);
  158. foreach ($size_products_data as $product_id => $size) {
  159. $product_entity_wrapper = entity_metadata_wrapper('commerce_product', $product_id);
  160. $match_tid = array_search(trim($size), $sandbox['terms']);
  161.  
  162. if (!empty($match_tid)) {
  163. $product_entity_wrapper->field_size = $match_tid;
  164. $product_entity_wrapper->save();
  165. }
  166.  
  167. $sandbox['progress']++;
  168. }
  169.  
  170. $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
  171.  
  172. // Delete temp migration data.
  173. if ($sandbox['#finished'] == 1) {
  174. variable_del('skg_sizes_backup');
  175. }
  176.  
  177. return t('Migrate size data @progress out of @max.', ['@progress' => $sandbox['progress'], '@max' => $sandbox['max']]);
  178. }
  179.  
  180. /**
  181. * Migrate 'Size 2' field from list to taxonomy type.
  182. */
  183. function skg_global_update_7010() {
  184. $all_product_bundles = array_keys(field_info_bundles('commerce_product'));
  185.  
  186. // Delete old size2 field from product instances.
  187. foreach ($all_product_bundles as $bundle) {
  188. if ($instance = field_info_instance('commerce_product', 'field_size2', $bundle)) {
  189. field_delete_instance($instance, TRUE);
  190. field_purge_batch(1);
  191. }
  192. }
  193.  
  194. // Delete old size2 field from system.
  195. field_delete_field('field_size2');
  196. field_purge_batch(1);
  197.  
  198. // Create taxonomy 'Size 2' field.
  199. $taxonomy_size_field = array(
  200. 'field_name' => 'field_size2',
  201. 'type' => 'taxonomy_term_reference',
  202. 'settings' => array(
  203. 'allowed_values' => array(
  204. array(
  205. 'vocabulary' => 'sizes',
  206. 'parent' => 0,
  207. ),
  208. ),
  209. ),
  210. );
  211. field_create_field($taxonomy_size_field);
  212.  
  213. // Attach taxonomy size2 field to product bundle.
  214. foreach ($all_product_bundles as $product_bundle_name) {
  215. $instance = array(
  216. 'field_name' => 'field_size2',
  217. 'entity_type' => 'commerce_product',
  218. 'label' => t('Size 2'),
  219. 'bundle' => $product_bundle_name,
  220. 'required' => FALSE,
  221. 'widget' => array(
  222. 'type' => 'options_select',
  223. 'weight' => 4.2,
  224. ),
  225. 'commerce_bpc' => array(
  226. 'show_field' => 1,
  227. ),
  228. 'display' => array(
  229. 'default' => array(
  230. 'type' => 'taxonomy_term_reference_link',
  231. 'weight' => 0.2,
  232. ),
  233. 'teaser' => array(
  234. 'type' => 'taxonomy_term_reference_link',
  235. 'weight' => 10,
  236. ),
  237. ),
  238. );
  239.  
  240. field_create_instance($instance);
  241. }
  242. }
Add Comment
Please, Sign In to add comment