123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- <?php
- /**
- * Created by PhpStorm.
- * User: fgm
- * Date: 11/04/2017
- * Time: 08:35
- */
- namespace Drupal\reinstall\Plugin\migrate\source;
- trait SimpleSourceTrait {
- /**
- * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
- */
- protected $sstEntityTypeBundleInfo;
- /**
- * @var \Drupal\Core\Entity\EntityFieldManagerInterface
- */
- protected $sstEntityFieldManager;
- /**
- * @var string
- */
- protected $sstEntityType;
- /**
- * @var \Drupal\Core\Entity\EntityTypeManagerInterface
- */
- protected $sstEntityTypeManager;
- /**
- * {@inheritdoc}
- */
- public function fields() {
- $bundles = $this->getEntityTypeBundleInfo()
- ->getBundleInfo($typeName = $this->getEntityType());
- $rows = [];
- foreach ($bundles as $bundleName => $bundle) {
- $fieldDefinitions = $this->getEntityFieldManager()
- ->getFieldDefinitions($typeName, $bundleName);
- foreach ($fieldDefinitions as $fieldName => $fieldDefinition) {
- $rows[$fieldName][$bundleName] = $fieldDefinition->getLabel();
- }
- }
- $fields = [];
- $singleBundle = count($bundles) === 1;
- foreach ($rows as $fieldName => $labels) {
- if ($singleBundle) {
- $fields[$fieldName] = reset($labels);
- continue;
- }
- if (count(array_unique($labels)) === 1) {
- $fields[$fieldName] = reset($labels);
- continue;
- }
- $ret = [];
- ksort($labels);
- foreach ($labels as $ct => $label) {
- $ret[] = $this->t('@ct: @label', ['@ct' => $ct, '@label' => $label]);
- }
- $fields[$fieldName] = implode(', ', $ret);
- }
- ksort($fields);
- return $fields;
- }
- protected function getEntityTypeBundleInfo() {
- if (!isset($this->sstEntityTypeBundleInfo)) {
- $this->sstEntityTypeBundleInfo = \Drupal::service('entity_type.bundle.info');
- }
- return $this->sstEntityTypeBundleInfo;
- }
- protected function getEntityFieldManager() {
- if (!isset($this->sstEntityFieldManager)) {
- $this->sstEntityFieldManager = \Drupal::service('entity_field.manager');
- }
- return $this->sstEntityFieldManager;
- }
- protected function getEntityType() {
- assert(isset($this->sstEntityType));
- return $this->sstEntityType;
- }
- protected function getEntityTypeManager() {
- if (!isset($this->sstEntityTypeManager)) {
- $this->sstEntityTypeManager = \Drupal::service('entity_type.manager');
- }
- return $this->sstEntityTypeManager;
- }
- /**
- * {@inheritdoc}
- */
- public function getIds() {
- $typeName = $this->getEntityType();
- $typeDefinition = $this->getEntityTypeManager()->getDefinition($typeName);
- $idName = $typeDefinition->getKey('id');
- assert(!empty($idName));
- $definitions = $this->getEntityFieldManager()->getBaseFieldDefinitions($typeName);
- assert(isset($definitions[$idName]));
- $idDefinition = $definitions[$idName];
- $idType = $idDefinition->getType();
- $ids = [
- $idName => [
- 'type' => $idType,
- ],
- ];
- return $ids;
- }
- }
|