ReinstallTermSource.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace Drupal\reinstall\Plugin\migrate\source;
  3. use Drupal\migrate\Plugin\MigrationInterface;
  4. use Drupal\migrate\Row;
  5. /**
  6. * Source plugin for terms from a YAML file.
  7. *
  8. * @MigrateSource(
  9. * id = "reinstall_terms"
  10. * )
  11. */
  12. class ReinstallTermSource extends SimpleSource {
  13. /**
  14. * Constructor.
  15. */
  16. public function __construct(
  17. array $configuration,
  18. string $pluginId,
  19. array $pluginDefinition,
  20. MigrationInterface $migration
  21. ) {
  22. parent::__construct($configuration, $pluginId, $pluginDefinition, $migration);
  23. $rawRecords = array_map([$this, 'flattenRecord'], $this->initialParse($configuration));
  24. $this->records = $rawRecords;
  25. }
  26. protected function flattenRecord($record) {
  27. $row = new Row($record);
  28. $this->flattenRow($row);
  29. return $row->getSource();
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function fields() {
  35. $ret = [
  36. 'changed' => 'Modification timestamp',
  37. 'default_langcode' => 'ISO 639 default language code',
  38. 'description' => 'The entity description',
  39. 'langcode' => 'ISO 639 language code',
  40. 'name' => 'The term name (PK)',
  41. 'parent' => 'Parent term ID',
  42. 'path' => 'Optional path alias',
  43. 'tid' => 'Taxonomy term ID',
  44. 'uuid' => 'UUID',
  45. 'vid' => 'Vocabulary ID',
  46. 'weight' => 'Poids',
  47. ];
  48. return $ret;
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function getIds() {
  54. $ids = [
  55. 'tid' => [
  56. 'type' => 'integer',
  57. ],
  58. ];
  59. return $ids;
  60. }
  61. /**
  62. * Obtain the term parents from either of their two formats.
  63. *
  64. * @param mixed $source
  65. * A parent, from the migration pipeline.
  66. *
  67. * @return mixed
  68. * The parent, as its target_id.
  69. *
  70. * @see (reinstall)/README.md
  71. */
  72. public static function getParent($source) {
  73. if (is_scalar($source)) {
  74. return $source;
  75. }
  76. elseif (isset($source['target_id'])) {
  77. return $source['target_id'];
  78. }
  79. }
  80. }