/** * Responds to entity PATCH requests. * * @Thruway(name = "update", type="procedure") * * @param \Drupal\Core\Entity\EntityInterface $original_entity * The original entity object. * * @return array * */ public function patch(EntityInterface $original_entity) { $entity = entity_load($original_entity->getEntityTypeId(), $original_entity->id()); if (!$entity) { throw new BadRequestHttpException(t('Invalid entity')); } $definition = $this->getPluginDefinition(); if ($entity->getEntityTypeId() != $definition['entity_type']) { throw new BadRequestHttpException(t('Invalid entity type')); } if (!$entity->access('update')) { throw new AccessDeniedHttpException(); } // Overwrite the received properties. foreach ($original_entity as $field_name => $field) { if (isset($entity->{$field_name})) { // It is not possible to set the language to NULL as it is automatically // re-initialized. As it must not be empty, skip it if it is. // @todo: Use the langcode entity key when available. See // https://drupal.org/node/2143729. if ($field_name == 'langcode' && $field->isEmpty()) { continue; } // if ($field->isEmpty() && !$original_entity->get($field_name)->access('delete')) { // throw new AccessDeniedHttpException( // t('Access denied on deleting field @field.', array('@field' => $field_name)) // ); // } $entity->set($field_name, $original_entity->getValue($field_name)[$field_name]); if (!$entity->get($field_name)->access('update')) { throw new AccessDeniedHttpException(t('Access denied on updating field @field.', array('@field' => $field_name))); } } } // Validate the received data before saving. $this->validate($original_entity); try { $entity->save(); // $this->logger->notice( // 'Updated entity %type with ID %id.', // array('%type' => $entity->getEntityTypeId(), '%id' => $entity->id()) // ); // Update responses have an empty body. return $entity; } catch (EntityStorageException $e) { throw new HttpException(500, t('Internal Server Error'), $e); } }
/** * Creates a log message when an exception occured during import. * * @param \Exception $e * The exception that was thrown during processing. * @param \Drupal\Core\Entity\EntityInterface $entity * The entity object that was being processed. * @param arary $item * The parser result for this entity. * * @return string * The message to log. * * @todo This no longer works due to circular references. */ protected function createLogMessage(\Exception $e, EntityInterface $entity, array $item) { include_once DRUPAL_ROOT . '/core/includes/utility.inc'; $message = $e->getMessage(); $message .= '<h3>Original item</h3>'; $message .= '<pre>' . drupal_var_export($item) . '</pre>'; $message .= '<h3>Entity</h3>'; $message .= '<pre>' . drupal_var_export($entity->getValue()) . '</pre>'; return $message; }