commerce_product_bundle.module in Commerce Product Bundle 8
Same filename and directory in other branches
Contains commerce_product_bundle.module.
File
commerce_product_bundle.moduleView source
<?php
/**
* @file
* Contains commerce_product_bundle.module.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Routing\CurrentRouteMatch;
use Drupal\entity\BundleFieldDefinition;
use Drupal\commerce_product_bundle\Entity\BundleTypeInterface;
use Drupal\commerce_product_bundle\Entity\BundleItemTypeInterface;
/**
* Implements hook_help().
*/
function commerce_product_bundle_help($route_name, CurrentRouteMatch $route_match) {
switch ($route_name) {
// Main module help for the commerce_product_bundle module.
case 'help.page.commerce_product_bundle':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Extends Drupal Commerce with a product bundle functionality.') . '</p>';
return $output;
}
}
/**
* Implements hook_theme().
*/
function commerce_product_bundle_theme() {
return [
'commerce_product_bundle_form' => [
'render element' => 'form',
],
'commerce_product_bundle' => [
'render element' => 'elements',
],
'commerce_product_bundle_i' => [
'render element' => 'elements',
],
'container__field__widget_commerce_product_bundle_items' => [
'render element' => 'elements',
],
];
}
/**
* Implements hook_theme_suggestions_HOOK().
*/
function commerce_product_bundle_theme_suggestions_commerce_product_bundle(array $variables) {
return _commerce_entity_theme_suggestions('commerce_product_bundle', $variables);
}
/**
* Implements hook_theme_suggestions_HOOK().
*/
function commerce_product_bundle_theme_suggestions_commerce_product_bundle_i(array $variables) {
return _commerce_entity_theme_suggestions('commerce_product_bundle_i', $variables);
}
/**
* Prepares variables for product bundle templates.
*
* Default template: commerce-product-bundle.html.twig.
*
* @param array $variables
* An associative array containing:
* - elements: An associative array containing rendered fields.
* - attributes: HTML attributes for the containing element.
*/
function template_preprocess_commerce_product_bundle(array &$variables) {
/** @var Drupal\commerce_product_bundle\Entity\BundleInterface $bundle */
$product_bundle = $variables['elements']['#commerce_product_bundle'];
$variables['product_bundle_entity'] = $product_bundle;
$variables['product_bundle_url'] = $product_bundle
->toUrl();
$variables['product_bundle'] = [];
foreach (Element::children($variables['elements']) as $key) {
$variables['product_bundle'][$key] = $variables['elements'][$key];
}
}
/**
* Prepares variables for product bundle item templates.
*
* Default template: commerce-product-bundle-i.html.twig.
*
* @param array $variables
* An associative array containing:
* - elements: An associative array containing rendered fields.
* - attributes: HTML attributes for the containing element.
*/
function template_preprocess_commerce_product_bundle_i(array &$variables) {
/** @var Drupal\commerce_product_bundle\Entity\BundleItemInterface $product_bundle_item */
$product_bundle_item = $variables['elements']['#commerce_product_bundle_i'];
$variables['product_bundle_item_entity'] = $product_bundle_item;
$variables['product_bundle_item'] = [];
foreach (Element::children($variables['elements']) as $key) {
$variables['product_bundle_item'][$key] = $variables['elements'][$key];
}
}
/**
* Implements hook_form_alter().
*/
function commerce_product_bundle_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// Add theme wrapper for the product bundle items add to cart field widget.
if (strpos($form_id, 'add_to_cart') !== FALSE && substr($form_id, 0, 43) == 'commerce_order_item_commerce_product_bundle') {
$form['purchased_entity']['#theme_wrappers'] = [
'container__field__widget_commerce_product_bundle_items',
];
}
}
/**
* Adds the default body field to a product bundle type.
*
* @param \Drupal\commerce_product_bundle\Entity\BundleTypeInterface $product_bundle_type
* The product bundle type.
* @param string $label
* (optional) The label for the body instance. Defaults to 'Body'.
*/
function commerce_product_bundle_add_body_field(BundleTypeInterface $product_bundle_type, $label = 'Body') {
$field_definition = BundleFieldDefinition::create('text_with_summary')
->setTargetEntityTypeId('commerce_product_bundle')
->setTargetBundle($product_bundle_type
->id())
->setName('body')
->setLabel($label)
->setSetting('display_summary', FALSE)
->setDisplayOptions('form', [
'type' => 'text_textarea_with_summary',
'weight' => 1,
])
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'text_default',
]);
$configurable_field_manager = \Drupal::service('commerce.configurable_field_manager');
$configurable_field_manager
->createField($field_definition, FALSE);
}
/**
* Adds the default variations field to a bundle item type.
*
* Variations can't be a base field because the Views integration is broken.
* Instead, it is created as a configurable field for each bundle item type.
*
* @param \Drupal\commerce_product_bundle\Entity\BundleTypeInterface $bundle_type
* The bundle item type the field should be attached to.
*
* @todo Consider how this may change.
* @see https://www.drupal.org/node/2837499
*/
function commerce_product_bundle_add_items_field(BundleTypeInterface $bundle_type) {
$field_definition = BundleFieldDefinition::create('entity_reference')
->setTargetEntityTypeId('commerce_product_bundle')
->setTargetBundle($bundle_type
->id())
->setName('bundle_items')
->setLabel('Bundle items')
->setCardinality(BundleFieldDefinition::CARDINALITY_UNLIMITED)
->setRequired(FALSE)
->setTranslatable(FALSE)
->setSetting('target_type', 'commerce_product_bundle_i')
->setSetting('handler', 'default')
->setDisplayOptions('form', [
'type' => 'entity_reference_autocomplete',
'weight' => 10,
'settings' => [
'override_labels' => TRUE,
'label_singular' => 'bundle item',
'label_plural' => 'bundle items',
'match_operator' => 'CONTAINS',
'size' => '60',
'placeholder' => '',
'match_limit' => 10,
],
])
->setDisplayOptions('view', [
'type' => 'commerce_add_to_cart',
'weight' => 10,
]);
$configurable_field_manager = \Drupal::service('commerce.configurable_field_manager');
$configurable_field_manager
->createField($field_definition);
}
/**
* Adds the default variations field to a bundle item type.
*
* Variations can't be a base field because the Views integration is broken.
* Instead, it is created as a configurable field for each bundle item type.
*
* @param \Drupal\commerce_product_bundle\Entity\BundleItemTypeInterface $bundle_item_type
* The bundle item type the field should be attached to.
*
* @todo Consider how this may change.
* @see https://www.drupal.org/node/2837499
*/
function commerce_product_bundle_add_variations_field(BundleItemTypeInterface $bundle_item_type) {
$field_definition = BundleFieldDefinition::create('entity_reference')
->setTargetEntityTypeId('commerce_product_bundle_i')
->setTargetBundle($bundle_item_type
->id())
->setName('variations')
->setLabel('Variations')
->setCardinality(BundleFieldDefinition::CARDINALITY_UNLIMITED)
->setRequired(FALSE)
->setTranslatable(FALSE)
->setSetting('target_type', 'commerce_product_variation')
->setSetting('handler', 'default')
->setDisplayOptions('form', [
'type' => 'options_buttons',
'weight' => 10,
])
->setDisplayOptions('view', [
'type' => 'commerce_add_to_cart',
'weight' => 10,
]);
$configurable_field_manager = \Drupal::service('commerce.configurable_field_manager');
$configurable_field_manager
->createField($field_definition);
}
/**
* Adds the default stores field to a product bundle.
*
* A product bundle can belong to multiple stores.
* Store id can't be a base field because the Views integration is broken.
* Instead, it is created as a configurable field for each order type.
*
* @param \Drupal\commerce_product_bundle\Entity\BundleTypeInterface $product_bundle_type
* The product bundle type.
*/
function commerce_product_bundle_add_stores_field(BundleTypeInterface $product_bundle_type) {
$field_definition = BundleFieldDefinition::create('entity_reference')
->setTargetEntityTypeId('commerce_product_bundle')
->setTargetBundle($product_bundle_type
->id())
->setName('stores')
->setLabel('Stores')
->setCardinality(BundleFieldDefinition::CARDINALITY_UNLIMITED)
->setRequired(FALSE)
->setTranslatable(FALSE)
->setSetting('target_type', 'commerce_store')
->setSetting('handler', 'default')
->setDisplayOptions('form', [
'type' => 'commerce_entity_select',
'weight' => -10,
]);
$configurable_field_manager = \Drupal::service('commerce.configurable_field_manager');
$configurable_field_manager
->createField($field_definition);
}
Functions
Name | Description |
---|---|
commerce_product_bundle_add_body_field | Adds the default body field to a product bundle type. |
commerce_product_bundle_add_items_field | Adds the default variations field to a bundle item type. |
commerce_product_bundle_add_stores_field | Adds the default stores field to a product bundle. |
commerce_product_bundle_add_variations_field | Adds the default variations field to a bundle item type. |
commerce_product_bundle_form_alter | Implements hook_form_alter(). |
commerce_product_bundle_help | Implements hook_help(). |
commerce_product_bundle_theme | Implements hook_theme(). |
commerce_product_bundle_theme_suggestions_commerce_product_bundle | Implements hook_theme_suggestions_HOOK(). |
commerce_product_bundle_theme_suggestions_commerce_product_bundle_i | Implements hook_theme_suggestions_HOOK(). |
template_preprocess_commerce_product_bundle | Prepares variables for product bundle templates. |
template_preprocess_commerce_product_bundle_i | Prepares variables for product bundle item templates. |