Skip to content

Instantly share code, notes, and snippets.

@killua99
Created December 19, 2016 11:34
Show Gist options
  • Save killua99/9cc08f147e2d0d43ca6c1b8799d99833 to your computer and use it in GitHub Desktop.
Save killua99/9cc08f147e2d0d43ca6c1b8799d99833 to your computer and use it in GitHub Desktop.
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