getParameter('reinstall.path'); $configuration['importPath'] = $importPath; return new static($configuration, $pluginId, $pluginDefinition, $migration); } /** * {@inheritdoc} */ public function doCount() { return count($this->records); } /** * Flatten a typical Drupal 8 field array to a 1-level array. */ protected function flattenRow(Row $row) { $source = $row->getSource(); foreach ($source as $key => &$item_list) { if (is_scalar($item_list)) { continue; } if (count($item_list) > 1) { $item = $item_list; } else { $item = reset($item_list); } if (isset($item['target_id'])) { $value = $item['target_id']; } elseif (is_scalar($item) || (count($item) != 1 && !isset($item['width']) && !isset($item['pid']))) { $value = $item; } elseif (isset($item['value'])) { $value = $item['value']; } // Handle bundle['target_id'] // Exclude image field to keep metadata (alt / title) elseif (isset($item['target_id']) && !isset($item['alt']) && !isset($item['title'])) { $value = $item['target_id']; } elseif (isset($item['pid'])) { $value = $item['alias']; } else { $value = $item; } if (empty($item)) { $value = NULL; } $row->setSourceProperty($key, $value); } } /** * {@inheritdoc} */ protected function initializeIterator() { return new \ArrayIterator($this->records); } /** * Load then parse the file requested in configuration and return its records. * * @param array $configuration * * @param string $key * Optional. A top-level key for the source document. If empty, items will * be parsed from the root of the source document. * * * @return mixed * * @throws \Drupal\migrate\MigrateException */ protected function initialParse(array $configuration, string $key = NULL) { $baseFilePath = $configuration['file'] ?? NULL; $importPath = $configuration['importPath'] ?? NULL; $filePath = realpath("$importPath/$baseFilePath"); if (!is_file($filePath) || !is_readable($filePath)) { throw new MigrateException("${filePath} is not a readable file."); } try { $raw = file_get_contents($filePath); $data = Yaml::parse($raw); } catch (ParseException $e) { throw new MigrateException("Cannot parse the contents of ${filePath}."); } if ($key) { return $data[$key] ?? []; } return $data ?? []; } /** * {@inheritdoc} */ public function __toString() { $current = $this->getIterator()->current(); $ret = json_encode($current, JSON_PRETTY_PRINT); return $ret; } }