예제 #1
0
 /**
  * {@inheritdoc}
  */
 public function save(EntityInterface $entity)
 {
     // We return SAVED_UPDATED by default because the logic below might not
     // update the entity if its values haven't changed, so returning FALSE
     // would be confusing in that situation.
     $return = SAVED_UPDATED;
     $transaction = $this->database->startTransaction();
     try {
         // Load the stored entity, if any.
         if (!$entity->isNew() && !isset($entity->original)) {
             $id = $entity->id();
             if ($entity->getOriginalId() !== NULL) {
                 $id = $entity->getOriginalId();
             }
             $entity->original = $this->loadUnchanged($id);
         }
         if ($entity->isNew()) {
             $entity->mlid = $this->database->insert($this->entityType->getBaseTable())->fields(array('menu_name' => $entity->menu_name))->execute();
             $entity->enforceIsNew();
         }
         // Unlike the save() method from EntityDatabaseStorage, we invoke the
         // 'presave' hook first because we want to allow modules to alter the
         // entity before all the logic from our preSave() method.
         $this->invokeHook('presave', $entity);
         $entity->preSave($this);
         // If every value in $entity->original is the same in the $entity, there
         // is no reason to run the update queries or clear the caches. We use
         // array_intersect_key() with the $entity as the first parameter because
         // $entity may have additional keys left over from building a router entry.
         // The intersect removes the extra keys, allowing a meaningful comparison.
         if ($entity->isNew() || array_intersect_key(get_object_vars($entity), get_object_vars($entity->original)) != get_object_vars($entity->original)) {
             $return = drupal_write_record($this->entityType->getBaseTable(), $entity, $this->idKey);
             if ($return) {
                 if (!$entity->isNew()) {
                     $this->resetCache(array($entity->{$this->idKey}));
                     $entity->postSave($this, TRUE);
                     $this->invokeHook('update', $entity);
                 } else {
                     $return = SAVED_NEW;
                     $this->resetCache();
                     $entity->enforceIsNew(FALSE);
                     $entity->postSave($this, FALSE);
                     $this->invokeHook('insert', $entity);
                 }
             }
         }
         // Ignore replica server temporarily.
         db_ignore_replica();
         unset($entity->original);
         return $return;
     } catch (\Exception $e) {
         $transaction->rollback();
         watchdog_exception($this->entityTypeId, $e);
         throw new EntityStorageException($e->getMessage(), $e->getCode(), $e);
     }
 }
예제 #2
0
 /**
  * {@inheritdoc}
  */
 public function save(EntityInterface $entity)
 {
     // The anonymous user account is saved with the fixed user ID of 0.
     // Therefore we need to check for NULL explicitly.
     if ($entity->id() === NULL) {
         $entity->uid->value = $this->database->nextId($this->database->query('SELECT MAX(uid) FROM {users}')->fetchField());
         $entity->enforceIsNew();
     }
     parent::save($entity);
 }
예제 #3
0
 /**
  * Performs post save entity processing.
  *
  * @param \Drupal\Core\Entity\EntityInterface $entity
  *   The saved entity.
  * @param bool $update
  *   Specifies whether the entity is being updated or created.
  */
 protected function doPostSave(EntityInterface $entity, $update)
 {
     $this->resetCache(array($entity->id()));
     // The entity is no longer new.
     $entity->enforceIsNew(FALSE);
     // Allow code to run after saving.
     $entity->postSave($this, $update);
     $this->invokeHook($update ? 'update' : 'insert', $entity);
     // After saving, this is now the "original entity", and subsequent saves
     // will be updates instead of inserts, and updates must always be able to
     // correctly identify the original entity.
     $entity->setOriginalId($entity->id());
     unset($entity->original);
 }
 /**
  * {@inheritdoc}
  */
 protected function doSave($id, EntityInterface $entity)
 {
     /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
     if ($entity->isNew()) {
         // Ensure the entity is still seen as new after assigning it an id, while
         // storing its data.
         $entity->enforceIsNew();
         if ($this->entityType->isRevisionable()) {
             $entity->setNewRevision();
         }
         $return = SAVED_NEW;
     } else {
         // @todo Consider returning a different value when saving a non-default
         //   entity revision. See https://www.drupal.org/node/2509360.
         $return = $entity->isDefaultRevision() ? SAVED_UPDATED : FALSE;
     }
     $this->populateAffectedRevisionTranslations($entity);
     $this->doSaveFieldItems($entity);
     return $return;
 }
예제 #5
0
 /**
  * Responds to entity DELETE requests.
  *
  * @Thruway(name = "remove", type="procedure")
  *
  * @param \Drupal\Core\Entity\EntityInterface $entity
  *   The entity object.
  *
  * @return array
  *
  * @throws \Symfony\Component\HttpKernel\Exception\HttpException
  */
 public function delete(EntityInterface $entity)
 {
     if (!$entity->access('delete')) {
         throw new AccessDeniedHttpException();
     }
     try {
         $entity->enforceIsNew(FALSE);
         $entity->delete();
         //            $this->logger->notice(
         //                'Deleted entity %type with ID %id.',
         //                array('%type' => $entity->getEntityTypeId(), '%id' => $entity->id())
         //            );
         // Delete responses have an empty body.
         return $entity;
     } catch (EntityStorageException $e) {
         throw new HttpException(500, t('Internal Server Error'), $e);
     }
 }
 /**
  * @return WorkflowTransitionInterface
  */
 protected function getTransitionForExecution(EntityInterface $entity)
 {
     $user = workflow_current_user();
     if (!$entity) {
         \Drupal::logger('workflow_action')->notice('Unable to get current entity - entity is not defined.', []);
         return NULL;
     }
     // Get the entity type and numeric ID.
     $entity_id = $entity->id();
     if (!$entity_id) {
         \Drupal::logger('workflow_action')->notice('Unable to get current entity ID - entity is not yet saved.', []);
         return NULL;
     }
     // In 'after saving new content', the node is already saved. Avoid second insert.
     // Todo: clone?
     $entity->enforceIsNew(FALSE);
     $config = $this->configuration;
     $field_name = workflow_get_field_name($entity, $config['field_name']);
     $current_sid = workflow_node_current_state($entity, $field_name);
     if (!$current_sid) {
         \Drupal::logger('workflow_action')->notice('Unable to get current workflow state of entity %id.', array('%id' => $entity_id));
         return NULL;
     }
     $to_sid = isset($config['to_sid']) ? $config['to_sid'] : '';
     // Get the Comment. Parse the $comment variables.
     $comment_string = $this->configuration['comment'];
     $comment = t($comment_string, array('%title' => $entity->label(), '%state' => workflow_get_sid_name($to_sid), '%user' => $user->getUsername()));
     $force = $this->configuration['force'];
     $transition = WorkflowTransition::create([$current_sid, 'field_name' => $field_name]);
     $transition->setTargetEntity($entity);
     $transition->setValues($to_sid, $user->id(), REQUEST_TIME, $comment);
     $transition->force($force);
     return $transition;
 }