D7UserProfile.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. namespace Drupal\materio_migrate\Plugin\migrate\source;
  3. use Drupal\Core\Extension\ModuleHandlerInterface;
  4. use Drupal\migrate\Row;
  5. use Drupal\migrate_drupal\Plugin\migrate\source\d7\FieldableEntity;
  6. use Drupal\Core\Database\Query\SelectInterface;
  7. use Drupal\Core\Entity\EntityManagerInterface;
  8. use Drupal\Core\Extension\ModuleHandler;
  9. use Drupal\Core\State\StateInterface;
  10. use Drupal\migrate\Plugin\MigrationInterface;
  11. use Symfony\Component\DependencyInjection\ContainerInterface;
  12. use Drupal\Core\Locale\CountryManagerInterface;
  13. use libphonenumber\PhoneNumber;
  14. use libphonenumber\PhoneNumberUtil;
  15. use libphonenumber\PhoneNumberFormat;
  16. /**
  17. * Drupal 7 node source from database.
  18. *
  19. * @MigrateSource(
  20. * id = "d7_user_profile",
  21. * source_module = "profile2"
  22. * )
  23. */
  24. class D7UserProfile extends FieldableEntity {
  25. /**
  26. * Phone Number util.
  27. *
  28. * @var \libphonenumber\PhoneNumberUtil
  29. */
  30. public $phoneUtils;
  31. /**
  32. * Country Manager service.
  33. *
  34. * @var \Drupal\Core\Locale\CountryManagerInterface
  35. */
  36. public $countryManager;
  37. /**
  38. * The module handler.
  39. *
  40. * @var \Drupal\Core\Extension\ModuleHandlerInterface
  41. */
  42. protected $moduleHandler;
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, StateInterface $state, EntityManagerInterface $entity_manager, ModuleHandlerInterface $module_handler, CountryManagerInterface $country_manager) {
  47. parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $state, $entity_manager);
  48. $this->moduleHandler = $module_handler;
  49. $this->phoneUtils = PhoneNumberUtil::getInstance();
  50. $this->countryManager = $country_manager;
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
  56. return new static(
  57. $configuration,
  58. $plugin_id,
  59. $plugin_definition,
  60. $migration,
  61. $container->get('state'),
  62. $container->get('entity.manager'),
  63. $container->get('module_handler'),
  64. $container->get('country_manager')
  65. );
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function query() {
  71. // Select node in its last revision.
  72. $query = $this->select('profile', 'p')
  73. ->fields('p', [
  74. 'pid',
  75. 'type',
  76. 'uid',
  77. 'label',
  78. 'created',
  79. 'changed',
  80. ])
  81. ->orderBy('changed');
  82. return $query;
  83. }
  84. /**
  85. * {@inheritdoc}
  86. */
  87. public function prepareRow(Row $row) {
  88. $pid = $row->getSourceProperty('pid');
  89. $uid = $row->getSourceProperty('uid');
  90. $type = $row->getSourceProperty('type');
  91. // drush_print('-- '.$pid."\t".$uid."\t".$type);
  92. // Get Field API field values.
  93. foreach ($this->getFields('profile2', $type) as $field_name => $field) {
  94. // drush_print($field_name);
  95. $value = $this->getFieldValues('profile2', $field_name, $pid);
  96. $row->setSourceProperty($field_name, $value);
  97. }
  98. // merge : field_administrative_email & field_private_email into field_email
  99. // merge : field_private_name & field_name into field_name
  100. switch ($type) {
  101. case 'contact_operationnel':
  102. $row->setSourceProperty('field_email', $row->getSourceProperty('field_private_email'));
  103. $row->setSourceProperty('field_name', $row->getSourceProperty('field_private_name'));
  104. break;
  105. case 'adherent':
  106. $row->setSourceProperty('field_email', $row->getSourceProperty('field_administrative_email'));
  107. break;
  108. }
  109. // make sure that field_website url is absolute
  110. $field_website = $row->getSourceProperty('field_user_website');
  111. if(isset($field_website[0]['url'])){
  112. $url = $field_website[0]['url'];
  113. if(!strpos($url, 'http://')){
  114. $field_website[0]['url'] = 'http://'.$url;
  115. $row->setSourceProperty('field_user_website', $field_website);
  116. }
  117. }
  118. // convert phone field with libphonenumber
  119. $field_phone = $row->getSourceProperty('field_private_phone');
  120. if(isset($field_phone[0]['number']) && !empty($field_phone[0]['number'])){
  121. $national_number = $field_phone[0]['number'];
  122. $region = strtoupper($field_phone[0]['country_codes']);
  123. // isValidRegionCode($regionCode)
  124. if($this->phoneUtils->isPossibleNumber($national_number, $region)){
  125. $number = $this->phoneUtils->parse($national_number, $region);
  126. $row->setSourceProperty('field_private_phone', $this->phoneUtils->format($number, PhoneNumberFormat::E164));
  127. }else{
  128. // add bad phone number to memo field
  129. $memo .= "#migration : invalid phone number: ".$national_number.' region: '.$region."\n";
  130. drush_print('WARNING: phone number invalide; number: '.$national_number.' region: '.$region);
  131. }
  132. }
  133. // record migration errors in field_memo
  134. if(isset($memo)){
  135. $field_memo = $row->getSourceProperty('field_memo');
  136. $field_memo[0]['value'] .= "\n".$memo;
  137. $row->setSourceProperty('field_memo', $field_memo);
  138. }
  139. return parent::prepareRow($row);
  140. }
  141. /**
  142. * {@inheritdoc}
  143. */
  144. public function fields() {
  145. $fields = [
  146. 'pid' => $this->t('Profile ID'),
  147. 'type' => $this->t('Type'),
  148. 'uid' => $this->t('Title'),
  149. 'label' => $this->t('Label'),
  150. 'created' => $this->t('Created timestamp'),
  151. 'changed' => $this->t('Modified timestamp'),
  152. ];
  153. return $fields;
  154. }
  155. /**
  156. * {@inheritdoc}
  157. */
  158. public function getIds() {
  159. $ids['pid']['type'] = 'integer';
  160. $ids['pid']['alias'] = 'p';
  161. return $ids;
  162. }
  163. }