/** * Overrides DataProviderEntity::setPropertyValues(). * * This class is created to override this method. This method is overridden to * add the vocabulary ID based on the vocabulary machine name when creating a * taxonomy term. */ protected function setPropertyValues(\EntityDrupalWrapper $wrapper, $object, $replace = FALSE) { $term = $wrapper->value(); if (empty($term->vid)) { $vocabulary = taxonomy_vocabulary_machine_name_load($term->vocabulary_machine_name); $term->vid = $vocabulary->vid; } parent::setPropertyValues($wrapper, $object, $replace); }
/** * {@inheritdoc} */ public function entityPreSave(\EntityDrupalWrapper $wrapper) { $comment = $wrapper->value(); if (!empty($comment->cid)) { // Comment is already saved. return; } $comment->uid = $this->getAccount()->uid; }
/** * {@inheritdoc} */ public function entityPreSave(\EntityDrupalWrapper $wrapper) { $node = $wrapper->value(); if (!empty($node->nid)) { // Node is already saved. return; } node_object_prepare($node); $node->uid = $this->getAccount()->uid; }
/** * {@inheritdoc} */ public function entityValidate(\EntityDrupalWrapper $wrapper) { if (!module_exists('entity_validator')) { // Entity validator doesn't exist. return; } try { $validator_handler = ValidatorPluginManager::EntityValidator($wrapper->type(), $wrapper->getBundle()); } catch (PluginNotFoundException $e) { // Entity validator handler doesn't exist for the entity. return; } if ($validator_handler->validate($wrapper->value(), TRUE)) { // Entity is valid. return; } $errors = $validator_handler->getErrors(FALSE); $map = array(); foreach ($this->fieldDefinitions as $resource_field_name => $resource_field) { if (!($property = $resource_field->getProperty())) { continue; } $public_name = $resource_field->getPublicName(); if (empty($errors[$public_name])) { // Field validated. continue; } $map[$public_name] = $resource_field_name; $params['@fields'][] = $resource_field_name; } if (empty($params['@fields'])) { // There was a validation error, but on non-public fields, so we need to // throw an exception, but can't say on which fields it occurred. throw new BadRequestException('Invalid value(s) sent with the request.'); } $params['@fields'] = implode(',', $params['@fields']); $exception = new BadRequestException(format_plural(count($map), 'Invalid value in field @fields.', 'Invalid values in fields @fields.', $params)); foreach ($errors as $property_name => $messages) { if (empty($map[$property_name])) { // Entity is not valid, but on a field not public. continue; } $resource_field_name = $map[$property_name]; foreach ($messages as $message) { $message['params']['@field'] = $resource_field_name; $output = format_string($message['message'], $message['params']); $exception->addFieldError($resource_field_name, $output); } } // Throw the exception. throw $exception; }
/** * Get value from a field rendered by Drupal field API's formatter. * * @param \EntityMetadataWrapper $property_wrapper * The property wrapper. Either \EntityDrupalWrapper or \EntityListWrapper. * @param \EntityDrupalWrapper $wrapper * The entity wrapper. * * @return mixed * A single or multiple values. * * @throws \Drupal\restful\Exception\ServerConfigurationException */ protected function formatterValue(\EntityMetadataWrapper $property_wrapper, \EntityDrupalWrapper $wrapper) { $value = NULL; if (!ResourceFieldEntity::propertyIsField($this->getProperty())) { // Property is not a field. throw new ServerConfigurationException(format_string('@property is not a configurable field, so it cannot be processed using field API formatter', array('@property' => $this->getProperty()))); } // Get values from the formatter. $output = field_view_field($this->getEntityType(), $wrapper->value(), $this->getProperty(), $this->getFormatter()); // Unset the theme, as we just want to get the value from the formatter, // without the wrapping HTML. unset($output['#theme']); if ($property_wrapper instanceof \EntityListWrapper) { // Multiple values. foreach (element_children($output) as $delta) { $value[] = drupal_render($output[$delta]); } } else { // Single value. $value = drupal_render($output); } return $value; }