You are here

public static function Deployment::baseFieldDefinitions in Build Hooks 8.2

Same name and namespace in other branches
  1. 3.x src/Entity/Deployment.php \Drupal\build_hooks\Entity\Deployment::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/Deployment.php, line 73

Class

Deployment
Defines an entity to track a deployment.

Namespace

Drupal\build_hooks\Entity

Code

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
  $fields = parent::baseFieldDefinitions($entity_type);
  $fields['label'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Label'))
    ->setRequired(TRUE)
    ->setTranslatable(TRUE)
    ->setRevisionable(TRUE)
    ->setSetting('max_length', 255)
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'string',
    'weight' => -5,
  ])
    ->setDisplayConfigurable('view', TRUE)
    ->setDisplayConfigurable('form', TRUE);
  $fields['status'] = BaseFieldDefinition::create('boolean')
    ->setLabel(new TranslatableMarkup('Deployed'))
    ->setRevisionable(TRUE)
    ->setDefaultValue(FALSE);

  // Add the revision metadata fields.
  $fields += static::revisionLogBaseFieldDefinitions($entity_type);
  $fields['created'] = BaseFieldDefinition::create('created')
    ->setLabel(t('Created'))
    ->setDescription(t('The time the deployment was created.'))
    ->setRevisionable(TRUE)
    ->setDisplayConfigurable('view', TRUE)
    ->setDisplayConfigurable('form', TRUE);
  $fields['deployed'] = BaseFieldDefinition::create('timestamp')
    ->setLabel(t('Created'))
    ->setDescription(t('The time the deployment was deployed.'))
    ->setRevisionable(TRUE)
    ->setDisplayConfigurable('view', TRUE)
    ->setDisplayConfigurable('form', TRUE);
  $fields['changed'] = BaseFieldDefinition::create('changed')
    ->setLabel(t('Changed'))
    ->setDescription(t('The time that the deployment was last edited.'))
    ->setRevisionable(TRUE)
    ->setDisplayConfigurable('view', TRUE)
    ->setDisplayConfigurable('form', TRUE);
  $fields['contents'] = BaseFieldDefinition::create('dynamic_entity_reference')
    ->setLabel((string) new TranslatableMarkup('Deployment contents'))
    ->setDescription((string) new TranslatableMarkup('Content entities updated since the last deployment.'))
    ->setRequired(FALSE)
    ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
    ->setDisplayOptions('view', [
    'label' => 'above',
    'type' => 'dynamic_entity_reference_label',
    'weight' => 20,
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', TRUE)
    ->setSettings([
    'exclude_entity_types' => TRUE,
    'entity_type_ids' => [
      'build_hooks_deployment',
    ],
  ]);
  $fields['deleted'] = BaseFieldDefinition::create('string')
    ->setLabel((string) new TranslatableMarkup('Deleted items'))
    ->setDescription((string) new TranslatableMarkup('Content entities deleted since the last deployment.'))
    ->setRequired(FALSE)
    ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
    ->setDisplayOptions('view', [
    'label' => 'above',
    'type' => 'string',
    'weight' => 20,
  ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', TRUE);
  return $fields;
}