You are here

public static function OgMembership::baseFieldDefinitions in Organic groups 8

Provides base field definitions for an entity type.

Implementations typically use the class \Drupal\Core\Field\BaseFieldDefinition for creating the field definitions; for example a 'name' field could be defined as the following:

$fields['name'] = BaseFieldDefinition::create('string')
  ->setLabel(t('Name'));

By definition, base fields are fields that exist for every bundle. To provide definitions for fields that should only exist on some bundles, use \Drupal\Core\Entity\FieldableEntityInterface::bundleFieldDefinitions().

The definitions returned by this function can be overridden for all bundles by hook_entity_base_field_info_alter() or overridden on a per-bundle basis via 'base_field_override' configuration entities.

Parameters

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

Return value

\Drupal\Core\Field\FieldDefinitionInterface[] An array of base field definitions for the entity type, keyed by field name.

Overrides ContentEntityBase::baseFieldDefinitions

See also

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

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

File

src/Entity/OgMembership.php, line 370

Class

OgMembership
The membership entity that connects a group and a user.

Namespace

Drupal\og\Entity

Code

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
  $fields = [];
  $fields['id'] = BaseFieldDefinition::create('integer')
    ->setLabel(new TranslatableMarkup('ID'))
    ->setDescription(t("The group membership's unique ID."))
    ->setReadOnly(TRUE)
    ->setSetting('unsigned', TRUE);
  $fields['uuid'] = BaseFieldDefinition::create('uuid')
    ->setLabel(new TranslatableMarkup('UUID'))
    ->setDescription(new TranslatableMarkup('The membership UUID.'))
    ->setReadOnly(TRUE);
  $fields['type'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(new TranslatableMarkup('Type'))
    ->setDescription(new TranslatableMarkup('The bundle of the membership'))
    ->setSetting('target_type', 'og_membership_type');
  $fields['uid'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(new TranslatableMarkup('Member User ID'))
    ->setDescription(new TranslatableMarkup('The user ID of the member.'))
    ->setSetting('target_type', 'user');
  $fields['entity_type'] = BaseFieldDefinition::create('string')
    ->setLabel(new TranslatableMarkup('Group entity type'))
    ->setDescription(new TranslatableMarkup('The entity type of the group.'));
  $fields['entity_bundle'] = BaseFieldDefinition::create('string')
    ->setLabel(new TranslatableMarkup('Group bundle ID'))
    ->setDescription(new TranslatableMarkup('The bundle ID of the group.'));
  $fields['entity_id'] = BaseFieldDefinition::create('string')
    ->setLabel(new TranslatableMarkup('Group entity ID'))
    ->setDescription(new TranslatableMarkup('The entity ID of the group.'));
  $fields['state'] = BaseFieldDefinition::create('string')
    ->setLabel(new TranslatableMarkup('State'))
    ->setDescription(new TranslatableMarkup('The user membership state: active, pending, or blocked.'))
    ->setDefaultValue(OgMembershipInterface::STATE_ACTIVE);
  $fields['roles'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(new TranslatableMarkup('Roles'))
    ->setDescription(new TranslatableMarkup('The OG roles related to an OG membership entity.'))
    ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'entity_reference_label',
    'weight' => 0,
  ])
    ->setSetting('target_type', 'og_role');
  $fields['created'] = BaseFieldDefinition::create('created')
    ->setLabel(new TranslatableMarkup('Create'))
    ->setDescription(new TranslatableMarkup('The Unix timestamp when the membership was created.'));
  $fields['language'] = BaseFieldDefinition::create('language')
    ->setLabel(new TranslatableMarkup('Language'))
    ->setDescription(new TranslatableMarkup('The {languages}.language of this membership.'));
  return $fields;
}