You are here

public static function GroupContent::bundleFieldDefinitions in Group 8

Same name and namespace in other branches
  1. 2.0.x src/Entity/GroupContent.php \Drupal\group\Entity\GroupContent::bundleFieldDefinitions()

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

src/Entity/GroupContent.php, line 347

Class

GroupContent
Defines the Group content entity.

Namespace

Drupal\group\Entity

Code

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

  // Borrowed this logic from the Comment module.
  // Warning! May change in the future: https://www.drupal.org/node/2346347
  if ($group_content_type = GroupContentType::load($bundle)) {
    $plugin = $group_content_type
      ->getContentPlugin();

    /** @var \Drupal\Core\Field\BaseFieldDefinition $original */
    $original = $base_field_definitions['entity_id'];

    // Recreated the original entity_id field so that it does not contain any
    // data in its "propertyDefinitions" or "schema" properties because those
    // were set based on the base field which had no clue what bundle to serve
    // up until now. This is a bug in core because we can't simply unset those
    // two properties, see: https://www.drupal.org/node/2346329
    $fields['entity_id'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel($plugin
      ->getEntityReferenceLabel() ?: $original
      ->getLabel())
      ->setDescription($plugin
      ->getEntityReferenceDescription() ?: $original
      ->getDescription())
      ->setConstraints($original
      ->getConstraints())
      ->setDisplayOptions('view', $original
      ->getDisplayOptions('view'))
      ->setDisplayOptions('form', $original
      ->getDisplayOptions('form'))
      ->setDisplayConfigurable('view', $original
      ->isDisplayConfigurable('view'))
      ->setDisplayConfigurable('form', $original
      ->isDisplayConfigurable('form'))
      ->setRequired($original
      ->isRequired());
    foreach ($plugin
      ->getEntityReferenceSettings() as $name => $setting) {
      $fields['entity_id']
        ->setSetting($name, $setting);
    }
    return $fields;
  }
  return [];
}