SimpleSourceTrait.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: fgm
  5. * Date: 11/04/2017
  6. * Time: 08:35
  7. */
  8. namespace Drupal\reinstall\Plugin\migrate\source;
  9. trait SimpleSourceTrait {
  10. /**
  11. * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
  12. */
  13. protected $sstEntityTypeBundleInfo;
  14. /**
  15. * @var \Drupal\Core\Entity\EntityFieldManagerInterface
  16. */
  17. protected $sstEntityFieldManager;
  18. /**
  19. * @var string
  20. */
  21. protected $sstEntityType;
  22. /**
  23. * @var \Drupal\Core\Entity\EntityTypeManagerInterface
  24. */
  25. protected $sstEntityTypeManager;
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public function fields() {
  30. $bundles = $this->getEntityTypeBundleInfo()
  31. ->getBundleInfo($typeName = $this->getEntityType());
  32. $rows = [];
  33. foreach ($bundles as $bundleName => $bundle) {
  34. $fieldDefinitions = $this->getEntityFieldManager()
  35. ->getFieldDefinitions($typeName, $bundleName);
  36. foreach ($fieldDefinitions as $fieldName => $fieldDefinition) {
  37. $rows[$fieldName][$bundleName] = $fieldDefinition->getLabel();
  38. }
  39. }
  40. $fields = [];
  41. $singleBundle = count($bundles) === 1;
  42. foreach ($rows as $fieldName => $labels) {
  43. if ($singleBundle) {
  44. $fields[$fieldName] = reset($labels);
  45. continue;
  46. }
  47. if (count(array_unique($labels)) === 1) {
  48. $fields[$fieldName] = reset($labels);
  49. continue;
  50. }
  51. $ret = [];
  52. ksort($labels);
  53. foreach ($labels as $ct => $label) {
  54. $ret[] = $this->t('@ct: @label', ['@ct' => $ct, '@label' => $label]);
  55. }
  56. $fields[$fieldName] = implode(', ', $ret);
  57. }
  58. ksort($fields);
  59. return $fields;
  60. }
  61. protected function getEntityTypeBundleInfo() {
  62. if (!isset($this->sstEntityTypeBundleInfo)) {
  63. $this->sstEntityTypeBundleInfo = \Drupal::service('entity_type.bundle.info');
  64. }
  65. return $this->sstEntityTypeBundleInfo;
  66. }
  67. protected function getEntityFieldManager() {
  68. if (!isset($this->sstEntityFieldManager)) {
  69. $this->sstEntityFieldManager = \Drupal::service('entity_field.manager');
  70. }
  71. return $this->sstEntityFieldManager;
  72. }
  73. protected function getEntityType() {
  74. assert(isset($this->sstEntityType));
  75. return $this->sstEntityType;
  76. }
  77. protected function getEntityTypeManager() {
  78. if (!isset($this->sstEntityTypeManager)) {
  79. $this->sstEntityTypeManager = \Drupal::service('entity_type.manager');
  80. }
  81. return $this->sstEntityTypeManager;
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. public function getIds() {
  87. $typeName = $this->getEntityType();
  88. $typeDefinition = $this->getEntityTypeManager()->getDefinition($typeName);
  89. $idName = $typeDefinition->getKey('id');
  90. assert(!empty($idName));
  91. $definitions = $this->getEntityFieldManager()->getBaseFieldDefinitions($typeName);
  92. assert(isset($definitions[$idName]));
  93. $idDefinition = $definitions[$idName];
  94. $idType = $idDefinition->getType();
  95. $ids = [
  96. $idName => [
  97. 'type' => $idType,
  98. ],
  99. ];
  100. return $ids;
  101. }
  102. }