You are here

public static function Consumer::baseFieldDefinitions in Consumers 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/Consumer.php, line 95

Class

Consumer
Defines the Consumer entity.

Namespace

Drupal\consumers\Entity

Code

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
  $fields = parent::baseFieldDefinitions($entity_type);
  $fields += static::ownerBaseFieldDefinitions($entity_type);
  $fields['label'] = BaseFieldDefinition::create('string')
    ->setLabel(new TranslatableMarkup('Label'))
    ->setDescription(new TranslatableMarkup('The consumer label.'))
    ->setRequired(TRUE)
    ->setTranslatable(TRUE)
    ->setRevisionable(TRUE)
    ->setSetting('max_length', 255)
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'string',
    'weight' => -5,
  ])
    ->setDisplayOptions('form', [
    'type' => 'string_textfield',
    'weight' => -5,
  ])
    ->setDisplayConfigurable('form', TRUE);
  $fields['description'] = BaseFieldDefinition::create('string_long')
    ->setLabel(t('Description'))
    ->setDescription(t('A description of the consumer. This text will be shown to the users to authorize sharing their data to create an access token.'))
    ->setTranslatable(TRUE)
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'string',
    'weight' => 0,
  ])
    ->setDisplayConfigurable('view', TRUE)
    ->setDisplayOptions('form', [
    'type' => 'string_textfield',
    'weight' => 0,
  ])
    ->setDisplayConfigurable('form', TRUE);
  $fields['image'] = BaseFieldDefinition::create('image')
    ->setLabel(t('Logo'))
    ->setDescription(t('Logo of the consumer.'))
    ->setRevisionable(TRUE)
    ->setTranslatable(TRUE)
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'image',
    'weight' => -3,
  ])
    ->setDisplayOptions('form', [
    'type' => 'image_image',
    'weight' => -3,
    'settings' => [
      'preview_image_style' => 'thumbnail',
      'progress_indicator' => 'throbber',
    ],
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', TRUE);
  $fields['third_party'] = BaseFieldDefinition::create('boolean')
    ->setLabel(new TranslatableMarkup('Is this consumer 3rd party?'))
    ->setDescription(new TranslatableMarkup('Mark this if the organization behind this consumer is not the same as the one behind the Drupal API.'))
    ->setDisplayOptions('view', [
    'label' => 'inline',
    'type' => 'boolean',
    'weight' => 4,
  ])
    ->setDisplayOptions('form', [
    'weight' => 4,
  ])
    ->setRevisionable(TRUE)
    ->setTranslatable(TRUE)
    ->setDefaultValue(TRUE);
  $fields['is_default'] = BaseFieldDefinition::create('boolean')
    ->setLabel(new TranslatableMarkup('Is this the default consumer?'))
    ->setDescription(new TranslatableMarkup('There can only be one default consumer. Mark this to use this consumer when none other applies.'))
    ->setDisplayOptions('view', [
    'label' => 'inline',
    'type' => 'boolean',
    'weight' => 4,
  ])
    ->setDisplayOptions('form', [
    'weight' => 4,
  ])
    ->setRevisionable(TRUE)
    ->setTranslatable(TRUE)
    ->setDefaultValue(FALSE);
  return $fields;
}