You are here

public function EntityReference::getTargetBundleId in Reference Table Formatter 8

Get the target bundle from a reference field.

Parameters

\Drupal\Core\Field\FieldDefinitionInterface $field_definition: The field definition to check the target bundle.

Return value

string The bundle that is the target of the field.

Throws

\Exception

Overrides FormatterInterface::getTargetBundleId

File

src/Plugin/Field/FieldFormatter/EntityReference.php, line 33

Class

EntityReference
A field formatter to display a table.

Namespace

Drupal\reference_table_formatter\Plugin\Field\FieldFormatter

Code

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'])) {

      // Default to the first bundle, currently only supporting a single
      // bundle.
      $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 {

    // Since we are only supporting rendering a single bundle, we wont know
    // what bundle we are rendering if users aren't using the default
    // selection, which is a simple configuration form.
    throw new \Exception('Using non-default reference handler with reference_table_formatter has not yet been implemented.');
  }
  return $target_bundle;
}