EntityReference.php in Reference Table Formatter 8
File
src/Plugin/Field/FieldFormatter/EntityReference.php
View source
<?php
namespace Drupal\reference_table_formatter\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\reference_table_formatter\FormatterBase;
class EntityReference extends FormatterBase {
public function getEntityIdFromFieldItem(FieldItemInterface $item) {
return $item
->getValue()['target_id'];
}
public function getTargetBundleId(FieldDefinitionInterface $field_definition) {
$definition_settings = $field_definition
->getSettings();
if (strpos($definition_settings['handler'], 'default') === 0) {
$target_entity_type = $this->entityManager
->getDefinition($this
->getTargetEntityId($field_definition));
if (!$target_entity_type
->hasKey('bundle')) {
$target_bundle = $definition_settings['target_type'];
}
elseif (!empty($definition_settings['handler_settings']['target_bundles'])) {
$target_bundle = array_values($definition_settings['handler_settings']['target_bundles']);
$target_bundle = array_shift($target_bundle);
}
else {
throw new \Exception('Cannot render reference table for ' . $this->fieldDefinition
->getLabel() . ': target_bundles setting on the field should not be empty.');
}
}
else {
throw new \Exception('Using non-default reference handler with reference_table_formatter has not yet been implemented.');
}
return $target_bundle;
}
public function getTargetEntityId(FieldDefinitionInterface $field_definition) {
return $field_definition
->getFieldStorageDefinition()
->getSetting('target_type');
}
}