Commit 60f7da0c authored by mglaman's avatar mglaman Committed by mglaman

Issue #3178599 by mglaman, jsacksick: Provide a computed field for accessing the default variation

parent b7d91c89
......@@ -6,6 +6,7 @@ use Drupal\commerce\Entity\CommerceContentEntityBase;
use Drupal\commerce\EntityOwnerTrait;
use Drupal\commerce_product\Event\ProductDefaultVariationEvent;
use Drupal\commerce_product\Event\ProductEvents;
use Drupal\commerce_product\Plugin\Field\ComputedDefaultVariation;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\EntityPublishedTrait;
use Drupal\Core\Entity\EntityChangedTrait;
......@@ -378,6 +379,17 @@ class Product extends CommerceContentEntityBase implements ProductInterface {
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['default_variation'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Default variation'))
->setDescription(t('The default variation.'))
->setSetting('target_type', 'commerce_product_variation')
->setSetting('handler', 'default')
->setComputed(TRUE)
->setCardinality(1)
->setClass(ComputedDefaultVariation::class)
->setDisplayConfigurable('form', FALSE)
->setDisplayConfigurable('view', FALSE);
$fields['path'] = BaseFieldDefinition::create('path')
->setLabel(t('URL alias'))
->setDescription(t('The product URL alias.'))
......@@ -426,6 +438,7 @@ class Product extends CommerceContentEntityBase implements ProductInterface {
/** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
$fields = [];
$fields['variations'] = clone $base_field_definitions['variations'];
$fields['default_variation'] = clone $base_field_definitions['default_variation'];
/** @var \Drupal\commerce_product\Entity\ProductTypeInterface $product_type */
$product_type = ProductType::load($bundle);
if ($product_type) {
......@@ -434,6 +447,9 @@ class Product extends CommerceContentEntityBase implements ProductInterface {
$fields['variations']->setSetting('handler_settings', [
'target_bundles' => [$variation_type_id => $variation_type_id],
]);
$fields['default_variation']->setSetting('handler_settings', [
'target_bundles' => [$variation_type_id => $variation_type_id],
]);
}
return $fields;
......
<?php
namespace Drupal\commerce_product\Plugin\Field;
use Drupal\commerce_product\Entity\ProductInterface;
use Drupal\Core\Field\EntityReferenceFieldItemList;
use Drupal\Core\TypedData\ComputedItemListTrait;
/**
* Computed field item list for the default_variation field.
*/
class ComputedDefaultVariation extends EntityReferenceFieldItemList {
use ComputedItemListTrait;
/**
* {@inheritdoc}
*/
protected function computeValue() {
$product = $this->getEntity();
assert($product instanceof ProductInterface);
$this->list[0] = $this->createItem(0, $product->getDefaultVariation());
}
}
......@@ -163,6 +163,7 @@ class ProductTest extends CommerceKernelTestBase {
$this->assertNotEmpty($product->hasVariation($variation1));
$this->assertEquals($product->getDefaultVariation(), $variation2);
$this->assertEquals($variation2, $product->get('default_variation')->entity);
$this->assertNotEquals($product->getDefaultVariation(), $variation1);
// Confirm that postSave() sets the product_id on each variation.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment