SimpleSourceTrait.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace Drupal\reinstall\Plugin\migrate\source;
  3. /**
  4. * Class SimpleSourceTrait provides shared features for entity field handling.
  5. */
  6. trait SimpleSourceTrait {
  7. /**
  8. * The entity_type.bundle.info service.
  9. *
  10. * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
  11. */
  12. protected $sstEntityTypeBundleInfo;
  13. /**
  14. * The entity_field.manager service.
  15. *
  16. * @var \Drupal\Core\Entity\EntityFieldManagerInterface
  17. */
  18. protected $sstEntityFieldManager;
  19. /**
  20. * The name of the entity type being handled.
  21. *
  22. * @var string
  23. */
  24. protected $sstEntityType;
  25. /**
  26. * The entity_type.manager service.
  27. *
  28. * @var \Drupal\Core\Entity\EntityTypeManagerInterface
  29. */
  30. protected $sstEntityTypeManager;
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function fields() {
  35. $bundles = $this->getEntityTypeBundleInfo()
  36. ->getBundleInfo($typeName = $this->getEntityType());
  37. $rows = [];
  38. foreach ($bundles as $bundleName => $bundle) {
  39. $fieldDefinitions = $this->getEntityFieldManager()
  40. ->getFieldDefinitions($typeName, $bundleName);
  41. foreach ($fieldDefinitions as $fieldName => $fieldDefinition) {
  42. $rows[$fieldName][$bundleName] = $fieldDefinition->getLabel();
  43. }
  44. }
  45. $fields = [];
  46. $singleBundle = count($bundles) === 1;
  47. foreach ($rows as $fieldName => $labels) {
  48. if ($singleBundle) {
  49. $fields[$fieldName] = reset($labels);
  50. continue;
  51. }
  52. if (count(array_unique($labels)) === 1) {
  53. $fields[$fieldName] = reset($labels);
  54. continue;
  55. }
  56. $ret = [];
  57. ksort($labels);
  58. foreach ($labels as $ct => $label) {
  59. $ret[] = $this->t('@ct: @label', ['@ct' => $ct, '@label' => $label]);
  60. }
  61. $fields[$fieldName] = implode(', ', $ret);
  62. }
  63. ksort($fields);
  64. return $fields;
  65. }
  66. /**
  67. * Getter for the entity_type.bundle.info service.
  68. *
  69. * @return \Drupal\Core\Entity\EntityTypeBundleInfoInterface
  70. * The service.
  71. */
  72. protected function getEntityTypeBundleInfo() {
  73. if (!isset($this->sstEntityTypeBundleInfo)) {
  74. $this->sstEntityTypeBundleInfo = \Drupal::service('entity_type.bundle.info');
  75. }
  76. return $this->sstEntityTypeBundleInfo;
  77. }
  78. /**
  79. * Getter for the entity_field.manager service.
  80. *
  81. * @return \Drupal\Core\Entity\EntityFieldManagerInterface|mixed
  82. * The service.
  83. */
  84. protected function getEntityFieldManager() {
  85. if (!isset($this->sstEntityFieldManager)) {
  86. $this->sstEntityFieldManager = \Drupal::service('entity_field.manager');
  87. }
  88. return $this->sstEntityFieldManager;
  89. }
  90. /**
  91. * Getter for the current entity type.
  92. *
  93. * @return string
  94. * The machine name of the type.
  95. */
  96. protected function getEntityType() {
  97. assert(isset($this->sstEntityType));
  98. return $this->sstEntityType;
  99. }
  100. /**
  101. * Getter for the entity_type.manager service.
  102. *
  103. * @return \Drupal\Core\Entity\EntityTypeManagerInterface|mixed
  104. * The service.
  105. */
  106. protected function getEntityTypeManager() {
  107. if (!isset($this->sstEntityTypeManager)) {
  108. $this->sstEntityTypeManager = \Drupal::service('entity_type.manager');
  109. }
  110. return $this->sstEntityTypeManager;
  111. }
  112. /**
  113. * {@inheritdoc}
  114. */
  115. public function getIds() {
  116. $typeName = $this->getEntityType();
  117. $typeDefinition = $this->getEntityTypeManager()->getDefinition($typeName);
  118. $idName = $typeDefinition->getKey('id');
  119. assert(!empty($idName));
  120. $definitions = $this->getEntityFieldManager()->getBaseFieldDefinitions($typeName);
  121. assert(isset($definitions[$idName]));
  122. $idDefinition = $definitions[$idName];
  123. $idType = $idDefinition->getType();
  124. $ids = [
  125. $idName => [
  126. 'type' => $idType,
  127. ],
  128. ];
  129. return $ids;
  130. }
  131. }