You are here

protected function EntityReferenceTableFormatter::getTargetBundleId in Reference Table Formatter 2.0.x

Get the target bundle from a reference field.

Return value

string The bundle that is the target of the field.

Throws

\Exception

1 call to EntityReferenceTableFormatter::getTargetBundleId()
EntityReferenceTableFormatter::viewElements in src/Plugin/Field/FieldFormatter/EntityReferenceTableFormatter.php
Builds a renderable array for a field value.

File

src/Plugin/Field/FieldFormatter/EntityReferenceTableFormatter.php, line 209

Class

EntityReferenceTableFormatter
A field formatter to display a table.

Namespace

Drupal\reference_table_formatter\Plugin\Field\FieldFormatter

Code

protected function getTargetBundleId() {
  $settings = $this
    ->getFieldSettings();
  if (strpos($settings['handler'], 'default') === 0) {
    $target_entity_type = $this->entityTypeManager
      ->getDefinition($settings['target_type']);
    if (!$target_entity_type
      ->hasKey('bundle')) {
      $target_bundle = $settings['target_type'];
    }
    elseif (!empty($settings['handler_settings']['target_bundles'])) {

      // Default to the first bundle, currently only supporting a single
      // bundle.
      $target_bundle = array_values($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;
}