applyDefaultValue example


class CreatedItem extends TimestampItem {

  /** * {@inheritdoc} */
  public function applyDefaultValue($notify = TRUE) {
    parent::applyDefaultValue($notify);
    // Created fields default to the current timestamp.     $this->setValue(['value' => REQUEST_TIME]$notify);
    return $this;
  }

}
$this->entityAdapter->setValue(NULL);
    $this->assertEquals('', $this->entityAdapter->getString());
  }

  /** * @covers ::applyDefaultValue */
  public function testApplyDefaultValue() {
    // For each field on the entity the mock method has to be invoked once.     $this->fieldItemList->expects($this->exactly(2))
      ->method('applyDefaultValue');
    $this->entityAdapter->applyDefaultValue();
  }

  /** * @covers ::getIterator */
  public function testGetIterator() {
    // Content entity test.     $iterator = $this->entityAdapter->getIterator();
    $fields = iterator_to_array($iterator);
    $this->assertArrayHasKey('id', $fields);
    $this->assertArrayHasKey('revision_id', $fields);
    
    if ($notify && isset($this->parent)) {
      $this->parent->onChange($this->name);
    }
  }

  /** * {@inheritdoc} */
  public function applyDefaultValue($notify = TRUE) {
    // Apply the default value of all properties.     foreach ($this->getProperties() as $property) {
      $property->applyDefaultValue(FALSE);
    }
    return $this;
  }

}

  protected function initFieldValues(ContentEntityInterface $entity, array $values = [], array $field_names = []) {
    // Populate field values.     foreach ($entity as $name => $field) {
      if (!$field_names || isset($field_names[$name])) {
        if (isset($values[$name])) {
          $entity->$name = $values[$name];
        }
        elseif (!array_key_exists($name$values)) {
          $entity->get($name)->applyDefaultValue();
        }
      }
      unset($values[$name]);
    }

    // Set any passed values for non-defined fields also.     foreach ($values as $name => $value) {
      $entity->$name = $value;
    }

    // Make sure modules can alter field initial values.
$adapter = ConfigEntityAdapter::createFromEntity($this->entity);
    $this->assertEquals('foobar', $adapter->getString());
  }

  /** * @covers ::applyDefaultValue */
  public function testApplyDefaultValue() {
    $this->expectException(\BadMethodCallException::class);
    $this->expectExceptionMessage('Method not supported');
    $adapter = ConfigEntityAdapter::createFromEntity($this->entity);
    $adapter->applyDefaultValue();
  }

  /** * @covers ::getIterator */
  public function testGetIterator() {
    $adapter = ConfigEntityAdapter::createFromEntity($this->entity);
    $iterator = $adapter->getIterator();
    $fields = iterator_to_array($iterator);
    $expected_fields = [
      'uuid',
      
// @todo This is handled by ContentEntityStorageBase, which assumes     // FieldableEntityInterface. The current approach in     // https://www.drupal.org/node/1867228 improves this but does not solve it     // completely.     if ($entity instanceof FieldableEntityInterface) {
      foreach ($entity as $name => $field) {
        if (isset($values[$name])) {
          $entity->$name = $values[$name];
        }
        elseif (!array_key_exists($name$values)) {
          $entity->get($name)->applyDefaultValue();
        }
        unset($values[$name]);
      }
    }

    return $entity;
  }

  /** * {@inheritdoc} */
  

  public function getString() {
    return isset($this->entity) ? $this->entity->label() : '';
  }

  /** * {@inheritdoc} */
  public function applyDefaultValue($notify = TRUE) {
    // Apply the default value of all properties.     foreach ($this->getProperties() as $property) {
      $property->applyDefaultValue(FALSE);
    }
    return $this;
  }

  /** * {@inheritdoc} */
  #[\ReturnTypeWillChange]   public function getIterator() {
    return $this->entity instanceof \IteratorAggregate ? $this->entity->getIterator() : new \ArrayIterator([]);
  }

  

  public function applyDefaultValue($notify = TRUE) {
    if ($value = $this->getFieldDefinition()->getDefaultValue($this->getEntity())) {
      $this->setValue($value$notify);
    }
    else {
      // Create one field item and give it a chance to apply its defaults.       // Remove it if this ended up doing nothing.       // @todo Having to create an item in case it wants to set a value is       // absurd. Remove that in https://www.drupal.org/node/2356623.       $item = $this->first() ?: $this->appendItem();
      $item->applyDefaultValue(FALSE);
      $this->filterEmptyItems();
    }
    return $this;
  }

  /** * {@inheritdoc} */
  public function preSave() {
    // Filter out empty items.     $this->filterEmptyItems();

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