modules/acm_sku/src/ProductManager.php
protected function getProcessedMedia(array $product, $current_value) {
    $media = [];

    // The Commerce Connector Service sends the media information
    // in $product['extension']['media'][0].
    // We note $product['extension']['media'][1] is deliberately always empty.
    // See Magento module method...
    // Model/ProductSyncManagement::processMediaGalleryExtension()
    // TODO What does Hybris send? The connector doesn't normalize.
    if (isset(
      $product['extension'],
      $product['extension']['media'],
      $product['extension']['media'][0])) {

      // Conveniently reset() returns the first element of the array.
      $media = reset($product['extension']['media']);

      if (isset($product['attributes'], $product['attributes']['image'])) {
        $image = $product['attributes']['image'];

        // If the base image is in the gallery then position it first.
        // But why? Is this an Acquia Commerce Manager requirement?
        // It is normal in Magento to set a gallery position for the base image
        // And to set images as 'hide in gallery' ie ['disabled'] = 1
        // So I suggest we honor the Magento data here.
        // TODO What does Hybris send? (what would the normalization be?)
        // Maybe we just bring in all images and let the Drupal user decide
        // what to display and in which position.
        // Later, will the Drupal theme honor ['disabled'] = 1 or do we
        // need to omit ['disabled'] = 1 images here.
        foreach ($media as &$data) {
          if (substr_compare($data['file'], $image, -strlen($image)) === 0) {
            $data['position'] = -1;
            break;
          }
        }
      }

      // Sort media data by position. Noting disabled images are included.
      usort($media, function ($a, $b) {
        $position1 = (int) $a['position'];
        $position2 = (int) $b['position'];

        if ($position1 == $position2) {
          return 0;
        }

        return ($position1 < $position2) ? -1 : 1;
      });
    }

    // Reassign old files to not have to redownload them.
    if (!empty($media)) {
      $current_media = unserialize($current_value);
      if (!empty($current_media) && is_array($current_media)) {
        $current_mapping = [];
        foreach ($current_media as $value) {
          if (!empty($value['fid'])) {
            $current_mapping[$value['value_id']]['fid'] = $value['fid'];
            $current_mapping[$value['value_id']]['file'] = $value['file'];
          }
        }

        foreach ($media as $key => $value) {
          if (isset($current_mapping[$value['value_id']])) {
            $media[$key]['fid'] = $current_mapping[$value['value_id']]['fid'];
            $media[$key]['file'] = $current_mapping[$value['value_id']]['file'];
          }
        }
      }
    }

    return serialize($media);
  }
protected function getProcessedMedia(array $product, $current_value) {
    $media = [];

    // The Commerce Connector Service sends the media information
    // in $product['extension']['media'][0].
    // We note $product['extension']['media'][1] is deliberately always empty.
    // See Magento module method...
    // Model/ProductSyncManagement::processMediaGalleryExtension()
    // TODO What does Hybris send? The connector doesn't normalize.
    if (isset(
      $product['extension'],
      $product['extension']['media'],
      $product['extension']['media'][0])) {

      // Conveniently reset() returns the first element of the array.
      $media = reset($product['extension']['media']);

      if (isset($product['attributes'], $product['attributes']['image'])) {
        $image = $product['attributes']['image'];

        // If the base image is in the gallery then position it first.
        // But why? Is this an Acquia Commerce Manager requirement?
        // It is normal in Magento to set a gallery position for the base image
        // And to set images as 'hide in gallery' ie ['disabled'] = 1
        // So I suggest we honor the Magento data here.
        // TODO What does Hybris send? (what would the normalization be?)
        // Maybe we just bring in all images and let the Drupal user decide
        // what to display and in which position.
        // Later, will the Drupal theme honor ['disabled'] = 1 or do we
        // need to omit ['disabled'] = 1 images here.
        foreach ($media as &$data) {
          if (substr_compare($data['file'], $image, -strlen($image)) === 0) {
            $data['position'] = -1;
            break;
          }
        }
      }

      // Sort media data by position. Noting disabled images are included.
      usort($media, function ($a, $b) {
        $position1 = (int) $a['position'];
        $position2 = (int) $b['position'];

        if ($position1 == $position2) {
          return 0;
        }

        return ($position1 < $position2) ? -1 : 1;
      });
    }

    // Reassign old files to not have to redownload them.
    if (!empty($media)) {
      $current_media = unserialize($current_value);
      if (!empty($current_media) && is_array($current_media)) {
        $current_mapping = [];
        foreach ($current_media as $value) {
          if (!empty($value['fid'])) {
            $current_mapping[$value['value_id']]['fid'] = $value['fid'];
            $current_mapping[$value['value_id']]['file'] = $value['file'];
          }
        }

        foreach ($media as $key => $value) {
          if (isset($current_mapping[$value['value_id']])) {
            $media[$key]['fid'] = $current_mapping[$value['value_id']]['fid'];
            $media[$key]['file'] = $current_mapping[$value['value_id']]['file'];
          }
        }
      }
    }

    return serialize($media);
  }
protected function getProcessedMedia ( array $ product , $ current_value ) { $ media = [ ] ; // The Commerce Connector Service sends the media information // in $product['extension']['media'][0]. // We note $product['extension']['media'][1] is deliberately always empty. // See Magento module method... // Model/ProductSyncManagement::processMediaGalleryExtension() // TODO What does Hybris send? The connector doesn't normalize. if ( isset ( $ product [ 'extension' ] , $ product [ 'extension' ] [ 'media' ] , $ product [ 'extension' ] [ 'media' ] [ 0 ] ) ) { // Conveniently reset() returns the first element of the array. $ media = reset ( $ product [ 'extension' ] [ 'media' ] ) ; if ( isset ( $ product [ 'attributes' ] , $ product [ 'attributes' ] [ 'image' ] ) ) { $ image = $ product [ 'attributes' ] [ 'image' ] ; // If the base image is in the gallery then position it first. // But why? Is this an Acquia Commerce Manager requirement? // It is normal in Magento to set a gallery position for the base image // And to set images as 'hide in gallery' ie ['disabled'] = 1 // So I suggest we honor the Magento data here. // TODO What does Hybris send? (what would the normalization be?) // Maybe we just bring in all images and let the Drupal user decide // what to display and in which position. // Later, will the Drupal theme honor ['disabled'] = 1 or do we // need to omit ['disabled'] = 1 images here. foreach ( $ media as & $ data ) { if ( substr_compare ( $ data [ 'file' ] , $ image , - strlen ( $ image ) ) === 0 ) { $ data [ 'position' ] = - 1 ; break ; } } } // Sort media data by position. Noting disabled images are included. usort ( $ media , function ( $ a , $ b ) { $ position1 = ( int ) $ a [ 'position' ] ; $ position2 = ( int ) $ b [ 'position' ] ; if ( $ position1 == $ position2 ) { return 0 ; } return ( $ position1 < $ position2 ) ? - 1 : 1 ; } ) ; } // Reassign old files to not have to redownload them. if ( ! empty ( $ media ) ) { $ current_media = unserialize ( $ current_value ) ; if ( ! empty ( $ current_media ) && is_array ( $ current_media ) ) { $ current_mapping = [ ] ; foreach ( $ current_media as $ value ) { if ( ! empty ( $ value [ 'fid' ] ) ) { $ current_mapping [ $ value [ 'value_id' ] ] [ 'fid' ] = $ value [ 'fid' ] ; $ current_mapping [ $ value [ 'value_id' ] ] [ 'file' ] = $ value [ 'file' ] ; } } foreach ( $ media as $ key => $ value ) { if ( isset ( $ current_mapping [ $ value [ 'value_id' ] ] ) ) { $ media [ $ key ] [ 'fid' ] = $ current_mapping [ $ value [ 'value_id' ] ] [ 'fid' ] ; $ media [ $ key ] [ 'file' ] = $ current_mapping [ $ value [ 'value_id' ] ] [ 'file' ] ; } } } } return serialize ( $ media ) ; }
Extracts and orders the media gallery by position and sets the main image.

Extracts the media gallery fields from product[extension][media][0].
Orders the media gallery by position and sets the base (main) image
to be positioned first.
Disabled images are left in place.

@param array $product
Array with product information.
@param string $current_value
The current media data of the sku bein processed.

@return string
Serialized array with product's media information.
Extracts and orders the media gallery by position and sets the main image .