Skip to content

Commit deba412

Browse files
berdirbojanz
authored andcommitted
Issue #2870672 by bojanz, vasike, Berdir: Product should implement EntityPublishedInterface
1 parent 2ba01d1 commit deba412

8 files changed

Lines changed: 40 additions & 57 deletions

File tree

modules/cart/tests/src/Functional/MultipleCartMultipleVariationTypesTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class MultipleCartMultipleVariationTypesTest extends CartBrowserTestBase {
4343
protected function setUp() {
4444
parent::setUp();
4545
// Unpublish parent test product.
46-
$this->variation->getProduct()->setPublished(FALSE);
46+
$this->variation->getProduct()->setUnpublished();
4747
$this->variation->getProduct()->save();
4848

4949
// Create three variation types.

modules/cart/tests/src/FunctionalJavascript/MultipleCartFormsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ protected function setUp() {
5050
$this->maximumMetaRefreshCount = 0;
5151

5252
// Delete parent test product.
53-
$this->variation->getProduct()->setPublished(FALSE);
53+
$this->variation->getProduct()->setUnpublished();
5454
$this->variation->getProduct()->save();
5555

5656
/** @var \Drupal\Core\Entity\Entity\EntityFormDisplay $order_item_form_display */

modules/cart/tests/src/FunctionalJavascript/MultipleCartMultipleVariationTypesTest.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class MultipleCartMultipleVariationTypesTest extends CartBrowserTestBase {
4848
protected function setUp() {
4949
parent::setUp();
5050
// Unpublish parent test product.
51-
$this->variation->getProduct()->setPublished(FALSE);
51+
$this->variation->getProduct()->setUnpublished();
5252
$this->variation->getProduct()->save();
5353

5454
// Create three variation types.
@@ -83,12 +83,16 @@ protected function setUp() {
8383
$this->sizeAttributes[$key] = $this->createAttributeValue($size_attribute->id(), $value);
8484
}
8585

86-
// The error seems to occur when variation with one attribute is first.
87-
// So remove title sort. Otherwise if first product has both attributes,
88-
// all seems to be fine.
86+
// The error seems to occur when the variation with one attribute is first.
87+
// So replace the title sort with a product_id one. Otherwise if the first
88+
// product has both attributes, all seems to be fine.
8989
$view = View::load('test_multiple_cart_forms');
9090
$display =& $view->getDisplay('default');
91-
$display['display_options']['sorts'] = [];
91+
$display['display_options']['sorts']['product_id'] = $display['display_options']['sorts']['title'];
92+
$display['display_options']['sorts']['product_id']['id'] = 'product_id';
93+
$display['display_options']['sorts']['product_id']['field'] = 'product_id';
94+
$display['display_options']['sorts']['product_id']['entity_field'] = 'product_id';
95+
unset($display['display_options']['sorts']['title']);
9296
$view->save();
9397

9498
// Create products.

modules/product/commerce_product.install

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,16 @@ function commerce_product_update_8201() {
1919
}
2020
}
2121
}
22+
23+
/**
24+
* Set the 'published' entity key.
25+
*/
26+
function commerce_product_update_8202() {
27+
$definition_update_manager = \Drupal::entityDefinitionUpdateManager();
28+
$entity_type = $definition_update_manager->getEntityType('commerce_product');
29+
$keys = $entity_type->getKeys();
30+
$keys['published'] = 'status';
31+
unset($keys['status']);
32+
$entity_type->set('entity_keys', $keys);
33+
$definition_update_manager->updateEntityType($entity_type);
34+
}

modules/product/src/Entity/Product.php

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
namespace Drupal\commerce_product\Entity;
44

5-
use Drupal\user\UserInterface;
5+
use Drupal\Core\Entity\EntityPublishedTrait;
66
use Drupal\Core\Entity\ContentEntityBase;
77
use Drupal\Core\Entity\EntityChangedTrait;
88
use Drupal\Core\Entity\EntityStorageInterface;
99
use Drupal\Core\Entity\EntityTypeInterface;
1010
use Drupal\Core\Field\BaseFieldDefinition;
11+
use Drupal\user\UserInterface;
1112

1213
/**
1314
* Defines the product entity class.
@@ -55,7 +56,7 @@
5556
* "label" = "title",
5657
* "langcode" = "langcode",
5758
* "uuid" = "uuid",
58-
* "status" = "status",
59+
* "published" = "status",
5960
* },
6061
* links = {
6162
* "canonical" = "/product/{commerce_product}",
@@ -73,6 +74,7 @@
7374
class Product extends ContentEntityBase implements ProductInterface {
7475

7576
use EntityChangedTrait;
77+
use EntityPublishedTrait;
7678

7779
/**
7880
* {@inheritdoc}
@@ -89,21 +91,6 @@ public function setTitle($title) {
8991
return $this;
9092
}
9193

92-
/**
93-
* {@inheritdoc}
94-
*/
95-
public function isPublished() {
96-
return (bool) $this->getEntityKey('status');
97-
}
98-
99-
/**
100-
* {@inheritdoc}
101-
*/
102-
public function setPublished($published) {
103-
$this->set('status', (bool) $published);
104-
return $this;
105-
}
106-
10794
/**
10895
* {@inheritdoc}
10996
*/
@@ -347,6 +334,7 @@ public static function postDelete(EntityStorageInterface $storage, array $entiti
347334
*/
348335
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
349336
$fields = parent::baseFieldDefinitions($entity_type);
337+
$fields += static::publishedBaseFieldDefinitions($entity_type);
350338

351339
$fields['uid'] = BaseFieldDefinition::create('entity_reference')
352340
->setLabel(t('Author'))
@@ -393,11 +381,14 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
393381
->setDisplayConfigurable('form', TRUE)
394382
->setComputed(TRUE);
395383

396-
$fields['status'] = BaseFieldDefinition::create('boolean')
397-
->setLabel(t('Published'))
398-
->setDescription(t('Whether the product is published.'))
399-
->setDefaultValue(TRUE)
400-
->setTranslatable(TRUE)
384+
$fields['status']
385+
->setDisplayOptions('form', [
386+
'type' => 'boolean_checkbox',
387+
'settings' => [
388+
'display_label' => TRUE,
389+
],
390+
'weight' => 90,
391+
])
401392
->setDisplayConfigurable('form', TRUE);
402393

403394
$fields['created'] = BaseFieldDefinition::create('created')

modules/product/src/Entity/ProductInterface.php

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
use Drupal\commerce_store\Entity\EntityStoresInterface;
66
use Drupal\Core\Entity\ContentEntityInterface;
77
use Drupal\Core\Entity\EntityChangedInterface;
8+
use Drupal\Core\Entity\EntityPublishedInterface;
89
use Drupal\user\EntityOwnerInterface;
910

1011
/**
1112
* Defines the interface for products.
1213
*/
13-
interface ProductInterface extends ContentEntityInterface, EntityChangedInterface, EntityOwnerInterface, EntityStoresInterface {
14+
interface ProductInterface extends ContentEntityInterface, EntityChangedInterface, EntityOwnerInterface, EntityPublishedInterface, EntityStoresInterface {
1415

1516
/**
1617
* Gets the product title.
@@ -30,26 +31,6 @@ public function getTitle();
3031
*/
3132
public function setTitle($title);
3233

33-
/**
34-
* Get whether the product is published.
35-
*
36-
* Unpublished products are only visible to their authors and administrators.
37-
*
38-
* @return bool
39-
* TRUE if the product is published, FALSE otherwise.
40-
*/
41-
public function isPublished();
42-
43-
/**
44-
* Sets whether the product is published.
45-
*
46-
* @param bool $published
47-
* Whether the product is published.
48-
*
49-
* @return $this
50-
*/
51-
public function setPublished($published);
52-
5334
/**
5435
* Gets the product creation timestamp.
5536
*

modules/product/src/Plugin/Action/UnpublishProduct.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class UnpublishProduct extends ActionBase {
2121
*/
2222
public function execute($entity = NULL) {
2323
/** @var \Drupal\commerce_product\Entity\ProductInterface $entity */
24-
$entity->setPublished(FALSE);
24+
$entity->setUnpublished();
2525
$entity->save();
2626
}
2727

modules/product/tests/src/Kernel/Entity/ProductTest.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ protected function setUp() {
4949
/**
5050
* @covers ::getTitle
5151
* @covers ::setTitle
52-
* @covers ::isPublished
53-
* @covers ::setPublished
5452
* @covers ::getCreatedTime
5553
* @covers ::setCreatedTime
5654
* @covers ::getStores
@@ -71,10 +69,6 @@ public function testProduct() {
7169
$product->setTitle('My title');
7270
$this->assertEquals('My title', $product->getTitle());
7371

74-
$this->assertEquals(TRUE, $product->isPublished());
75-
$product->setPublished(FALSE);
76-
$this->assertEquals(FALSE, $product->isPublished());
77-
7872
$product->setCreatedTime(635879700);
7973
$this->assertEquals(635879700, $product->getCreatedTime());
8074

0 commit comments

Comments
 (0)