12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- <?php
- /**
- * Created by PhpStorm.
- * User: fgm
- * Date: 11/04/2017
- * Time: 08:35
- */
- namespace Drupal\reinstall\Plugin\migrate\source;
- trait SimpleSourceTrait {
- protected $sstEntityTypeBundleInfo;
- protected $sstEntityFieldManager;
- protected $sstEntityType;
- 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;
- }
- /**
- * {@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;
- }
- }
|