protected function GoogleSheets::getSourceData in Migrate Google Sheets 2.x
Same name and namespace in other branches
- 8 src/Plugin/migrate_plus/data_parser/GoogleSheets.php \Drupal\migrate_google_sheets\Plugin\migrate_plus\data_parser\GoogleSheets::getSourceData()
File
- src/
Plugin/ migrate_plus/ data_parser/ GoogleSheets.php, line 35
Class
- GoogleSheets
- Obtain Google Sheet data for migration.
Namespace
Drupal\migrate_google_sheets\Plugin\migrate_plus\data_parserCode
protected function getSourceData($url) {
// Since we're being explicit about the data location, we can return the
// array without calling getSourceIterator to get an iterator to find the
// correct values.
try {
$response = $this
->getDataFetcherPlugin()
->getResponseContent($url);
// Response returns json wrapped in a function call, first 47 and last 2 chars.
$response = substr($response, 47, strlen($response) - 47 - 2);
// The TRUE setting means decode the response into an associative array.
$array = json_decode($response, TRUE);
// For Google Sheets, the actual row data lives under table->rows.
if (isset($array['table']) && isset($array['table']['rows'])) {
if (isset($array['table']['cols']) && $array['table']['parsedNumHeaders'] > 0) {
// Set headers based on column labels.
$columns = array_column($array['table']['cols'], 'label');
}
else {
// Set headers from first row.
$first_row = array_shift($array['table']['rows']);
$columns = array_column($first_row['c'], 'v');
}
$this->headers = array_map(function ($col) {
return strtolower($col);
}, $columns);
$array = $array['table']['rows'];
}
else {
$array = [];
}
return $array;
} catch (RequestException $e) {
throw new MigrateException($e
->getMessage(), $e
->getCode(), $e);
}
}