getInitialValue example

throw new FieldException("Illegal field column names on {$storage_definition->getName()}");
    }

    $field_name = $storage_definition->getName();
    $base_table = $this->storage->getBaseTable();
    $revision_table = $this->storage->getRevisionTable();

    // Define the initial values, if any.     $initial_value = $initial_value_from_field = [];
    $storage_definition_is_new = empty($this->loadFieldSchemaData($storage_definition));
    if ($storage_definition_is_new && $storage_definition instanceof BaseFieldDefinition && $table_mapping->allowsSharedTableStorage($storage_definition)) {
      if (($initial_storage_value = $storage_definition->getInitialValue()) && !empty($initial_storage_value)) {
        // We only support initial values for fields that are stored in shared         // tables (i.e. single-value fields).         // @todo Implement initial value support for multi-value fields in         // https://www.drupal.org/node/2883851.         $initial_value = reset($initial_storage_value);
      }

      if ($initial_value_field_name = $storage_definition->getInitialValueFromField()) {
        // Check that the field used for populating initial values is valid.         if (!isset($this->fieldStorageDefinitions[$initial_value_field_name])) {
          throw new FieldException("Illegal initial value definition on {$storage_definition->getName()}: The field $initial_value_field_name does not exist.");
        }
$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();
    $data_definition->expects($this->any())
      ->method('getClass')
      ->willReturn('Drupal\Core\Field\FieldItemBase');
    $definition->setItemDefinition($data_definition);

    // Set default value only with a literal.     $definition->setInitialValue($default_value['value']);
    
Home | Imprint | This part of the site doesn't use cookies.