|
@@ -4,6 +4,15 @@ namespace Drupal\materio_migrate\Plugin\migrate\source;
|
|
|
|
|
|
use Drupal\migrate\Row;
|
|
|
use Drupal\migrate_drupal\Plugin\migrate\source\d7\FieldableEntity;
|
|
|
+use Symfony\Component\DependencyInjection\ContainerInterface;
|
|
|
+use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
|
|
|
+use Drupal\migrate\Plugin\MigrationInterface;
|
|
|
+use Drupal\Core\State\StateInterface;
|
|
|
+use Drupal\Core\Entity\EntityManagerInterface;
|
|
|
+use Drupal\Core\Locale\CountryManagerInterface;
|
|
|
+use libphonenumber\PhoneNumber;
|
|
|
+use libphonenumber\PhoneNumberUtil;
|
|
|
+use libphonenumber\PhoneNumberFormat;
|
|
|
|
|
|
/**
|
|
|
* Taxonomy term source from database.
|
|
@@ -15,14 +24,52 @@ use Drupal\migrate_drupal\Plugin\migrate\source\d7\FieldableEntity;
|
|
|
*/
|
|
|
class D7TaxonomyTermShowroom extends FieldableEntity {
|
|
|
|
|
|
+ /**
|
|
|
+ * Phone Number util.
|
|
|
+ *
|
|
|
+ * @var \libphonenumber\PhoneNumberUtil
|
|
|
+ */
|
|
|
+ public $phoneUtils;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Country Manager service.
|
|
|
+ *
|
|
|
+ * @var \Drupal\Core\Locale\CountryManagerInterface
|
|
|
+ */
|
|
|
+ public $countryManager;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * {@inheritdoc}
|
|
|
+ */
|
|
|
+ public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, StateInterface $state, EntityManagerInterface $entity_manager, CountryManagerInterface $country_manager) {
|
|
|
+ parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $state, $entity_manager);
|
|
|
+ $this->phoneUtils = PhoneNumberUtil::getInstance();
|
|
|
+ $this->countryManager = $country_manager;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * {@inheritdoc}
|
|
|
+ */
|
|
|
+ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
|
|
|
+ return new static(
|
|
|
+ $configuration,
|
|
|
+ $plugin_id,
|
|
|
+ $plugin_definition,
|
|
|
+ $migration,
|
|
|
+ $container->get('state'),
|
|
|
+ $container->get('entity.manager'),
|
|
|
+ $container->get('country_manager')
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* {@inheritdoc}
|
|
|
*/
|
|
|
public function query() {
|
|
|
$query = $this->select('taxonomy_term_data', 'td')
|
|
|
->fields('td')
|
|
|
- ->distinct()
|
|
|
->orderBy('tid');
|
|
|
+
|
|
|
$query->leftJoin('taxonomy_vocabulary', 'tv', 'td.vid = tv.vid');
|
|
|
$query->addField('tv', 'machine_name');
|
|
|
|
|
@@ -55,12 +102,19 @@ class D7TaxonomyTermShowroom extends FieldableEntity {
|
|
|
* {@inheritdoc}
|
|
|
*/
|
|
|
public function prepareRow(Row $row) {
|
|
|
- print("\n".'-- prepareRow -- '.$row->getSourceProperty('tid')." -- ".$row->getSourceProperty('name')."\n");
|
|
|
+ $language = $row->getSourceProperty('language');
|
|
|
+ $tid = $row->getSourceProperty('tid');
|
|
|
+ drush_print('-- '.$language."\t".$tid."\t".$row->getSourceProperty('name'));
|
|
|
+
|
|
|
+ // vocabulary machine name
|
|
|
+ $machine_name = $row->getSourceProperty('machine_name');
|
|
|
+ $revid = $row->getSourceProperty('revision_id');
|
|
|
|
|
|
// Get Field API field values.
|
|
|
- foreach (array_keys($this->getFields('taxonomy_term', $row->getSourceProperty('machine_name'))) as $field) {
|
|
|
- $tid = $row->getSourceProperty('tid');
|
|
|
- $row->setSourceProperty($field, $this->getFieldValues('taxonomy_term', $field, $tid));
|
|
|
+ foreach ($this->getFields('taxonomy_term', $machine_name) as $field_name => $field) {
|
|
|
+ // Ensure we're using the right language if the entity is translatable.
|
|
|
+ $field_language = $field['translatable'] ? $language : NULL;
|
|
|
+ $row->setSourceProperty($field_name, $this->getFieldValues('taxonomy_term', $field_name, $tid, $revid, $field_language));
|
|
|
}
|
|
|
|
|
|
// Find parents for this row.
|
|
@@ -72,23 +126,73 @@ class D7TaxonomyTermShowroom extends FieldableEntity {
|
|
|
$row->setSourceProperty('parent', $parents);
|
|
|
// print_r($parents);
|
|
|
|
|
|
- // If the term name or term description were replaced by real fields using
|
|
|
- // the Drupal 7 Title module, use the fields value instead of the term name
|
|
|
- // or term description.
|
|
|
- // TODO: translations
|
|
|
+ // If the node title was replaced by a real field using the Drupal 7 Title
|
|
|
+ // module, use the field value instead of the node title.
|
|
|
if ($this->moduleExists('title')) {
|
|
|
$name_field = $row->getSourceProperty('name_field');
|
|
|
- // print_r($name_field);
|
|
|
if (isset($name_field[0]['value'])) {
|
|
|
$row->setSourceProperty('name', $name_field[0]['value']);
|
|
|
}
|
|
|
- $description_field = $row->getSourceProperty('description_field');
|
|
|
- if (isset($description_field[0]['value'])) {
|
|
|
- $row->setSourceProperty('description', $description_field[0]['value']);
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
- // TODO: tode node showroom
|
|
|
+ // get the tode nid
|
|
|
+ $tode_nid = $this->select('field_data_field_tode_showroom', 'tode')
|
|
|
+ ->condition('entity_type', 'node')
|
|
|
+ ->condition('field_tode_showroom_tid', $tid)
|
|
|
+ ->fields('tode', ['entity_id'])
|
|
|
+ ->execute()->fetchField();
|
|
|
+ // drush_print('tode_nid: '.$tode_nid);
|
|
|
+
|
|
|
+ // drush_print('tode_nid: ');
|
|
|
+ // print_r($tode_nid);
|
|
|
+ // drush_print(' ');
|
|
|
+ if($tode_nid){
|
|
|
+ // get the tode fields
|
|
|
+ foreach ($this->getFields('node', 'company') as $field_name => $field) {
|
|
|
+ // Ensure we're using the right language if the entity is translatable.
|
|
|
+ $field_language = $field['translatable'] ? $language : NULL;
|
|
|
+ $field_values = $this->getFieldValues('node', $field_name, $tode_nid, null, $field_language);
|
|
|
+
|
|
|
+ $row->setSourceProperty($field_name, $field_values);
|
|
|
+ }
|
|
|
+
|
|
|
+ // make sure that field_website url is absolute
|
|
|
+ $field_website = $row->getSourceProperty('field_website');
|
|
|
+ if(isset($field_website[0]['url'])){
|
|
|
+ $url = $field_website[0]['url'];
|
|
|
+ if(!strpos($url, 'http://')){
|
|
|
+ $field_website[0]['url'] = 'http://'.$url;
|
|
|
+ $row->setSourceProperty('field_website', $field_website);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // convert phone field with libphonenumber
|
|
|
+ $field_phone = $row->getSourceProperty('field_public_phone');
|
|
|
+ if(isset($field_phone[0]['number']) && !empty($field_phone[0]['number'])){
|
|
|
+ $national_number = $field_phone[0]['number'];
|
|
|
+ $region = strtoupper($field_phone[0]['country_codes']);
|
|
|
+ // isValidRegionCode($regionCode)
|
|
|
+ if($this->phoneUtils->isPossibleNumber($national_number, $region)){
|
|
|
+ $number = $this->phoneUtils->parse($national_number, $region);
|
|
|
+ $row->setSourceProperty('public_phone', $this->phoneUtils->format($number, PhoneNumberFormat::E164));
|
|
|
+ }else{
|
|
|
+ // add bad phone number to memo field
|
|
|
+ $memo .= "#migration : invalid phone number: ".$national_number.' region: '.$region."\n";
|
|
|
+ drush_print('WARNING: phone number invalide; number: '.$national_number.' region: '.$region);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }else{
|
|
|
+ // if no tode set workflow to hidden
|
|
|
+ $memo .= "#migration : missing data from tode\n";
|
|
|
+ drush_print('WARNING: no tode');
|
|
|
+ }
|
|
|
+ // record migration errors in field_memo
|
|
|
+ if(isset($memo)){
|
|
|
+ $field_memo = $row->getSourceProperty('field_memo');
|
|
|
+ $field_memo[0]['value'] .= "\n".$memo;
|
|
|
+ $row->setSourceProperty('field_memo', $field_memo);
|
|
|
+ }
|
|
|
|
|
|
|
|
|
return parent::prepareRow($row);
|