commerce_inventory-8.x-1.0-alpha6/modules/commerce_inventory_product/commerce_inventory_product.module

modules/commerce_inventory_product/commerce_inventory_product.module
<?php

/**
 * @file
 * Contains commerce_inventory_product.module.
 */

use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;

/**
 * Implements hook_help().
 */
function commerce_inventory_product_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    // Main module help for the commerce_inventory_product module.
    case 'help.page.commerce_inventory_product':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Allows Commerce Inventory to manage Commerce Product entities.') . '</p>';
      return $output;

    default:
  }
}

/**
 * Implements hook_entity_base_field_info_alter().
 */
function commerce_inventory_product_entity_base_field_info_alter(&$fields, EntityTypeInterface $entity_type) {
  /** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
  if ($entity_type->id() == 'commerce_inventory_item') {
    $entity_type_ids = $fields['purchasable_entity']->getSetting('entity_type_ids');
    $entity_type_ids[] = 'commerce_product_variation';
    $fields['purchasable_entity']->setSetting('entity_type_ids', array_unique($entity_type_ids));
  }
}

/**
 * Implements hook_entity_type_alter().
 */
function commerce_inventory_product_entity_type_alter(array &$entity_types) {
  /** @var \Drupal\Core\Entity\EntityTypeInterface[] $entity_types */
  $entity_types['commerce_inventory_location']->setLinkTemplate('inventory-add-product', '/admin/commerce/location/{commerce_inventory_location}/inventory/add');
}

/**
 * Implements hook_entity_view_display_alter().
 */
function commerce_inventory_product_entity_view_display_alter(EntityViewDisplayInterface $display, array $context) {
  if ($context['entity_type'] == 'commerce_product_variation' && $context['view_mode'] == 'commerce_inventory_product_attribute_summary') {
    if ($display->isNew() || $display->getOriginalMode() !== $display->getMode()) {
      /** @var \Drupal\commerce_product\ProductAttributeFieldManagerInterface $attribute_manager */
      $attribute_manager = \Drupal::service('commerce_product.attribute_field_manager');
      /** @var \Drupal\Core\Field\FormatterPluginManager $formatter_plugin_manager */
      $formatter_plugin_manager = \Drupal::service('plugin.manager.field.formatter');

      // If view-display isn't explicitly set, clear default settings and
      // display only attribute fields.
      $display->set('content', []);
      foreach ($attribute_manager->getFieldDefinitions($context['bundle']) as $field_name => $field_definition) {
        $options = $formatter_plugin_manager->prepareConfiguration($field_definition->getType(), [
          'label' => 'inline',
        ]);
        $options += ['settings' => [], 'third_party_settings' => []];
        $display->setComponent($field_name, $options);
      }
    }
  }
}

/**
 * Implements hook_ENTITY_TYPE_delete() for commerce_product_variation.
 */
function commerce_inventory_product_commerce_product_variation_delete(EntityInterface $entity) {
  /** @var \Drupal\commerce\PurchasableEntityInterface $entity */
  /** @var \Drupal\commerce_inventory\Entity\Storage\InventoryItemStorageInterface $item_storage */
  $item_storage = \Drupal::entityTypeManager()->getStorage('commerce_inventory_item');
  $items = $item_storage->loadItemsByPurchasableEntity($entity->getEntityTypeId(), $entity->id());

  // Deactivate all items.
  foreach ($items as $item) {
    $item->inactivate();
    $item->save();
  }
}

/**
 * Implements hook_ENTITY_TYPE_access() for commerce_product.
 */
function commerce_inventory_product_commerce_product_access(EntityInterface $entity, $operation, AccountInterface $account) {
  /** @var \Drupal\commerce_product\Entity\Product $entity */
  if ($operation == 'delete' && $entity->getDefaultVariation()) {
    /** @var \Drupal\commerce_inventory\Entity\Storage\InventoryItemStorageInterface $storage */
    $storage = \Drupal::entityTypeManager()->getStorage('commerce_inventory_item');
    $ids = $storage->getItemIdsByPurchasableEntity($entity->getDefaultVariation()->getEntityTypeId(), $entity->getVariationIds());
    if (!empty($ids)) {
      // @todo create permission for overriding ability to delete.
      return AccessResult::forbidden('Purchasable Entity currently used in Inventory management.');
    }
  }

  return AccessResult::neutral();
}

/**
 * Implements hook_ENTITY_TYPE_access() for commerce_product_variation.
 */
function commerce_inventory_product_commerce_product_variation_access(EntityInterface $entity, $operation, AccountInterface $account) {
  if ($operation == 'delete') {
    /** @var \Drupal\commerce_inventory\Entity\Storage\InventoryItemStorageInterface $storage */
    $storage = \Drupal::entityTypeManager()->getStorage('commerce_inventory_item');
    $ids = $storage->getItemIdsByPurchasableEntity($entity->getEntityTypeId(), $entity->id());
    if (!empty($ids)) {
      // @todo create permission for overriding ability to delete.
      return AccessResult::forbidden('Purchasable Entity currently used in Inventory management.');
    }
  }

  return AccessResult::neutral();
}

/**
 * Implements hook_form_BASE_FORM_ID_alter() for views_form_commerce_inventory_location_add_product_inventory_add_product.
 */
function commerce_inventory_product_form_views_form_commerce_inventory_location_add_product_inventory_add_product_alter(&$form, FormStateInterface $form_state, $form_id) {
  $form['#submit'][] = 'commerce_inventory_product_form_views_form_commerce_inventory_location_add_product_inventory_add_product_submit';
}

/**
 * Submit callback set in commerce_inventory_form_views_form_commerce_inventory_location_add_product_inventory_add_product_alter().
 */
function commerce_inventory_product_form_views_form_commerce_inventory_location_add_product_inventory_add_product_submit(&$form, FormStateInterface $form_state) {
  // Fix to pass current route parameters into view bulk operations.
  // Follow https://www.drupal.org/node/2901412.
  if ($form_state->getRedirect()) {
    foreach (\Drupal::routeMatch()->getRawParameters()->all() as $key => $parameter) {
      $form_state->getRedirect()->setRouteParameter($key, $parameter);
    }
  }
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc