Reputation: 41
I was under the impression that by removing fields from public static function baseFieldDefinitions
that they would not be required anymore during trying to save the entity. My function now only contains the following:
$fields['id'] = BaseFieldDefinition::create('integer')
->setLabel(t('ID'))
->setDescription(t('The ID of the Content entity.'))
->setReadOnly(TRUE);
$fields['name'] = BaseFieldDefinition::create('string')
->setLabel(t('Name'))
->setDescription(t('The name of the Content entity.'))
->setSettings(array(
'max_length' => 50,
'text_processing' => 0,
))
->setDefaultValue('')
->setDisplayOptions('view', array(
'label' => 'above',
'type' => 'string',
'weight' => -4,
))
->setDisplayOptions('form', array(
'type' => 'string_textfield',
'weight' => -4,
))
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
However, when I try to add my entity it returns the following exception: Uncaught PHP Exception InvalidArgumentException: "Field changed is unknown." at /var/www/html/core/lib/Drupal/Core/Entity/ContentEntityBase.php line 474
This is my first attempt at making a content entity so may be being done wrong.
Upvotes: 1
Views: 304
Reputation: 11
I know that question is too old, but that helped for me:
modify to:
class YourClass extends ContentEntityBase implements InvitationInterface {
use EntityChangedTrait;
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['id'] = BaseFieldDefinition::create('integer')
->setLabel(t('ID'))
->setDescription(t('The ID of the Content entity.'))
->setReadOnly(TRUE);
$fields['name'] = BaseFieldDefinition::create('string')
->setLabel(t('Name'))
->setDescription(t('The name of the Content entity.'))
->setSettings(array(
'max_length' => 50,
'text_processing' => 0,
))
->setDefaultValue('')
->setDisplayOptions('view', array(
'label' => 'above',
'type' => 'string',
'weight' => -4,
))
->setDisplayOptions('form', array(
'type' => 'string_textfield',
'weight' => -4,
))
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time that the entity was last edited.'));
}
}
Upvotes: 0