示例#1
0
 /**
  * {@inheritdoc}
  */
 public function normalize($entity, $format = NULL, array $context = array())
 {
     $data = parent::normalize($entity, $format, $context);
     // Replace the file url with a full url for the file.
     $data['uri'][0]['value'] = $this->getEntityUri($entity);
     return $data;
 }
 /**
  * {@inheritdoc}
  */
 public function denormalize($data, $class, $format = NULL, array $context = array())
 {
     // File content can be passed base64 encoded in a special "data" property.
     // That property is not a field, so we remove it before denormalizing the
     // rest of the file entity.
     $file_data = $data['data'][0]['value'];
     unset($data['data']);
     $entity = parent::denormalize($data, $class, $format, $context);
     // Decode and save to file if it's a new file.
     if (!isset($context['request_method']) || $context['request_method'] != 'patch') {
         $file_contents = base64_decode($file_data);
         $dirname = $this->fileSystem->dirname($entity->getFileUri());
         file_prepare_directory($dirname, FILE_CREATE_DIRECTORY);
         if ($uri = file_unmanaged_save_data($file_contents, file_build_uri(drupal_basename($entity->getFilename())))) {
             $entity->setFileUri($uri);
         } else {
             throw new RuntimeException('failed to write ' . $entity->getFilename());
         }
     }
     return $entity;
 }