modules/acm_sku/src/Entity/SKU.php
public static function loadFromSku($sku, $langcode = '', $log_not_found = TRUE, $create_translation = FALSE) {

    $skus_static_cache = &drupal_static(__FUNCTION__, []);

    $is_multilingual = \Drupal::languageManager()->isMultilingual();
    if ($is_multilingual && empty($langcode)) {
      $langcode = \Drupal::languageManager()->getCurrentLanguage()->getId();
    }

    $static_cache_sku_identifier = $sku . ':' . $langcode;

    // Check if data is available in static cache, return from there.
    // If create translation is true, it means we are doing product sync.
    // For this case we don't want to use any static cache.
    if (isset($skus_static_cache[$static_cache_sku_identifier]) && !$create_translation) {
      return $skus_static_cache[$static_cache_sku_identifier];
    }

    $storage = \Drupal::entityTypeManager()->getStorage('acm_sku');
    $skus = $storage->loadByProperties(['sku' => $sku]);

    if (count($skus) == 0) {
      // We don't log the error while doing sync.
      if ($log_not_found) {
        \Drupal::logger('acm_sku')->error('No SKU found for @sku.', ['@sku' => $sku]);
      }
      return NULL;
    }

    $sku_entity = reset($skus);

    // Sanity check.
    if (!($sku_entity instanceof SKUInterface)) {
      return NULL;
    }

    // Now discard all skus in other languages if there is more than one.
    if ($is_multilingual && count($skus) > 1) {
      // Get rid of undesired languages. Later the first sku is picked up.
      foreach ($skus as $key => $skuEntity) {
        if ($skuEntity->language()->getId() != $langcode) {
          unset($skus[$key]);
        }
      }
    }

    // Now test if there is still more than one sku found.
    // Noting for multiple entries, we just log the error
    // and continue with first sku.
    if (count($skus) > 1) {
      \Drupal::logger('acm_sku')->error('Duplicate SKUs found while loading for @sku.', ['@sku' => $sku]);
    }

    if ($is_multilingual) {
      if ($sku_entity->hasTranslation($langcode)) {
        $sku_entity = $sku_entity->getTranslation($langcode);

        // Set value in static variable.
        // We set in static cache only for proper case, when returning different
        // language or creating translation we can avoid static cache.
        $skus_static_cache[$static_cache_sku_identifier] = $sku_entity;
      }
      elseif ($create_translation) {
        $sku_entity = $sku_entity->addTranslation($langcode, ['sku' => $sku]);
      }
      // We will continue execution with available translation and just log
      // a message. During sync we say don't log messages.
      elseif ($log_not_found) {
        \Drupal::logger('acm_sku')->error('SKU translation not found of @sku for @langcode', ['@sku' => $sku, '@langcode' => $langcode]);
      }
    }
    else {
      // Set value in static variable directly if not a multi-lingual site.
      $skus_static_cache[$static_cache_sku_identifier] = $sku_entity;
    }

    return $sku_entity;
  }
public static function loadFromSku($sku, $langcode = '', $log_not_found = TRUE, $create_translation = FALSE) {

    $skus_static_cache = &drupal_static(__FUNCTION__, []);

    $is_multilingual = \Drupal::languageManager()->isMultilingual();
    if ($is_multilingual && empty($langcode)) {
      $langcode = \Drupal::languageManager()->getCurrentLanguage()->getId();
    }

    $static_cache_sku_identifier = $sku . ':' . $langcode;

    // Check if data is available in static cache, return from there.
    // If create translation is true, it means we are doing product sync.
    // For this case we don't want to use any static cache.
    if (isset($skus_static_cache[$static_cache_sku_identifier]) && !$create_translation) {
      return $skus_static_cache[$static_cache_sku_identifier];
    }

    $storage = \Drupal::entityTypeManager()->getStorage('acm_sku');
    $skus = $storage->loadByProperties(['sku' => $sku]);

    if (count($skus) == 0) {
      // We don't log the error while doing sync.
      if ($log_not_found) {
        \Drupal::logger('acm_sku')->error('No SKU found for @sku.', ['@sku' => $sku]);
      }
      return NULL;
    }

    $sku_entity = reset($skus);

    // Sanity check.
    if (!($sku_entity instanceof SKUInterface)) {
      return NULL;
    }

    // Now discard all skus in other languages if there is more than one.
    if ($is_multilingual && count($skus) > 1) {
      // Get rid of undesired languages. Later the first sku is picked up.
      foreach ($skus as $key => $skuEntity) {
        if ($skuEntity->language()->getId() != $langcode) {
          unset($skus[$key]);
        }
      }
    }

    // Now test if there is still more than one sku found.
    // Noting for multiple entries, we just log the error
    // and continue with first sku.
    if (count($skus) > 1) {
      \Drupal::logger('acm_sku')->error('Duplicate SKUs found while loading for @sku.', ['@sku' => $sku]);
    }

    if ($is_multilingual) {
      if ($sku_entity->hasTranslation($langcode)) {
        $sku_entity = $sku_entity->getTranslation($langcode);

        // Set value in static variable.
        // We set in static cache only for proper case, when returning different
        // language or creating translation we can avoid static cache.
        $skus_static_cache[$static_cache_sku_identifier] = $sku_entity;
      }
      elseif ($create_translation) {
        $sku_entity = $sku_entity->addTranslation($langcode, ['sku' => $sku]);
      }
      // We will continue execution with available translation and just log
      // a message. During sync we say don't log messages.
      elseif ($log_not_found) {
        \Drupal::logger('acm_sku')->error('SKU translation not found of @sku for @langcode', ['@sku' => $sku, '@langcode' => $langcode]);
      }
    }
    else {
      // Set value in static variable directly if not a multi-lingual site.
      $skus_static_cache[$static_cache_sku_identifier] = $sku_entity;
    }

    return $sku_entity;
  }
public static function loadFromSku ( $ sku , $ langcode = '' , $ log_not_found = TRUE , $ create_translation = FALSE ) { $ skus_static_cache = & drupal_static ( __FUNCTION__ , [ ] ) ; $ is_multilingual = \ Drupal :: languageManager ( ) -> isMultilingual ( ) ; if ( $ is_multilingual && empty ( $ langcode ) ) { $ langcode = \ Drupal :: languageManager ( ) -> getCurrentLanguage ( ) -> getId ( ) ; } $ static_cache_sku_identifier = $ sku . ':' . $ langcode ; // Check if data is available in static cache, return from there. // If create translation is true, it means we are doing product sync. // For this case we don't want to use any static cache. if ( isset ( $ skus_static_cache [ $ static_cache_sku_identifier ] ) && ! $ create_translation ) { return $ skus_static_cache [ $ static_cache_sku_identifier ] ; } $ storage = \ Drupal :: entityTypeManager ( ) -> getStorage ( 'acm_sku' ) ; $ skus = $ storage -> loadByProperties ( [ 'sku' => $ sku ] ) ; if ( count ( $ skus ) == 0 ) { // We don't log the error while doing sync. if ( $ log_not_found ) { \ Drupal :: logger ( 'acm_sku' ) -> error ( 'No SKU found for @sku.' , [ '@sku' => $ sku ] ) ; } return NULL ; } $ sku_entity = reset ( $ skus ) ; // Sanity check. if ( ! ( $ sku_entity instanceof SKUInterface ) ) { return NULL ; } // Now discard all skus in other languages if there is more than one. if ( $ is_multilingual && count ( $ skus ) > 1 ) { // Get rid of undesired languages. Later the first sku is picked up. foreach ( $ skus as $ key => $ skuEntity ) { if ( $ skuEntity -> language ( ) -> getId ( ) != $ langcode ) { unset ( $ skus [ $ key ] ) ; } } } // Now test if there is still more than one sku found. // Noting for multiple entries, we just log the error // and continue with first sku. if ( count ( $ skus ) > 1 ) { \ Drupal :: logger ( 'acm_sku' ) -> error ( 'Duplicate SKUs found while loading for @sku.' , [ '@sku' => $ sku ] ) ; } if ( $ is_multilingual ) { if ( $ sku_entity -> hasTranslation ( $ langcode ) ) { $ sku_entity = $ sku_entity -> getTranslation ( $ langcode ) ; // Set value in static variable. // We set in static cache only for proper case, when returning different // language or creating translation we can avoid static cache. $ skus_static_cache [ $ static_cache_sku_identifier ] = $ sku_entity ; } elseif ( $ create_translation ) { $ sku_entity = $ sku_entity -> addTranslation ( $ langcode , [ 'sku' => $ sku ] ) ; } // We will continue execution with available translation and just log // a message. During sync we say don't log messages. elseif ( $ log_not_found ) { \ Drupal :: logger ( 'acm_sku' ) -> error ( 'SKU translation not found of @sku for @langcode' , [ '@sku' => $ sku , '@langcode' => $ langcode ] ) ; } } else { // Set value in static variable directly if not a multi-lingual site. $ skus_static_cache [ $ static_cache_sku_identifier ] = $ sku_entity ; } return $ sku_entity ; }
Loads a SKU Entity from SKU.

@param string $sku
SKU to load.
@param string $langcode
Language code.
@param bool $log_not_found
Log errors when store not found. Can be false during sync.
@param bool $create_translation
Create translation and return if entity available but translation is not.

@return SKU|null
Found SKU

@throws \Exception
Loads a SKU Entity from SKU .