commerce_product_bundles-8.x-1.0/commerce_product_bundles.install
commerce_product_bundles.install
<?php use Drupal\Core\Session\AccountInterface; /** * Implements hook_install(). * Grant 'View Commerce Product Bundle' permission to admin and regular user. */ function commerce_product_bundles_install() { // Allow all roles to view published products. user_role_grant_permissions(AccountInterface::ANONYMOUS_ROLE, ['view commerce_product_bundles']); user_role_grant_permissions(AccountInterface::AUTHENTICATED_ROLE, ['view commerce_product_bundles']); } /** * Update the 'uid' field for bundle product and bundle variations to use EntityOwnerTrait implementation. */ function commerce_product_bundles_update_8201() { $definition_update_manager = \Drupal::entityDefinitionUpdateManager(); $base_field_override_storage = \Drupal::entityTypeManager()->getStorage('base_field_override'); foreach (['commerce_product_bundles', 'commerce_bundle_variation'] as $entity_type_id) { $storage_definition = $definition_update_manager->getFieldStorageDefinition('uid', $entity_type_id); $default_value_callback = $entity_type_id === 'commerce_product_bundles' ? ProductBundle::class . '::getDefaultEntityOwner' : ProductBundleVariation::class . '::getDefaultEntityOwner'; $base_field_overrides = $base_field_override_storage->loadByProperties([ 'entity_type' => $entity_type_id, 'field_name' => 'uid', ]); /** @var \Drupal\Core\Field\FieldDefinitionInterface $base_field_override */ foreach ($base_field_overrides as $base_field_override) { if ($base_field_override->getDefaultValueCallback() !== $storage_definition->getDefaultValueCallback()) { continue; } $base_field_override->setDefaultValueCallback($default_value_callback); $base_field_override->save(); } $storage_definition->setDefaultValueCallback($default_value_callback); $definition_update_manager->updateFieldStorageDefinition($storage_definition); } } /** * Implements hook_install(). * * Set duplicated PB class. */ function commerce_product_bundles_update_8202() { $entity_type_manager = \Drupal::entityTypeManager(); $definition_update_manager = \Drupal::entityDefinitionUpdateManager(); // Set App UI string storage schema. $commerce_product_bundles = $entity_type_manager->getDefinition('commerce_product_bundles') ->setFormClass('duplicate', 'Drupal\commerce_product_bundles\Form\ProductBundleForm'); $definition_update_manager->updateEntityType($commerce_product_bundles); }