D7TaxonomyTermCompany.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. namespace Drupal\materio_migrate\Plugin\migrate\source;
  3. use Drupal\migrate\Row;
  4. use Drupal\migrate_drupal\Plugin\migrate\source\d7\FieldableEntity;
  5. use Symfony\Component\DependencyInjection\ContainerInterface;
  6. use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
  7. use Drupal\migrate\Plugin\MigrationInterface;
  8. use Drupal\Core\State\StateInterface;
  9. use Drupal\Core\Entity\EntityManagerInterface;
  10. use Drupal\Core\Locale\CountryManagerInterface;
  11. use libphonenumber\PhoneNumber;
  12. use libphonenumber\PhoneNumberUtil;
  13. use libphonenumber\PhoneNumberFormat;
  14. /**
  15. * Taxonomy term source from database.
  16. *
  17. * @MigrateSource(
  18. * id = "d7_taxonomy_term_company",
  19. * source_module = "taxonomy"
  20. * )
  21. */
  22. class D7TaxonomyTermCompany extends FieldableEntity implements ContainerFactoryPluginInterface{
  23. /**
  24. * Phone Number util.
  25. *
  26. * @var \libphonenumber\PhoneNumberUtil
  27. */
  28. public $phoneUtils;
  29. /**
  30. * Country Manager service.
  31. *
  32. * @var \Drupal\Core\Locale\CountryManagerInterface
  33. */
  34. public $countryManager;
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, StateInterface $state, EntityManagerInterface $entity_manager, CountryManagerInterface $country_manager) {
  39. parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $state, $entity_manager);
  40. $this->phoneUtils = PhoneNumberUtil::getInstance();
  41. $this->countryManager = $country_manager;
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
  47. return new static(
  48. $configuration,
  49. $plugin_id,
  50. $plugin_definition,
  51. $migration,
  52. $container->get('state'),
  53. $container->get('entity.manager'),
  54. $container->get('country_manager')
  55. );
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function query() {
  61. $query = $this->select('taxonomy_term_data', 'td')
  62. ->fields('td')
  63. ->orderBy('tid');
  64. $query->leftJoin('taxonomy_vocabulary', 'tv', 'td.vid = tv.vid');
  65. $query->addField('tv', 'machine_name');
  66. if (isset($this->configuration['bundle'])) {
  67. $query->condition('tv.machine_name', (array) $this->configuration['bundle'], 'IN');
  68. }
  69. return $query;
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function fields() {
  75. $fields = [
  76. 'tid' => $this->t('The term ID.'),
  77. 'vid' => $this->t('Existing term VID'),
  78. 'machine_name' => $this->t('Vocabulary machine name'),
  79. 'name' => $this->t('The name of the term.'),
  80. 'description' => $this->t('The term description.'),
  81. 'weight' => $this->t('Weight'),
  82. 'parent' => $this->t("The Drupal term IDs of the term's parents."),
  83. 'format' => $this->t("Format of the term description."),
  84. 'revision_id' => $this->t('Revision ID'),
  85. 'language' => $this->t('Term translation language'),
  86. 'source' => $this->t('Term translation source language'),
  87. 'public_phone' => $this->t('Public phone converted to new format (with libphonenumber)')
  88. ];
  89. return $fields;
  90. }
  91. /**
  92. * {@inheritdoc}
  93. */
  94. public function prepareRow(Row $row) {
  95. $language = $row->getSourceProperty('language');
  96. $tid = $row->getSourceProperty('tid');
  97. drush_print('-- '.$language.' -- '.$tid." -- ".$row->getSourceProperty('name')." -- ".$row->getSourceProperty('weight'));
  98. // vocabulary machine name
  99. $machine_name = $row->getSourceProperty('machine_name');
  100. $revid = $row->getSourceProperty('revision_id');
  101. // Get Field API field values.
  102. foreach ($this->getFields('taxonomy_term', $machine_name) as $field_name => $field) {
  103. // Ensure we're using the right language if the entity is translatable.
  104. $field_language = $field['translatable'] ? $language : NULL;
  105. $row->setSourceProperty($field_name, $this->getFieldValues('taxonomy_term', $field_name, $tid, $revid, $field_language));
  106. }
  107. // Find parents for this row.
  108. $parents = $this->select('taxonomy_term_hierarchy', 'th')
  109. ->fields('th', ['parent', 'tid'])
  110. ->condition('tid', $row->getSourceProperty('tid'))
  111. ->execute()
  112. ->fetchCol();
  113. $row->setSourceProperty('parent', $parents);
  114. // If the node title was replaced by a real field using the Drupal 7 Title
  115. // module, use the field value instead of the node title.
  116. if ($this->moduleExists('title')) {
  117. $name_field = $row->getSourceProperty('name_field');
  118. if (isset($name_field[0]['value'])) {
  119. $row->setSourceProperty('name', $name_field[0]['value']);
  120. }
  121. }
  122. // get the tode nid
  123. $tode_nid = $this->select('field_data_field_tode_company', 'tode')
  124. ->condition('entity_type', 'node')
  125. ->condition('field_tode_company_tid', $tid)
  126. ->fields('tode', ['entity_id'])
  127. ->execute()->fetchField();
  128. // drush_print('tode_nid: '.$tode_nid);
  129. // get the tode fields
  130. foreach ($this->getFields('node', 'company') as $field_name => $field) {
  131. // Ensure we're using the right language if the entity is translatable.
  132. $field_language = $field['translatable'] ? $language : NULL;
  133. $field_values = $this->getFieldValues('node', $field_name, $tode_nid, null, $field_language);
  134. $row->setSourceProperty($field_name, $field_values);
  135. }
  136. // drush_print('body: ');
  137. // print_r($row->getSourceProperty('body'));
  138. $field_infos = $row->getSourceProperty('field_infos_from_company');
  139. if(!empty($field_infos)){
  140. drush_print('field_infos_from_company: ');
  141. print_r($field_infos);
  142. }
  143. // make sure that field_website url is absolute
  144. $field_website = $row->getSourceProperty('field_website');
  145. if(isset($field_website[0]['url'])){
  146. $url = $field_website[0]['url'];
  147. if(!strpos($url, 'http://')){
  148. $field_website[0]['url'] = 'http://'.$url;
  149. $row->setSourceProperty('field_website', $field_website);
  150. }
  151. }
  152. // convert phone field with libphonenumber
  153. $field_phone = $row->getSourceProperty('field_public_phone');
  154. if(isset($field_phone[0]['number']) && !empty($field_phone[0]['number'])){
  155. $national_number = $field_phone[0]['number'];
  156. $region = strtoupper($field_phone[0]['country_codes']);
  157. // isValidRegionCode($regionCode)
  158. if($this->phoneUtils->isPossibleNumber($national_number, $region)){
  159. $number = $this->phoneUtils->parse($national_number, $region);
  160. $row->setSourceProperty('public_phone', $this->phoneUtils->format($number, PhoneNumberFormat::E164));
  161. }else{
  162. // add bad phone number to memo field
  163. $memo .= "invalid phone number: ".$national_number.' region: '.$region."\n";
  164. drush_print('WARNING: phone number invalide; number: '.$national_number.' region: '.$region);
  165. }
  166. }
  167. // attachment files
  168. // workflow
  169. // get the workflow status
  170. $query = $this->select('workflow_node', 'wn');
  171. $query->fields('wn', ['sid']);
  172. $query->condition('wn.nid', $tode_nid);
  173. $results = $query->execute()->fetchField();
  174. // drush_print('workflow query results : ', 0, null, false);
  175. // drush_print_r($results);
  176. $row->setSourceProperty('workflow', $results);
  177. // record migration errors in field_memo
  178. if(isset($memo)){
  179. $field_memo = $row->getSourceProperty('field_memo');
  180. $field_memo[0]['value'] .= "\n".$memo;
  181. $row->setSourceProperty('field_memo', $field_memo);
  182. }
  183. return parent::prepareRow($row);
  184. }
  185. /**
  186. * {@inheritdoc}
  187. */
  188. public function getIds() {
  189. $ids['tid']['type'] = 'integer';
  190. return $ids;
  191. }
  192. }