You are here

public static function WebformSubmission::baseFieldDefinitions in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Entity/WebformSubmission.php \Drupal\webform\Entity\WebformSubmission::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/WebformSubmission.php, line 134

Class

WebformSubmission
Defines the WebformSubmission entity.

Namespace

Drupal\webform\Entity

Code

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
  $fields['serial'] = BaseFieldDefinition::create('integer')
    ->setLabel(t('Serial number'))
    ->setDescription(t('The serial number of the webform submission entity.'))
    ->setReadOnly(TRUE);
  $fields['sid'] = BaseFieldDefinition::create('integer')
    ->setLabel(t('Submission ID'))
    ->setDescription(t('The ID of the webform submission entity.'))
    ->setReadOnly(TRUE);
  $fields['uuid'] = BaseFieldDefinition::create('uuid')
    ->setLabel(t('Submission UUID'))
    ->setDescription(t('The UUID of the webform submission entity.'))
    ->setReadOnly(TRUE);
  $fields['token'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Token'))
    ->setDescription(t('A secure token used to look up a submission.'))
    ->setSetting('max_length', 255)
    ->setReadOnly(TRUE);
  $fields['uri'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Submission URI'))
    ->setDescription(t('The URI the user submitted the webform.'))
    ->setSetting('max_length', 2000)
    ->setReadOnly(TRUE);
  $fields['created'] = BaseFieldDefinition::create('created')
    ->setLabel(t('Created'))
    ->setDescription(t('The time that the webform submission was first saved as draft or submitted.'));
  $fields['completed'] = BaseFieldDefinition::create('timestamp')
    ->setLabel(t('Completed'))
    ->setDescription(t('The time that the webform submission was submitted as complete (not draft).'));
  $fields['changed'] = BaseFieldDefinition::create('changed')
    ->setLabel(t('Changed'))
    ->setDescription(t('The time that the webform submission was last saved (complete or draft).'));
  $fields['in_draft'] = BaseFieldDefinition::create('boolean')
    ->setLabel(t('Is draft'))
    ->setDescription(t('Is this a draft of the submission?'))
    ->setDefaultValue(FALSE);
  $fields['current_page'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Current page'))
    ->setDescription(t('The current wizard page.'))
    ->setSetting('max_length', 128);
  $fields['remote_addr'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Remote IP address'))
    ->setDescription(t('The IP address of the user that submitted the webform.'))
    ->setSetting('max_length', 128);
  $fields['uid'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Submitted by'))
    ->setDescription(t('The username of the user that submitted the webform.'))
    ->setSetting('target_type', 'user')
    ->setDefaultValueCallback('Drupal\\webform\\Entity\\WebformSubmission::getCurrentUserId');
  $fields['langcode'] = BaseFieldDefinition::create('language')
    ->setLabel(t('Language'))
    ->setDescription(t('The submission language code.'));
  $fields['webform_id'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Webform'))
    ->setDescription(t('The associated webform.'))
    ->setSetting('target_type', 'webform');
  $fields['entity_type'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Submitted to: Entity type'))
    ->setDescription(t('The entity type to which this submission was submitted from.'))
    ->setSetting('is_ascii', TRUE)
    ->setSetting('max_length', EntityTypeInterface::ID_MAX_LENGTH);

  // Can't use entity reference without a target type because it defaults to
  // an integer which limits reference to only content entities (and not
  // config entities like Views, Panels, etc…).
  // @see \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem::propertyDefinitions()
  $fields['entity_id'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Submitted to: Entity ID'))
    ->setDescription(t('The ID of the entity of which this webform submission was submitted from.'))
    ->setSetting('max_length', 255);
  $fields['locked'] = BaseFieldDefinition::create('boolean')
    ->setLabel(t('Locked'))
    ->setDescription(t('A flag that indicates a locked webform submission.'))
    ->setDefaultValue(FALSE);
  $fields['sticky'] = BaseFieldDefinition::create('boolean')
    ->setLabel(t('Sticky'))
    ->setDescription(t('A flag that indicate the status of the webform submission.'))
    ->setDefaultValue(FALSE);
  $fields['notes'] = BaseFieldDefinition::create('string_long')
    ->setLabel(t('Notes'))
    ->setDescription(t('Administrative notes about the webform submission.'))
    ->setDefaultValue('');
  return $fields;
}