【问题标题】:Drupal 8.9.8 update custom entity name lengthDrupal 8.9.8 更新自定义实体名称长度
【发布时间】:2020-11-16 17:15:57
【问题描述】:

我有 Drupal 8.9.8 。 我创建了一个自定义实体“corso”,其基本字段名称为varchar (50)

现在我希望该名称是 varchar(255),所以我创建了一个挂钩以在 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);
    }

我更新实体定义

$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);

数据库列已更新,但我仍然在 drupal 中收到“不匹配的实体和/或字段定义

我错过了什么?

【问题讨论】:

    标签: database drupal entity drupal-8


    【解决方案1】:

    我找到了这个解决方案,它似乎有效!

    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);
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-24
      • 2016-03-04
      • 1970-01-01
      相关资源
      最近更新 更多