public function ContactMessageResource::post in Contact message REST 8
Responds to entity POST requests and saves the new entity.
Parameters
\Drupal\contact\MessageInterface $message: The Message entity.
Return value
\Drupal\rest\ResourceResponse The HTTP response object.
Throws
\Symfony\Component\HttpKernel\Exception\HttpException Throws HttpException in case of error.
File
- src/
Plugin/ rest/ resource/ ContactMessageResource.php, line 137 - Contains \Drupal\contact_message_rest\Plugin\rest\resource\ContactMessageResource.
Class
- ContactMessageResource
- Creates a resource for adding contact Message entities and sending them.
Namespace
Drupal\contact_message_rest\Plugin\rest\resourceCode
public function post(MessageInterface $message = NULL) {
if ($message == NULL) {
throw new BadRequestHttpException('No entity content received.');
}
if (!$message
->access('create')) {
throw new AccessDeniedHttpException();
}
// POSTed entities must not have an ID set, because we always want to create
// new entities here.
if (!$message
->isNew()) {
throw new BadRequestHttpException('Only new entities can be created');
}
// Only check 'edit' permissions for fields that were actually
// submitted by the user. Field access makes no difference between 'create'
// and 'update', so the 'edit' operation is used here.
foreach ($message->_restSubmittedFields as $key => $field_name) {
if (!$message
->get($field_name)
->access('edit')) {
throw new AccessDeniedHttpException("Access denied on creating field '{$field_name}'");
}
}
// Validate the received data before saving.
$this
->validate($message);
// Send out the contact message via mail.
$this->mailHandler
->sendMailMessages($message, $this->currentUser);
// Register submission with the flood service.
$this->flood
->register('contact', $this->config
->get('flood.interval'));
// Try saving the message entity. This will only be useful when
// contact_storage module is installed.
try {
$message
->save();
} catch (EntityStorageException $e) {
throw new HttpException(500, 'Internal Server Error', $e);
}
return new Response('', 200);
}