You are here

public static function GroupContent::baseFieldDefinitions in Group 8

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

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/GroupContent.php, line 273

Class

GroupContent
Defines the Group content entity.

Namespace

Drupal\group\Entity

Code

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
  $fields = parent::baseFieldDefinitions($entity_type);
  $fields += static::ownerBaseFieldDefinitions($entity_type);
  $fields['gid'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Parent group'))
    ->setDescription(t('The group containing the entity.'))
    ->setSetting('target_type', 'group')
    ->setReadOnly(TRUE)
    ->setRequired(TRUE);

  // Borrowed this logic from the Comment module.
  // Warning! May change in the future: https://www.drupal.org/node/2346347
  $fields['entity_id'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Content'))
    ->setDescription(t('The entity to add to the group.'))
    ->setDisplayOptions('form', [
    'type' => 'entity_reference_autocomplete',
    'weight' => 5,
    'settings' => [
      'match_operator' => 'CONTAINS',
      'size' => '60',
      'placeholder' => '',
    ],
  ])
    ->setDisplayConfigurable('view', TRUE)
    ->setDisplayConfigurable('form', TRUE)
    ->setRequired(TRUE);
  $fields['label'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Title'))
    ->setReadOnly(TRUE)
    ->setTranslatable(TRUE)
    ->setSetting('max_length', 255)
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'string',
    'weight' => -5,
  ]);
  $fields['uid']
    ->setLabel(t('Group content creator'))
    ->setDescription(t('The username of the group content creator.'))
    ->setDisplayConfigurable('view', TRUE)
    ->setDisplayConfigurable('form', TRUE);
  $fields['created'] = BaseFieldDefinition::create('created')
    ->setLabel(t('Created on'))
    ->setDescription(t('The time that the group content was created.'))
    ->setTranslatable(TRUE);
  $fields['changed'] = BaseFieldDefinition::create('changed')
    ->setLabel(t('Changed on'))
    ->setDescription(t('The time that the group content was last edited.'))
    ->setTranslatable(TRUE);
  if (\Drupal::moduleHandler()
    ->moduleExists('path')) {
    $fields['path'] = BaseFieldDefinition::create('path')
      ->setLabel(t('URL alias'))
      ->setTranslatable(TRUE)
      ->setDisplayOptions('form', [
      'type' => 'path',
      'weight' => 30,
    ])
      ->setDisplayConfigurable('form', TRUE)
      ->setComputed(TRUE);
  }
  return $fields;
}