You are here

public static function Connection::bundleFieldDefinitions in RedHen CRM 8

Provides field definitions for a specific bundle.

This function can return definitions both for bundle fields (fields that are not defined in $base_field_definitions, and therefore might not exist on some bundles) as well as bundle-specific overrides of base fields (fields that are defined in $base_field_definitions, and therefore exist for all bundles). However, bundle-specific base field overrides can also be provided by 'base_field_override' configuration entities, and that is the recommended approach except in cases where an entity type needs to provide a bundle-specific base field override that is decoupled from configuration. Note that for most entity types, the bundles themselves are derived from configuration (e.g., 'node' bundles are managed via 'node_type' configuration entities), so decoupling bundle-specific base field overrides from configuration only makes sense for entity types that also decouple their bundles from configuration. In cases where both this function returns a bundle-specific override of a base field and a 'base_field_override' configuration entity exists, the latter takes precedence.

@todo WARNING: This method will be changed in https://www.drupal.org/node/2346347.

Parameters

\Drupal\Core\Entity\EntityTypeInterface $entity_type: The entity type definition. Useful when a single class is used for multiple, possibly dynamic entity types.

string $bundle: The bundle.

\Drupal\Core\Field\FieldDefinitionInterface[] $base_field_definitions: The list of base field definitions.

Return value

\Drupal\Core\Field\FieldDefinitionInterface[] An array of bundle field definitions, keyed by field name.

Overrides ContentEntityBase::bundleFieldDefinitions

See also

\Drupal\Core\Entity\EntityFieldManagerInterface::getFieldDefinitions()

\Drupal\Core\Entity\FieldableEntityInterface::baseFieldDefinitions()

File

modules/redhen_connection/src/Entity/Connection.php, line 189

Class

Connection
Defines the Connection entity.

Namespace

Drupal\redhen_connection\Entity

Code

public static function bundleFieldDefinitions(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) {

  /** @var \Drupal\redhen_connection\ConnectionTypeInterface $connection_type */
  $connection_type = ConnectionType::load($bundle);
  $fields = [];

  // Set bundle specific settings for each of our endpoint fields.
  for ($x = 1; $x <= REDHEN_CONNECTION_ENDPOINTS; $x++) {

    /** @var \Drupal\Core\Field\BaseFieldDefinition $fields[$field] */
    $endpoint_type = $connection_type
      ->getEndpointEntityTypeId($x);
    $field = 'endpoint_' . $x;
    $fields[$field] = clone $base_field_definitions[$field];
    if ($endpoint_type) {
      $bundles = $connection_type
        ->getEndpointBundles($x);
      $endpoint_entity = \Drupal::entityTypeManager()
        ->getDefinition($endpoint_type);
      $label = !empty($connection_type
        ->getEndpointLabel($x)) ? $connection_type
        ->getEndpointLabel($x) : $endpoint_entity
        ->getLabel();
      $fields[$field]
        ->setSetting('target_type', $endpoint_type)
        ->setLabel($label);
      if (!empty($connection_type
        ->getEndpointDescription($x))) {
        $fields[$field]
          ->setDescription($connection_type
          ->getEndpointDescription($x));
      }
      if (!empty($bundles)) {
        $fields[$field]
          ->setSetting('handler_settings', [
          'target_bundles' => $bundles,
        ]);
      }
    }
  }
  $fields['role'] = clone $base_field_definitions['role'];
  $fields['role']
    ->setSetting('handler_settings', [
    'connection_type' => $connection_type
      ->id(),
  ]);
  return $fields;
}