public function putToNode(
    NodeInterface $node,
    MediaTypeInterface $media_type,
    TermInterface $taxonomy_term,
    $resource,
    $mimetype,
    $content_location
  ) {
    $existing = $this->islandoraUtils->getMediaReferencingNodeAndTerm($node, $taxonomy_term);

    if (!empty($existing)) {
      // Just update already existing media.
      $media = $this->entityTypeManager->getStorage('media')->load(reset($existing));
      $this->updateSourceField(
          $media,
          $resource,
          $mimetype
      );
      return FALSE;
    }
    else {
      // Otherwise, the media doesn't exist yet.
      // So make everything by hand.
      // Get the source field for the media type.
      $bundle = $media_type->id();
      $source_field = $this->getSourceFieldName($bundle);
      if (empty($source_field)) {
        throw new NotFoundHttpException("Source field not set for $bundle media");
      }

      // Construct the File.
      $file = $this->entityTypeManager->getStorage('file')->create([
        'uid' => $this->account->id(),
        'uri' => $content_location,
        'filename' => $this->fileSystem->basename($content_location),
        'filemime' => $mimetype,
        'status' => FILE_STATUS_PERMANENT,
      ]);

      // Validate file extension.
      $source_field_config = $this->entityTypeManager->getStorage('field_config')->load("media.$bundle.$source_field");
      $valid_extensions = $source_field_config->getSetting('file_extensions');
      $errors = file_validate_extensions($file, $valid_extensions);

      if (!empty($errors)) {
        throw new BadRequestHttpException("Invalid file extension.  Valid types are $valid_extensions");
      }

      $directory = $this->fileSystem->dirname($content_location);
      if (!file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
        throw new HttpException(500, "The destination directory does not exist, could not be created, or is not writable");
      }

      // Copy over the file content.
      $this->updateFile($file, $resource, $mimetype);
      $file->save();

      // Construct the Media.
      $media_struct = [
        'bundle' => $bundle,
        'uid' => $this->account->id(),
        'name' => $file->getFilename(),
        'langcode' => $this->languageManager->getDefaultLanguage()->getId(),
        "$source_field" => [
          'target_id' => $file->id(),
        ],
        IslandoraUtils::MEDIA_OF_FIELD => [
          'target_id' => $node->id(),
        ],
        IslandoraUtils::MEDIA_USAGE_FIELD => [
          'target_id' => $taxonomy_term->id(),
        ],
      ];

      // Set alt text.
      if ($source_field_config->getSetting('alt_field') && $source_field_config->getSetting('alt_field_required')) {
        $media_struct[$source_field]['alt'] = $file->getFilename;
      }

      $media = $this->entityTypeManager->getStorage('media')->create($media_struct);
      $media->save();
      return $media;
    }

  }
public function putToNode(
    NodeInterface $node,
    MediaTypeInterface $media_type,
    TermInterface $taxonomy_term,
    $resource,
    $mimetype,
    $content_location
  ) {
    $existing = $this->islandoraUtils->getMediaReferencingNodeAndTerm($node, $taxonomy_term);

    if (!empty($existing)) {
      // Just update already existing media.
      $media = $this->entityTypeManager->getStorage('media')->load(reset($existing));
      $this->updateSourceField(
          $media,
          $resource,
          $mimetype
      );
      return FALSE;
    }
    else {
      // Otherwise, the media doesn't exist yet.
      // So make everything by hand.
      // Get the source field for the media type.
      $bundle = $media_type->id();
      $source_field = $this->getSourceFieldName($bundle);
      if (empty($source_field)) {
        throw new NotFoundHttpException("Source field not set for $bundle media");
      }

      // Construct the File.
      $file = $this->entityTypeManager->getStorage('file')->create([
        'uid' => $this->account->id(),
        'uri' => $content_location,
        'filename' => $this->fileSystem->basename($content_location),
        'filemime' => $mimetype,
        'status' => FILE_STATUS_PERMANENT,
      ]);

      // Validate file extension.
      $source_field_config = $this->entityTypeManager->getStorage('field_config')->load("media.$bundle.$source_field");
      $valid_extensions = $source_field_config->getSetting('file_extensions');
      $errors = file_validate_extensions($file, $valid_extensions);

      if (!empty($errors)) {
        throw new BadRequestHttpException("Invalid file extension.  Valid types are $valid_extensions");
      }

      $directory = $this->fileSystem->dirname($content_location);
      if (!file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
        throw new HttpException(500, "The destination directory does not exist, could not be created, or is not writable");
      }

      // Copy over the file content.
      $this->updateFile($file, $resource, $mimetype);
      $file->save();

      // Construct the Media.
      $media_struct = [
        'bundle' => $bundle,
        'uid' => $this->account->id(),
        'name' => $file->getFilename(),
        'langcode' => $this->languageManager->getDefaultLanguage()->getId(),
        "$source_field" => [
          'target_id' => $file->id(),
        ],
        IslandoraUtils::MEDIA_OF_FIELD => [
          'target_id' => $node->id(),
        ],
        IslandoraUtils::MEDIA_USAGE_FIELD => [
          'target_id' => $taxonomy_term->id(),
        ],
      ];

      // Set alt text.
      if ($source_field_config->getSetting('alt_field') && $source_field_config->getSetting('alt_field_required')) {
        $media_struct[$source_field]['alt'] = $file->getFilename;
      }

      $media = $this->entityTypeManager->getStorage('media')->create($media_struct);
      $media->save();
      return $media;
    }

  }
public function putToNode ( NodeInterface $ node , MediaTypeInterface $ media_type , TermInterface $ taxonomy_term , $ resource , $ mimetype , $ content_location ) { $ existing = $ this -> islandoraUtils -> getMediaReferencingNodeAndTerm ( $ node , $ taxonomy_term ) ; if ( ! empty ( $ existing ) ) { // Just update already existing media. $ media = $ this -> entityTypeManager -> getStorage ( 'media' ) -> load ( reset ( $ existing ) ) ; $ this -> updateSourceField ( $ media , $ resource , $ mimetype ) ; return FALSE ; } else { // Otherwise, the media doesn't exist yet. // So make everything by hand. // Get the source field for the media type. $ bundle = $ media_type -> id ( ) ; $ source_field = $ this -> getSourceFieldName ( $ bundle ) ; if ( empty ( $ source_field ) ) { throw new NotFoundHttpException ( "Source field not set for $bundle media" ) ; } // Construct the File. $ file = $ this -> entityTypeManager -> getStorage ( 'file' ) -> create ( [ 'uid' => $ this -> account -> id ( ) , 'uri' => $ content_location , 'filename' => $ this -> fileSystem -> basename ( $ content_location ) , 'filemime' => $ mimetype , 'status' => FILE_STATUS_PERMANENT , ] ) ; // Validate file extension. $ source_field_config = $ this -> entityTypeManager -> getStorage ( 'field_config' ) -> load ( "media.$bundle.$source_field" ) ; $ valid_extensions = $ source_field_config -> getSetting ( 'file_extensions' ) ; $ errors = file_validate_extensions ( $ file , $ valid_extensions ) ; if ( ! empty ( $ errors ) ) { throw new BadRequestHttpException ( "Invalid file extension.  Valid types are $valid_extensions" ) ; } $ directory = $ this -> fileSystem -> dirname ( $ content_location ) ; if ( ! file_prepare_directory ( $ directory , FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS ) ) { throw new HttpException ( 500 , "The destination directory does not exist, could not be created, or is not writable" ) ; } // Copy over the file content. $ this -> updateFile ( $ file , $ resource , $ mimetype ) ; $ file -> save ( ) ; // Construct the Media. $ media_struct = [ 'bundle' => $ bundle , 'uid' => $ this -> account -> id ( ) , 'name' => $ file -> getFilename ( ) , 'langcode' => $ this -> languageManager -> getDefaultLanguage ( ) -> getId ( ) , "$source_field" => [ 'target_id' => $ file -> id ( ) , ] , IslandoraUtils :: MEDIA_OF_FIELD => [ 'target_id' => $ node -> id ( ) , ] , IslandoraUtils :: MEDIA_USAGE_FIELD => [ 'target_id' => $ taxonomy_term -> id ( ) , ] , ] ; // Set alt text. if ( $ source_field_config -> getSetting ( 'alt_field' ) && $ source_field_config -> getSetting ( 'alt_field_required' ) ) { $ media_struct [ $ source_field ] [ 'alt' ] = $ file -> getFilename ; } $ media = $ this -> entityTypeManager -> getStorage ( 'media' ) -> create ( $ media_struct ) ; $ media -> save ( ) ; return $ media ; } }
Creates a new Media using the provided resource, adding it to a Node.

@param \Drupal\node\NodeInterface $node
The node to reference the newly created 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 resource $resource
New file contents as a resource.
@param string $mimetype
New mimetype of contents.
@param string $content_location
Drupal/PHP stream wrapper for where to upload the binary.

@throws HttpException
Creates a new Media using the provided resource adding it to a Node .