Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
C
commerce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Custom Issue Tracker
Custom Issue Tracker
Labels
Merge Requests
20
Merge Requests
20
Requirements
Requirements
List
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Analytics
Analytics
Code Review
Insights
Issue
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
project
commerce
Commits
60f7da0c
Commit
60f7da0c
authored
Oct 30, 2020
by
mglaman
Committed by
mglaman
Oct 30, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue
#3178599
by mglaman, jsacksick: Provide a computed field for accessing the default variation
parent
b7d91c89
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
41 additions
and
0 deletions
+41
-0
modules/product/src/Entity/Product.php
modules/product/src/Entity/Product.php
+16
-0
modules/product/src/Plugin/Field/ComputedDefaultVariation.php
...les/product/src/Plugin/Field/ComputedDefaultVariation.php
+24
-0
modules/product/tests/src/Kernel/Entity/ProductTest.php
modules/product/tests/src/Kernel/Entity/ProductTest.php
+1
-0
No files found.
modules/product/src/Entity/Product.php
View file @
60f7da0c
...
...
@@ -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
;
...
...
modules/product/src/Plugin/Field/ComputedDefaultVariation.php
0 → 100644
View file @
60f7da0c
<?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
());
}
}
modules/product/tests/src/Kernel/Entity/ProductTest.php
View file @
60f7da0c
...
...
@@ -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.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment