modules/acm_sku/src/Entity/SKU.php
protected function processMediaItem(&$update_sku, array &$data, $download = FALSE) {
    $media_item = $data;

    // Processing is required only for media type image as of now.
    if (isset($data['media_type']) && $data['media_type'] == 'image') {
      if (!empty($data['fid'])) {
        $file = File::load($data['fid']);
        if (!($file instanceof FileInterface)) {
          \Drupal::logger('acm_sku')->error('Empty file object for fid @fid on sku "@sku"', [
            '@fid' => $data['fid'],
            '@sku' => $this->getSku(),
          ]);

          unset($data['fid']);

          // Try to download again if download flag is set to true.
          if ($download) {
            return $this->processMediaItem($update_sku, $data, TRUE);
          }
        }
      }
      elseif ($download) {
        try {
          // Prepare the File object when we access it the first time.
          $file = $this->downloadMediaImage($data);
          $update_sku = TRUE;
        }
        catch (\Exception $e) {
          \Drupal::logger('acm_sku')->error($e->getMessage());
          return NULL;
        }
      }

      if ($file instanceof FileInterface) {
        $data['fid'] = $file->id();
        $media_item['fid'] = $data['fid'];
        $media_item['file'] = $file;
      }

      if (empty($data['label'])) {
        $media_item['label'] = $this->label();
      }

      return $media_item;
    }
  }
protected function processMediaItem(&$update_sku, array &$data, $download = FALSE) {
    $media_item = $data;

    // Processing is required only for media type image as of now.
    if (isset($data['media_type']) && $data['media_type'] == 'image') {
      if (!empty($data['fid'])) {
        $file = File::load($data['fid']);
        if (!($file instanceof FileInterface)) {
          \Drupal::logger('acm_sku')->error('Empty file object for fid @fid on sku "@sku"', [
            '@fid' => $data['fid'],
            '@sku' => $this->getSku(),
          ]);

          unset($data['fid']);

          // Try to download again if download flag is set to true.
          if ($download) {
            return $this->processMediaItem($update_sku, $data, TRUE);
          }
        }
      }
      elseif ($download) {
        try {
          // Prepare the File object when we access it the first time.
          $file = $this->downloadMediaImage($data);
          $update_sku = TRUE;
        }
        catch (\Exception $e) {
          \Drupal::logger('acm_sku')->error($e->getMessage());
          return NULL;
        }
      }

      if ($file instanceof FileInterface) {
        $data['fid'] = $file->id();
        $media_item['fid'] = $data['fid'];
        $media_item['file'] = $file;
      }

      if (empty($data['label'])) {
        $media_item['label'] = $this->label();
      }

      return $media_item;
    }
  }
protected function processMediaItem ( & $ update_sku , array & $ data , $ download = FALSE ) { $ media_item = $ data ; // Processing is required only for media type image as of now. if ( isset ( $ data [ 'media_type' ] ) && $ data [ 'media_type' ] == 'image' ) { if ( ! empty ( $ data [ 'fid' ] ) ) { $ file = File :: load ( $ data [ 'fid' ] ) ; if ( ! ( $ file instanceof FileInterface ) ) { \ Drupal :: logger ( 'acm_sku' ) -> error ( 'Empty file object for fid @fid on sku "@sku"' , [ '@fid' => $ data [ 'fid' ] , '@sku' => $ this -> getSku ( ) , ] ) ; unset ( $ data [ 'fid' ] ) ; // Try to download again if download flag is set to true. if ( $ download ) { return $ this -> processMediaItem ( $ update_sku , $ data , TRUE ) ; } } } elseif ( $ download ) { try { // Prepare the File object when we access it the first time. $ file = $ this -> downloadMediaImage ( $ data ) ; $ update_sku = TRUE ; } catch ( \ Exception $ e ) { \ Drupal :: logger ( 'acm_sku' ) -> error ( $ e -> getMessage ( ) ) ; return NULL ; } } if ( $ file instanceof FileInterface ) { $ data [ 'fid' ] = $ file -> id ( ) ; $ media_item [ 'fid' ] = $ data [ 'fid' ] ; $ media_item [ 'file' ] = $ file ; } if ( empty ( $ data [ 'label' ] ) ) { $ media_item [ 'label' ] = $ this -> label ( ) ; } return $ media_item ; } }
Function to get processed media item with File entity in array.

@param bool $update_sku
Flag to specify if SKU should be updated or not.
Update is done in parent function, here we only update the flag.
@param array $data
Media item array.
@param bool $download
Flag to specify if we should download missing images or not.

@return array|null
Processed media item or null if some error occurred.
Function to get processed media item with File entity in array .