You are here

public static function ConsentAgreement::baseFieldDefinitions in General Data Protection Regulation 8.2

Same name and namespace in other branches
  1. 8 modules/gdpr_consent/src/Entity/ConsentAgreement.php \Drupal\gdpr_consent\Entity\ConsentAgreement::baseFieldDefinitions()
  2. 3.0.x modules/gdpr_consent/src/Entity/ConsentAgreement.php \Drupal\gdpr_consent\Entity\ConsentAgreement::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 RevisionableContentEntityBase::baseFieldDefinitions

See also

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

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

File

modules/gdpr_consent/src/Entity/ConsentAgreement.php, line 223

Class

ConsentAgreement
Defines the ConsentAgreement entity.

Namespace

Drupal\gdpr_consent\Entity

Code

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
  $fields = parent::baseFieldDefinitions($entity_type);
  $fields['title'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Title'))
    ->setRevisionable(TRUE)
    ->setTranslatable(TRUE)
    ->setDescription(t('Agreement title.'))
    ->setRequired(TRUE)
    ->setDisplayOptions('view', [
    'type' => 'textfield',
  ])
    ->setDisplayOptions('form', [
    'type' => 'textfield',
    'weight' => 0,
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', TRUE);
  $fields['mode'] = BaseFieldDefinition::create('list_string')
    ->setLabel(t('Agreement type'))
    ->setRevisionable(TRUE)
    ->setDescription(t('Whether consent is implicit or explicit. Set to "Explicit" if the user needs to explicitly agree, otherwise "Implicit".'))
    ->setDefaultValue('explicit')
    ->setSetting('allowed_values_function', [
    static::class,
    'getModes',
  ])
    ->setRequired(TRUE)
    ->setDisplayOptions('view', [
    'type' => 'select',
  ])
    ->setDisplayOptions('form', [
    'type' => 'select',
    'weight' => 1,
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', TRUE);
  $fields['description'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Description'))
    ->setRevisionable(TRUE)
    ->setTranslatable(TRUE)
    ->setDescription(t('Text displayed to the user on the form'))
    ->setRequired(TRUE)
    ->setDisplayOptions('view', [
    'type' => 'textfield',
  ])
    ->setDisplayOptions('form', [
    'type' => 'textfield',
    'weight' => 2,
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', TRUE);
  $fields['long_description'] = BaseFieldDefinition::create('string_long')
    ->setLabel(t('Long description'))
    ->setRevisionable(TRUE)
    ->setTranslatable(TRUE)
    ->setDescription(t('Text shown when the user clicks for more details.'))
    ->setDisplayOptions('view', [
    'type' => 'textarea',
  ])
    ->setDisplayOptions('form', [
    'type' => 'textarea',
    'weight' => 3,
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', TRUE);
  $fields['notes'] = BaseFieldDefinition::create('string_long')
    ->setLabel(t('Notes'))
    ->setRevisionable(TRUE)
    ->setTranslatable(TRUE)
    ->setDescription(t('This should contain the rationale behind the agreement.'))
    ->setDisplayOptions('view', [
    'type' => 'textarea',
  ])
    ->setDisplayOptions('form', [
    'type' => 'textarea',
    'weight' => 4,
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', TRUE);
  $fields['status'] = BaseFieldDefinition::create('boolean')
    ->setLabel(t('Publishing status'))
    ->setDescription(t('A boolean indicating whether the Consent Agreement is published.'))
    ->setRevisionable(TRUE)
    ->setDefaultValue(TRUE);
  $fields['created'] = BaseFieldDefinition::create('created')
    ->setLabel(t('Created'))
    ->setDescription(t('The time that the entity was created.'));
  $fields['changed'] = BaseFieldDefinition::create('changed')
    ->setLabel(t('Changed'))
    ->setDescription(t('The time that the entity was last edited.'));
  $fields['revision_translation_affected'] = BaseFieldDefinition::create('boolean')
    ->setLabel(t('Revision translation affected'))
    ->setDescription(t('Indicates if the last edit of a translation belongs to current revision.'))
    ->setReadOnly(TRUE)
    ->setRevisionable(TRUE)
    ->setTranslatable(TRUE);
  $fields['user_id'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Authored by'))
    ->setDescription(t('The user ID of author of the Consent Agreement entity.'))
    ->setRevisionable(TRUE)
    ->setSetting('target_type', 'user')
    ->setSetting('handler', 'default')
    ->setTranslatable(TRUE)
    ->setDisplayOptions('view', [
    'type' => 'author',
  ])
    ->setDisplayOptions('form', [
    'type' => 'hidden',
    'settings' => [
      'match_operator' => 'CONTAINS',
      'size' => '60',
      'autocomplete_type' => 'tags',
      'placeholder' => '',
    ],
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', TRUE);
  $fields['langcode'] = BaseFieldDefinition::create('language')
    ->setLabel(t('Language'))
    ->setRevisionable(TRUE)
    ->setTranslatable(TRUE)
    ->setDisplayOptions('view', [
    'region' => 'hidden',
  ])
    ->setDisplayOptions('form', [
    'type' => 'language_select',
    'weight' => 2,
  ]);
  return $fields;
}