Skip to content

Commit

Permalink
#29 Taxonomy support and improved tests
Browse files Browse the repository at this point in the history
  • Loading branch information
haringsrob committed Jan 27, 2017
1 parent 392a16a commit 3da4bc3
Show file tree
Hide file tree
Showing 4 changed files with 256 additions and 44 deletions.
32 changes: 32 additions & 0 deletions modules/integration_migrate/src/Plugin/migrate/process/TagToId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Drupal\integration_migrate\Plugin\migrate\process;

use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;

/**
* This plugin generates entities within the process plugin.
*
* @MigrateProcessPlugin(
* id = "tag_to_id"
* )
*
* @Todo: Normally we should use EntityLookup for this..
*/
class TagToId extends ProcessPluginBase {

/**
* {@inheritdoc}
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
if ($terms = taxonomy_term_load_multiple_by_name($value)) {
/** @var \Drupal\taxonomy\Entity\Term $term */
$term = reset($terms);
return $term->id();
}
return NULL;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ class IntegrationDocuments extends SourcePluginBase {
*/
private $documentsArray = [];

/**
* Contains mapping data.
*
* @var array
*/
private $mappingData;

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -97,44 +104,62 @@ public function getDocumentType() {

/**
* {@inheritdoc}
*
* @todo: This does a bit to much at the moment and needs some cleanup to
* avoid unwanted results..
*/
public function prepareRow(Row $row) {
$language = $row->getSource()['language'];

foreach ($this->getDocument()->getFieldMachineNames() as $field_name) {
$row->setDestinationProperty($field_name, $row->getSource()['processed']
->getFieldValue($field_name, $language));
}

// @todo: Static metadata, this can go into Document I think..
$static_metadata = [
'nid' => '_id',
'bundle' => 'type',
'created' => 'created',
'changed' => 'changed',
'status' => 'status',
'sticky' => 'sticky',
'default_langcode' => 'default_langcode',
];

foreach ($static_metadata as $destination => $source) {
foreach ($this->getMappingData() as $destination => $source) {
if (!is_null($this->getDocument()->getMetadata($source))) {
$row->setDestinationProperty($destination, $row->getSource()['processed']
->getMetadata($source));
$row->setSourceProperty($destination, $row->getSource()['processed']
->getMetadata($source));
}
}

// Map the remaining data, but exclude fields with custom mapping.
foreach ($this->getDocument()->getFieldMachineNames() as $field_name) {
$source = $field_name;
$destination = $field_name;
// Exclude already mapped data.
if (array_key_exists($field_name, $this->getMappingData())) { $destination = $this->getMappingData()[$field_name]; }
$row->setDestinationProperty($destination, $row->getSource()['processed']
->getFieldValue($source, $language));
$row->setSourceProperty($destination, $row->getSource()['processed']
->getFieldValue($source, $language));
}

// We need the language property.
$row->setDestinationProperty('language', $language);
$row->setDestinationProperty('langcode', $language);

$bar = $row->getIdMap();
$bar['destid2'] = $language;
$row->setIdMap($bar);

return parent::prepareRow($row);
}

/**
* Gets the mapping data as an array.
*
* @return array
* The mapping data destination=>source.
*/
private function getMappingData() {
if (empty($this->mappingData)) {
$this->mappingData = [
'nid' => '_id',
'bundle' => 'type',
'created' => 'created',
'changed' => 'changed',
'status' => 'status',
'sticky' => 'sticky',
'default_langcode' => 'default_langcode',
];
}
return $this->mappingData;
}

/**
* {@inheritdoc}
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"_id": "10931",
"default_language": "en",
"languages": [
"en"
],
"fields": {
"title": {
"en": [
"Test simple document title"
]
},
"tags": {
"en": [
"Tag 1",
"Tag 2"
]
},
"multi_data": {
"en": [
"List item 1",
"List item 2"
]
}
},
"type": "integration_document_entity_test",
"created": "1235583913",
"changed": "1329926433",
"status": "0",
"sticky": "0"
}
Loading

0 comments on commit 3da4bc3

Please sign in to comment.