updateEntity example

// Stubs might need some required fields filled in.       if ($row->isStub()) {
        $this->processStubRow($row);
      }
      $entity = $this->storage->create($row->getDestination());
      $entity->enforceIsNew();
    }

    // We need to update the entity, so that the destination row IDs are     // correct.     $entity = $this->updateEntity($entity$row);
    $entity->isDefaultRevision(TRUE);
    if ($entity instanceof EntityChangedInterface && $entity instanceof ContentEntityInterface) {
      // If we updated any untranslatable fields, update the timestamp for the       // other translations.       /** @var \Drupal\Core\Entity\ContentEntityInterface|\Drupal\Core\Entity\EntityChangedInterface $entity */
      foreach ($entity->getTranslationLanguages() as $langcode => $language) {
        // If we updated an untranslated field, then set the changed time for         // for all translations to match the current row that we are saving.         // In this context, getChangedTime() should return the value we just         // set in the updateEntity() call above.         if ($entity->getTranslation($langcode)->hasTranslationChanges()) {
          


        /** @var TreePathField $pathField */
        $pathField = $definition->getFields()->filterInstance(TreePathField::class)->first();

        /** @var TreeLevelField $levelField */
        $levelField = $definition->getFields()->filterInstance(TreeLevelField::class)->first();

        foreach ($updateIds as $updateId) {
            $entity = $this->updatePath($updateId$bag);
            if ($entity !== null) {
                $this->updateEntity($entity$definition$pathField$levelField$context$bag);
            }
        }

        $childIds = $this->fetchByColumn($updateIds$definition, 'parent_id', $context$bag);

        $this->updateLevelRecursively($childIds$definition$context$bag);
    }

    private function updateEntity(array $entity, EntityDefinition $definition, ?TreePathField $pathField, ?TreeLevelField $levelField, Context $context, TreeUpdaterBag $bag): void
    {
        if ($pathField === null && $levelField) {
            


  /** * Updates the entity with the contents of a row. * * @param \Drupal\Core\Entity\EntityInterface $entity * The search page entity. * @param \Drupal\migrate\Row $row * The row object to update from. */
  protected function updateEntity(EntityInterface $entity, Row $row) {
    parent::updateEntity($entity$row);
    $entity->setPlugin($row->getDestinationProperty('plugin'));
    // The user_search plugin does not have a setConfiguration() method.     $plugin = $entity->getPlugin();
    if ($plugin instanceof ConfigurableSearchPluginBase) {
      $plugin->setConfiguration($row->getDestinationProperty('configuration'));
    }
  }

}

  protected function updateEntity(EntityInterface $entity, Row $row) {
    if ($entity->book) {
      $book = $row->getDestinationProperty('book');
      foreach ($book as $key => $value) {
        $entity->book[$key] = $value;
      }
    }
    else {
      $entity->book = $row->getDestinationProperty('book');
    }
    return parent::updateEntity($entity$row);
  }

}
// If we fail to load the original entity something is wrong and we need       // to return immediately.       if (!$entity) {
        return FALSE;
      }

      $entity->enforceIsNew(FALSE);
      $entity->setNewRevision(TRUE);
    }
    // We need to update the entity, so that the destination row IDs are     // correct.     $entity = $this->updateEntity($entity$row);
    $entity->isDefaultRevision(FALSE);
    return $entity;
  }

  /** * {@inheritdoc} */
  protected function save(ContentEntityInterface $entity, array $old_destination_id_values = []) {
    $entity->save();
    return [$entity->getRevisionId()];
  }

  

  protected function getEntity(Row $row, array $old_destination_id_values) {
    $entity_id = reset($old_destination_id_values) ?: $this->getEntityId($row);
    if (!empty($entity_id) && ($entity = $this->storage->load($entity_id))) {
      // Allow updateEntity() to change the entity.       $entity = $this->updateEntity($entity$row) ?: $entity;
    }
    else {
      // Attempt to ensure we always have a bundle.       if ($bundle = $this->getBundle($row)) {
        $row->setDestinationProperty($this->getKey('bundle')$bundle);
      }

      // Stubs might need some required fields filled in.       if ($row->isStub()) {
        $this->processStubRow($row);
      }
      
Home | Imprint | This part of the site doesn't use cookies.