Skip to content

Instantly share code, notes, and snippets.

@pedrorocha-net
Created August 16, 2015 20:26
Show Gist options
  • Save pedrorocha-net/70d6a84c3ddba1c5939d to your computer and use it in GitHub Desktop.
Save pedrorocha-net/70d6a84c3ddba1c5939d to your computer and use it in GitHub Desktop.
Approach extending ContentEntityBase and using BaseFieldDefinition::create
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['id'] = BaseFieldDefinition::create('integer')
->setLabel(t('ID'))
->setDescription(t('The ID of the Voting API Vote entity.'))
->setReadOnly(TRUE);
$fields['uuid'] = BaseFieldDefinition::create('uuid')
->setLabel(t('UUID'))
->setDescription(t('The UUID of the Voting API Vote entity.'))
->setReadOnly(TRUE);
$fields['target_entity_type'] = BaseFieldDefinition::create('string')
->setLabel(t('Entity Type'))
->setSettings(array(
'max_length' => 64
))
->setDefaultValue('node');
$fields['target_entity_id'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Voted entity'))
->setRevisionable(FALSE)
->setSetting('handler', 'default')
->setDefaultValue(0);
$fields['value'] = BaseFieldDefinition::create('float')
->setLabel(t('Value'))
->setDefaultValue(0);
$fields['value_type'] = BaseFieldDefinition::create('string')
->setLabel(t('Value Type'))
->setSettings(array(
'max_length' => 64
))
->setDefaultValue('percent');
$fields['tag'] = BaseFieldDefinition::create('string')
->setLabel(t('Tag'))
->setSettings(array(
'max_length' => 64,
))
->setDefaultValue('vote');
$fields['user_id'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Authored by'))
->setDescription(t('The user ID of author of the Voting API Vote entity.'))
->setRevisionable(FALSE)
->setSetting('target_type', 'user')
->setSetting('handler', 'default')
->setTranslatable(FALSE)
->setDefaultValue(0);
$fields['timestamp'] = BaseFieldDefinition::create('created')
->setLabel(t('Created'))
->setDescription(t('The time that the entity was created.'))
->setDefaultValue(0);
$fields['vote_source'] = BaseFieldDefinition::create('string')
->setLabel(t('Value Type'))
->setSettings(array(
'max_length' => 255
));
return $fields;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment