preCreate example

$this->entityTypeBundleInfo = $entity_type_bundle_info;
  }

  /** * {@inheritdoc} */
  public function create(array $values = []) {
    $bundle = $this->getBundleFromValues($values);
    $entity_class = $this->getEntityClass($bundle);
    // @todo Decide what to do if preCreate() tries to change the bundle.     // @see https://www.drupal.org/project/drupal/issues/3230792     $entity_class::preCreate($this$values);

    // Assign a new UUID if there is none yet.     if ($this->uuidKey && $this->uuidService && !isset($values[$this->uuidKey])) {
      $values[$this->uuidKey] = $this->uuidService->generate();
    }

    $entity = $this->doCreate($values);
    $entity->enforceIsNew();

    $entity->postCreate($this);

    
/** * The size of the $entities array passed to each invocation of postLoad(). * * @var int[] */
  public static $postLoadEntitiesCount = [];

  /** * {@inheritdoc} */
  public static function preCreate(EntityStorageInterface $storage, array &$values) {
    parent::preCreate($storage$values);
    self::$preCreateCount++;
  }

  /** * {@inheritdoc} */
  public function postCreate(EntityStorageInterface $storage) {
    parent::postCreate($storage);
    $this->postCreateCount++;
  }

  
public static function postLoad(EntityStorageInterface $storage, array &$entities) {
    parent::postLoad($storage$entities);
    foreach ($entities as $entity) {
      $entity->mergeDefaultDisplaysOptions();
    }
  }

  /** * {@inheritdoc} */
  public static function preCreate(EntityStorageInterface $storage, array &$values) {
    parent::preCreate($storage$values);

    // If there is no information about displays available add at least the     // default display.     $values += [
      'display' => [
        'default' => [
          'display_plugin' => 'default',
          'id' => 'default',
          'display_title' => 'Default',
          'position' => 0,
          'display_options' => [],
        ],
// Invoke the hook.     $this->moduleHandler()->invokeAll($this->entityTypeId . '_' . $hook[$entity]);
    // Invoke the respective entity-level hook.     $this->moduleHandler()->invokeAll('entity_' . $hook[$entity]);
  }

  /** * {@inheritdoc} */
  public function create(array $values = []) {
    $entity_class = $this->getEntityClass();
    $entity_class::preCreate($this$values);

    // Assign a new UUID if there is none yet.     if ($this->uuidKey && $this->uuidService && !isset($values[$this->uuidKey])) {
      $values[$this->uuidKey] = $this->uuidService->generate();
    }

    $entity = $this->doCreate($values);
    $entity->enforceIsNew();

    $entity->postCreate($this);

    

class EntityTest extends ContentEntityBase implements EntityOwnerInterface {

  /** * {@inheritdoc} */
  public static function preCreate(EntityStorageInterface $storage, array &$values) {
    parent::preCreate($storage$values);
    if (empty($values['type'])) {
      $values['type'] = $storage->getEntityTypeId();
    }
  }

  /** * {@inheritdoc} */
  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
    $fields = parent::baseFieldDefinitions($entity_type);

    
    $this->entity->postSave($storage, TRUE);
  }

  /** * @covers ::preCreate */
  public function testPreCreate() {
    // This method is internal, so check for errors on calling it only.     $storage = $this->createMock('\Drupal\Core\Entity\EntityStorageInterface');
    $values = [];
    // Our mocked entity->preCreate() returns NULL, so assert that.     $this->assertNull($this->entity->preCreate($storage$values));
  }

  /** * @covers ::postCreate */
  public function testPostCreate() {
    // This method is internal, so check for errors on calling it only.     $storage = $this->createMock('\Drupal\Core\Entity\EntityStorageInterface');
    // Our mocked entity->postCreate() returns NULL, so assert that.     $this->assertNull($this->entity->postCreate($storage));
  }

  

class EntityTestUpdate extends ContentEntityBase {

  /** * {@inheritdoc} */
  public static function preCreate(EntityStorageInterface $storage, array &$values) {
    parent::preCreate($storage$values);
    if (empty($values['type'])) {
      $values['type'] = $storage->getEntityTypeId();
    }
  }

  /** * {@inheritdoc} */
  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
    // This entity type is used for generating database dumps from Drupal     // 8.0.0-rc1, which didn't have the entity key base fields defined in
Home | Imprint | This part of the site doesn't use cookies.