public function putToNode(
    NodeInterface $node,
    MediaTypeInterface $media_type,
    TermInterface $taxonomy_term,
    Request $request
  ) {
    $content_type = $request->headers->get('Content-Type', "");

    if (empty($content_type)) {
      throw new BadRequestHttpException("Missing Content-Type header");
    }

    $content_location = $request->headers->get('Content-Location', "");

    // Since we create both a Media and its File,
    // start a transaction.
    $transaction = $this->database->startTransaction();

    try {
      $media = $this->service->putToNode(
        $node,
        $media_type,
        $taxonomy_term,
        $request->getContent(TRUE),
        $content_type,
        $content_location
      );

      // We return the media if it was newly created.
      if ($media) {
        $response = new Response("", 201);
        $response->headers->set("Location", $media->url('canonical', ['absolute' => TRUE]));
      }
      else {
        $response = new Response("", 204);
      }
      return $response;
    }
    catch (HttpException $e) {
      $transaction->rollBack();
      throw $e;
    }
    catch (\Exception $e) {
      $transaction->rollBack();
      throw new HttpException(500, $e->getMessage());
    }
  }
public function putToNode(
    NodeInterface $node,
    MediaTypeInterface $media_type,
    TermInterface $taxonomy_term,
    Request $request
  ) {
    $content_type = $request->headers->get('Content-Type', "");

    if (empty($content_type)) {
      throw new BadRequestHttpException("Missing Content-Type header");
    }

    $content_location = $request->headers->get('Content-Location', "");

    // Since we create both a Media and its File,
    // start a transaction.
    $transaction = $this->database->startTransaction();

    try {
      $media = $this->service->putToNode(
        $node,
        $media_type,
        $taxonomy_term,
        $request->getContent(TRUE),
        $content_type,
        $content_location
      );

      // We return the media if it was newly created.
      if ($media) {
        $response = new Response("", 201);
        $response->headers->set("Location", $media->url('canonical', ['absolute' => TRUE]));
      }
      else {
        $response = new Response("", 204);
      }
      return $response;
    }
    catch (HttpException $e) {
      $transaction->rollBack();
      throw $e;
    }
    catch (\Exception $e) {
      $transaction->rollBack();
      throw new HttpException(500, $e->getMessage());
    }
  }
public function putToNode ( NodeInterface $ node , MediaTypeInterface $ media_type , TermInterface $ taxonomy_term , Request $ request ) { $ content_type = $ request -> headers -> get ( 'Content-Type' , "" ) ; if ( empty ( $ content_type ) ) { throw new BadRequestHttpException ( "Missing Content-Type header" ) ; } $ content_location = $ request -> headers -> get ( 'Content-Location' , "" ) ; // Since we create both a Media and its File, // start a transaction. $ transaction = $ this -> database -> startTransaction ( ) ; try { $ media = $ this -> service -> putToNode ( $ node , $ media_type , $ taxonomy_term , $ request -> getContent ( TRUE ) , $ content_type , $ content_location ) ; // We return the media if it was newly created. if ( $ media ) { $ response = new Response ( "" , 201 ) ; $ response -> headers -> set ( "Location" , $ media -> url ( 'canonical' , [ 'absolute' => TRUE ] ) ) ; } else { $ response = new Response ( "" , 204 ) ; } return $ response ; } catch ( HttpException $ e ) { $ transaction -> rollBack ( ) ; throw $ e ; } catch ( \ Exception $ e ) { $ transaction -> rollBack ( ) ; throw new HttpException ( 500 , $ e -> getMessage ( ) ) ; } }
Adds a Media to a Node using the specified field.

@param \Drupal\node\NodeInterface $node
The Node to which you want to add a Media.
@param \Drupal\media\MediaTypeInterface $media_type
Media type for new media.
@param \Drupal\taxonomy\TermInterface $taxonomy_term
Term from the 'Behavior' vocabulary to give to new media.
@param \Symfony\Component\HttpFoundation\Request $request
The request object.

@return \Symfony\Component\HttpFoundation\Response
201 on success with a Location link header.

@throws \Symfony\Component\HttpKernel\Exception\HttpException
Adds a Media to a Node using the specified field .