addBundleField example

$changes = $this->entityDefinitionUpdateManager->getChangeSummary();
    $this->assertCount(1, $changes['entity_test_update']);
    $this->assertEquals('The new_base_field field needs to be uninstalled.', strip_tags($changes['entity_test_update'][0]));
  }

  /** * Tests creating, updating, and deleting a bundle field if no entities exist. */
  public function testBundleFieldCreateUpdateDeleteWithoutData() {
    // Add a bundle field, ensure the update manager reports it, and the update     // creates its schema.     $this->addBundleField();
    $this->assertTrue($this->entityDefinitionUpdateManager->needsUpdates(), 'EntityDefinitionUpdateManager reports that updates are needed.');
    $changes = $this->entityDefinitionUpdateManager->getChangeSummary();
    $this->assertCount(1, $changes['entity_test_update']);
    $this->assertEquals('The A new bundle field field needs to be installed.', strip_tags($changes['entity_test_update'][0]));
    $this->applyEntityUpdates();
    $this->assertTrue($this->database->schema()->tableExists('entity_test_update__new_bundle_field'), 'Dedicated table created for new_bundle_field.');

    // Update the type of the base field from 'string' to 'text', ensure the     // update manager reports it, and the update adjusts the schema     // accordingly.     $this->modifyBundleField();
    
->setTargetEntityTypeId('entity_test_update')
      ->setRevisionable($revisionable)
      ->setTranslatable($translatable);
    $this->state->set('entity_test_update.additional_field_storage_definitions', $definitions);
    $this->state->set('entity_test_update.additional_bundle_field_definitions.test_bundle', $definitions);
  }

  /** * Modifies the new bundle field from 'string' to 'text'. */
  protected function modifyBundleField() {
    $this->addBundleField('text');
  }

  /** * Removes the new bundle field from the 'entity_test_update' entity type. */
  protected function removeBundleField() {
    $this->state->delete('entity_test_update.additional_field_storage_definitions');
    $this->state->delete('entity_test_update.additional_bundle_field_definitions.test_bundle');
  }

  /** * Adds an index to the 'entity_test_update' entity type's base table. * * @see \Drupal\entity_test\EntityTestStorageSchema::getEntitySchema() */
protected function setUp(): void {
    parent::setUp();
    $this->entityDefinitionUpdateManager = $this->container->get('entity.definition_update_manager');
    $this->lastInstalledSchemaRepository = $this->container->get('entity.last_installed_schema.repository');
    $this->installedStorageSchema = $this->container->get('keyvalue')->get('entity.storage_schema.sql');
    $this->entityTypeManager = $this->container->get('entity_type.manager');
    $this->entityFieldManager = $this->container->get('entity_field.manager');
    $this->database = $this->container->get('database');

    // Add a non-revisionable bundle field to test revision field table     // handling.     $this->addBundleField('string', FALSE, TRUE);

    // The 'changed' field type has a special behavior because it updates itself     // automatically if any of the other field values of an entity have been     // updated, so add it to the entity type that is being tested in order to     // provide test coverage for this special case.     $fields['changed'] = BaseFieldDefinition::create('changed')
      ->setLabel(t('Changed'))
      ->setDescription(t('The time that the content block was last edited.'))
      ->setTranslatable(TRUE)
      ->setRevisionable(TRUE);
    $this->state->set('entity_test_update.additional_base_field_definitions', $fields);

    
Home | Imprint | This part of the site doesn't use cookies.