Created
September 20, 2018 04:26
-
-
Save nikunjkotecha/c93b7d9e1003a2d8b9565b9bddf0a58c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Drupal\custom_migrate\Plugin\migrate_plus\data_parser; | |
use Drupal\migrate_plus\Plugin\migrate_plus\data_parser\Json; | |
/** | |
* Obtain JSON data for migration. | |
* | |
* @DataParser( | |
* id = "custom_json", | |
* title = @Translation("Custom JSON") | |
* ) | |
*/ | |
class CustomJson extends Json { | |
/** | |
* {@inheritdoc} | |
*/ | |
protected function getSourceData($url) { | |
$response = $this->getDataFetcherPlugin()->getResponseContent($url); | |
// Convert objects to associative arrays. | |
$source_data = json_decode($response, TRUE); | |
// If json_decode() has returned NULL, it might be that the data isn't | |
// valid utf8 - see http://php.net/manual/en/function.json-decode.php#86997. | |
if (is_null($source_data)) { | |
$utf8response = utf8_encode($response); | |
$source_data = json_decode($utf8response, TRUE); | |
} | |
// Code to modify the source data as per Drupal needs. | |
if (is_array($source_data)) { | |
$source_data = $this->processSourceData($source_data); | |
} | |
// In migration config, we will use our new source instead of | |
// original source from JSON. | |
// For individual paragraphs source would be 'scopes' and | |
// for main content where we import multiple values using | |
// migration_loop, we will use scopes inside data in fields | |
// and id for iterator. | |
$selectors = explode('/', trim($this->itemSelector, '/')); | |
foreach ($selectors as $selector) { | |
if (!empty($selector)) { | |
$source_data = $source_data[$selector]; | |
} | |
} | |
return $source_data; | |
} | |
/** | |
* Process source data. | |
* | |
* Add scopes as separate root element and scope data with proper ids in | |
* main content. | |
* | |
* @param array $source_data | |
* Source data. | |
* | |
* @return array | |
* Processed source data to add scopes as separate root element and scopes | |
* data with proper ids in main content. | |
*/ | |
protected function processSourceData(array $source_data): array { | |
$scopes = []; | |
foreach ($source_data['data'] as &$row) { | |
$id = $row['id']; | |
foreach ($row['scope'] ?? [] as $delta => $data) { | |
// Unique ID for each multi-value data item. | |
$scope_id = $id . '-' . $delta; | |
$scope = ['id' => $scope_id]; | |
foreach ($data as $item) { | |
$scope[$item['key']] = $item['value']; | |
} | |
// This will be used in migration of individual paragraphs. | |
$scopes[] = $scope; | |
// This will be used in migration of main content. | |
$row['scopes'][] = [ | |
'id' => $scope_id, | |
]; | |
} | |
} | |
$source_data['scopes'] = $scopes; | |
return $source_data; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment