Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Rest Resource Plugin
<?php
namespace Drupal\ssync_client\Plugin\rest\resource;
use Drupal\file\Entity\File;
use Drupal\rest\Plugin\ResourceBase;
use Drupal\rest\ResourceResponse;
/**
* Manipulate the image creation.
*
* @RestResource(
* id = "ssync_slave_create_file",
* label = @Translation("Ssync Slave rest resource"),
* serialization_class = "",
* uri_paths = {
* "canonical" = "/ssync/create_file"
* }
* )
*/
class CreateFileResource extends ResourceBase {
/**
* Handle the post METHOD.
*
* @param array $data
* Post Array.
*
* @return \Drupal\rest\ResourceResponse
* Resource formatted with JSON API.
*/
public function post(array $data = []) {
$uri_file_unmanaged = file_unmanaged_save_data(file_get_contents($data->attributes->url), file_default_scheme() . '://');
$file = File::create([
'uuid' => $data->id,
'uri' => $uri_file_unmanaged,
]);
$file->save();
return new ResourceResponse($file);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.