D7TaxonomyTermCompany.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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."\t".$tid."\t".$row->getSourceProperty('name'));
  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. // drush_print('tode_nid: ');
  130. // print_r($tode_nid);
  131. // drush_print(' ');
  132. if($tode_nid){
  133. // get the tode fields
  134. foreach ($this->getFields('node', 'company') as $field_name => $field) {
  135. // Ensure we're using the right language if the entity is translatable.
  136. $field_language = $field['translatable'] ? $language : NULL;
  137. $field_values = $this->getFieldValues('node', $field_name, $tode_nid, null, $field_language);
  138. $row->setSourceProperty($field_name, $field_values);
  139. }
  140. // $body = $row->getSourceProperty('body');
  141. // if(!empty($body)){
  142. // drush_print('body: ');
  143. // print_r($body);
  144. // }
  145. // $field_infos_from_company = $row->getSourceProperty('field_infos_from_company');
  146. // if(!empty($field_infos_from_company)){
  147. // drush_print('field_infos_from_company: ');
  148. // print_r($field_infos_from_company);
  149. // }
  150. // make sure that field_website url is absolute
  151. $field_website = $row->getSourceProperty('field_website');
  152. if(isset($field_website[0]['url'])){
  153. $url = $field_website[0]['url'];
  154. if(!strpos($url, 'http://')){
  155. $field_website[0]['url'] = 'http://'.$url;
  156. $row->setSourceProperty('field_website', $field_website);
  157. }
  158. }
  159. // convert phone field with libphonenumber
  160. $field_phone = $row->getSourceProperty('field_public_phone');
  161. if(isset($field_phone[0]['number']) && !empty($field_phone[0]['number'])){
  162. $national_number = $field_phone[0]['number'];
  163. $region = strtoupper($field_phone[0]['country_codes']);
  164. // isValidRegionCode($regionCode)
  165. if($this->phoneUtils->isPossibleNumber($national_number, $region)){
  166. $number = $this->phoneUtils->parse($national_number, $region);
  167. $row->setSourceProperty('public_phone', $this->phoneUtils->format($number, PhoneNumberFormat::E164));
  168. }else{
  169. // add bad phone number to memo field
  170. $memo .= "#migration : invalid phone number: ".$national_number.' region: '.$region."\n";
  171. drush_print('WARNING: phone number invalide; number: '.$national_number.' region: '.$region);
  172. }
  173. }
  174. // TODO: attachment files
  175. // no needs as there is no attachment field used for companies
  176. // workflow
  177. $query = $this->select('workflow_node', 'wn');
  178. $query->fields('wn', ['sid']);
  179. $query->condition('wn.nid', $tode_nid);
  180. $results = $query->execute()->fetchField();
  181. if(!$results){
  182. $results = 2;
  183. // add bad workflow to memo field
  184. $memo .= "#migration : invalid workflow\n";
  185. drush_print('WARNING: no workflow');
  186. }
  187. $row->setSourceProperty('workflow', $results);
  188. }else{
  189. // if no tode set workflow to hidden
  190. $row->setSourceProperty('workflow', 2);
  191. $memo .= "#migration : missing data from tode\n";
  192. drush_print('WARNING: no tode');
  193. }
  194. // record migration errors in field_memo
  195. if(isset($memo)){
  196. $field_memo = $row->getSourceProperty('field_memo');
  197. $field_memo[0]['value'] .= "\n".$memo;
  198. $row->setSourceProperty('field_memo', $field_memo);
  199. }
  200. return parent::prepareRow($row);
  201. }
  202. /**
  203. * {@inheritdoc}
  204. */
  205. public function getIds() {
  206. $ids['tid']['type'] = 'integer';
  207. return $ids;
  208. }
  209. }