1

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.

1 Answer 1

0

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.'));
        
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.