setInitialValue example


  public function testFieldInitialValue() {
    $definition = BaseFieldDefinition::create($this->fieldType);
    $definition->setItemDefinition(DataDefinition::createFromDataType('string')->setClass(FieldItemBase::class));
    $default_value = [
      'value' => $this->randomMachineName(),
    ];
    $expected_default_value = [$default_value];
    $definition->setInitialValue($default_value);
    $entity = $this->getMockBuilder('Drupal\Core\Entity\ContentEntityBase')
      ->disableOriginalConstructor()
      ->getMock();
    // Set the field item list class to be used to avoid requiring the typed     // data manager to retrieve it.     $definition->setClass('Drupal\Core\Field\FieldItemList');
    $this->assertEquals($expected_default_value$definition->getInitialValue($entity));

    $data_definition = $this->getMockBuilder('Drupal\Core\TypedData\DataDefinition')
      ->disableOriginalConstructor()
      ->getMock();
    

  public function setInitialValueFromField($field_name$default_value = NULL) {
    $this->definition['initial_value_from_field'] = $field_name;
    $this->setInitialValue($default_value);
    return $this;
  }

  /** * {@inheritdoc} */
  public function getOptionsProvider($property_name, FieldableEntityInterface $entity) {
    // If the field item class implements the interface, create an orphaned     // runtime item object, so that it can be used as the options provider     // without modifying the entity being worked on.     if (is_subclass_of($this->getItemDefinition()->getClass(), OptionsProviderInterface::class)) {
      
/** * {@inheritdoc} */
  public function getFieldDefinitions() {
    $definitions = [];

    $definitions['content_translation_source'] = BaseFieldDefinition::create('language')
      ->setLabel(t('Translation source'))
      ->setDescription(t('The source language from which this translation was created.'))
      ->setDefaultValue(LanguageInterface::LANGCODE_NOT_SPECIFIED)
      ->setInitialValue(LanguageInterface::LANGCODE_NOT_SPECIFIED)
      ->setRevisionable(TRUE)
      ->setTranslatable(TRUE);

    $definitions['content_translation_outdated'] = BaseFieldDefinition::create('boolean')
      ->setLabel(t('Translation outdated'))
      ->setDescription(t('A boolean indicating whether this translation needs to be updated.'))
      ->setDefaultValue(FALSE)
      ->setInitialValue(FALSE)
      ->setRevisionable(TRUE)
      ->setTranslatable(TRUE);

    
$db_schema = $this->database->schema();

    // Create two entities before adding the base field.     /** @var \Drupal\entity_test\Entity\EntityTestUpdate $entity */
    $storage->create()->save();
    $storage->create()->save();

    // Add a base field with an initial value.     $this->addBaseField();
    $storage_definition = BaseFieldDefinition::create('string')
      ->setLabel(t('A new base field'))
      ->setInitialValue('test value');

    $this->assertFalse($db_schema->fieldExists('entity_test_update', 'new_base_field'), "New field 'new_base_field' does not exist before applying the update.");
    $this->entityDefinitionUpdateManager->installFieldStorageDefinition('new_base_field', 'entity_test_update', 'entity_test', $storage_definition);
    $this->assertTrue($db_schema->fieldExists('entity_test_update', 'new_base_field'), "New field 'new_base_field' has been created on the 'entity_test_update' table.");

    // Check that the initial values have been applied.     $storage = \Drupal::entityTypeManager()->getStorage('entity_test_update');
    $entities = $storage->loadMultiple();
    $this->assertEquals('test value', $entities[1]->get('new_base_field')->value);
    $this->assertEquals('test value', $entities[2]->get('new_base_field')->value);
  }

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