EntityReferenceExport.php in Entity Export CSV 8
File
src/Plugin/FieldTypeExport/EntityReferenceExport.php
View source
<?php
namespace Drupal\entity_export_csv\Plugin\FieldTypeExport;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\entity_export_csv\Plugin\FieldTypeExportBase;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Field\FieldItemInterface;
class EntityReferenceExport extends FieldTypeExportBase {
public function getSummary() {
return [
'message' => [
'#markup' => $this
->t('Entity reference field type exporter.'),
],
];
}
public function massageExportPropertyValue(FieldItemInterface $field_item, $property_name, FieldDefinitionInterface $field_definition, $options = []) {
if ($field_item
->isEmpty()) {
return NULL;
}
$configuration = $this
->getConfiguration();
if (empty($configuration['format'])) {
return $field_item
->get($property_name)
->getValue();
}
$format = $configuration['format'];
if ($format === 'entity_reference_label') {
$entity = $field_item
->get('entity')
->getValue();
if ($entity instanceof EntityInterface) {
return $entity
->label();
}
}
return $field_item
->get($property_name)
->getValue();
}
protected function getFormatExportOptions(FieldDefinitionInterface $field_definition) {
$options = parent::getFormatExportOptions($field_definition);
$options['entity_reference_label'] = $this
->t('Label');
return $options;
}
}