You are here

commerce_pricelist.module in Commerce Pricelist 8

Same filename and directory in other branches
  1. 8.2 commerce_pricelist.module
  2. 7 commerce_pricelist.module

File

commerce_pricelist.module
View source
<?php

/**
 * @file
 * Contains commerce_pricelist.module..
 */
use Drupal\commerce\PurchasableEntityInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Entity\EntityInterface;

/**
 * Implements hook_help().
 */
function commerce_pricelist_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {

    // Main module help for the commerce_pricelist module.
    case 'help.page.commerce_pricelist':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Set product prices through price lists') . '</p>';
      return $output;
    default:
  }
}

/**
 * Implements hook_entity_delete().
 */
function commerce_pricelist_entity_delete(EntityInterface $entity) {
  if ($entity
    ->getEntityType()
    ->entityClassImplements(PurchasableEntityInterface::class)) {

    // Get the map info between purchased entity type and price list item type.
    $price_list_item_type_ids = [];
    $price_list_item_types = \Drupal::entityTypeManager()
      ->getStorage('price_list_item_type')
      ->loadByProperties();

    /** @var \Drupal\commerce_pricelist\Entity\PriceListItemType $price_list_item_type */
    foreach ($price_list_item_types as $price_list_item_type_id => $price_list_item_type) {
      $purchased_entity_type_id = $price_list_item_type
        ->getPurchasableEntityTypeId();
      $price_list_item_type_ids[$purchased_entity_type_id] = $price_list_item_type_id;
    }

    // A purchasable entity was deleted. Delete all of its price list items.
    $price_list_item_storage = \Drupal::entityTypeManager()
      ->getStorage('price_list_item');
    $query = $price_list_item_storage
      ->getQuery();
    $query
      ->condition('type', $price_list_item_type_ids[$entity
      ->getEntityTypeId()]);
    $query
      ->condition('purchased_entity', $entity
      ->id());
    $result = $query
      ->execute();
    if (!empty($result)) {

      // @todo This can crash due to there potentially being thousands of items.
      $price_list_items = $price_list_item_storage
        ->loadMultiple($result);
      $price_list_item_storage
        ->delete($price_list_items);
    }
  }
}

/**
 * Implements hook_entity_update().
 */
function commerce_pricelist_entity_update(EntityInterface $entity) {
  if ($entity instanceof PurchasableEntityInterface) {

    /** @var \Drupal\commerce\PurchasableEntityInterface $entity */
    foreach ($entity
      ->get('field_price_list_item') as $item) {
      $entityAdaper = $item
        ->get('entity')
        ->getTarget();
      if ($entityAdaper) {

        /** @var \Drupal\commerce_pricelist\Entity\PriceListItem $itemEntity */
        $itemEntity = $entityAdaper
          ->getValue();
        $itemEntity
          ->setPurchasedEntityId($entity
          ->id());
        if (!$itemEntity
          ->getName() && $entity
          ->getTitle()) {
          $itemEntity
            ->setName($entity
            ->getTitle());
        }
        if (!$itemEntity
          ->getPrice() && $entity
          ->getPrice()) {
          $itemEntity
            ->setPrice($entity
            ->getPrice());
        }
        $itemEntity
          ->save();
      }
    }
  }
}

/**
 * Implements hook_theme_suggestions_HOOK().
 */
function commerce_pricelist_theme_suggestions_price_list(array $variables) {
  $suggestions = [];
  $entity = $variables['elements']['#price_list'];
  $sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
  $suggestions[] = 'price_list__' . $sanitized_view_mode;
  $suggestions[] = 'price_list__' . $entity
    ->bundle();
  $suggestions[] = 'price_list__' . $entity
    ->bundle() . '__' . $sanitized_view_mode;
  $suggestions[] = 'price_list__' . $entity
    ->id();
  $suggestions[] = 'price_list__' . $entity
    ->id() . '__' . $sanitized_view_mode;
  return $suggestions;
}