1
votes

I have Drupal 8.9.8 . I created a custom entity 'corso' with the base field name varchar (50)

Now i want that name is a varchar(255) so i created a hook to update in mymodule.install

function mymodule_update_80485() {
        $database = \Drupal::database();
        $database->query("ALTER TABLE corso_field_data MODIFY name VARCHAR(300)");
        $database->query("ALTER TABLE corso_field_revision MODIFY name VARCHAR(300)");

$storage_key = 'ente.field_schema_data.name';
    $storage_schema = \Drupal::keyValue('entity.storage_schema.sql');
    $field_schema = $storage_schema->get($storage_key);
    $field_schema['ente_field_data']['fields']['name']['length'] = 300;
    $field_schema['ente_field_revision']['fields']['name']['length'] = 300;
    $storage_schema->set($storage_key, $field_schema);
    }

I update entity definition

$fields['name'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Name'))
      ->setDescription(t('The name of the Corso entity.'))
      ->setRevisionable(TRUE)
      ->setSettings([
        'max_length' => 300,
        'text_processing' => 0,
      ])
      ->setDefaultValue('')
      ->setDisplayOptions('view', [
        'label' => 'above',
        'type' => 'string',
        'weight' => -4,
      ])
      ->setDisplayOptions('form', [
        'type' => 'string_textfield',
        'weight' => -4,
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE)
      ->setRequired(TRUE);

The database columns are updated but I still get 'Mismatched entity and/or field definitions' in drupal

What am I missing?

1

1 Answers

0
votes

I found this solution, it seems to work!

function mymodule_update_80486() {
$definition = \Drupal\Core\Field\BaseFieldDefinition::create('string')
     ->setLabel(t('Name'))
     ->setDescription(t('The name of the Corso entity.'))
     ->setRevisionable(TRUE)
     ->setSettings([
       'max_length' => 255,
       'text_processing' => 0,
     ])
     ->setDefaultValue('')
     ->setDisplayOptions('view', [
       'label' => 'above',
       'type' => 'string',
       'weight' => -4,
     ])
     ->setDisplayOptions('form', [
       'type' => 'string_textfield',
       'weight' => -4,
     ])
     ->setDisplayConfigurable('form', TRUE)
     ->setDisplayConfigurable('view', TRUE)
     ->setRequired(TRUE);


  \Drupal::entityDefinitionUpdateManager()
    ->installFieldStorageDefinition('name', 'corso', 'corso', $definition);

}