Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
-
The logic of deleting price_list_item in pricelist's postdelete is wrong
class PriceListItemStorage extends CommerceContentEntityStorage implements PriceListItemStorageInterface { public function loadMultipleByPriceList($price_list_id) { $entity_query = $this->getQuery(); $entity_query->condition('price_list_id', $price_list_id); $entity_query->sort('weight'); $result = $entity_query->execute(); return $result ? $this->loadMultiple($result) : []; } } class PriceList extends CommerceContentEntityBase implements PriceListInterface { public function getItems() { // TODO: Implement getItems() method. $storage = $this->entityTypeManager()->getStorage('price_list_item'); return $storage->loadMultipleByPriceList($this->id()); } public static function postDelete(EntityStorageInterface $storage, array $entities) { // Delete the price list item of a deleted price list. foreach ($entities as $entity) { $price_list_items = $entity->getItems(); if (empty($price_list_items)) { continue; } $price_list_item_storage = \Drupal::service('entity_type.manager') ->getStorage('price_list_item'); $price_list_item_storage->delete($price_list_items); } } }
The logic of getting the needed deleted price_list_item in loadMultipleByPriceList() is wrong,
The right way of getting one price_list's price_list_items is directly from EntityReference, not writing complicated filtering logic like$entity_query = $this->getQuery(); $entity_query->condition('price_list_id', $price_list_id); $entity_query->sort('weight'); $result = $entity_query->execute();
.
-
The value of price_list_item's price_list_id field should be generated in entity level through postSave(), not form level. because "form level" will cause price_list_id be NULL when the price_list_item was added to price_list by api of code
$pricelist->get('field_price_list_item')->appendItem($priceListItem);
The above two problem caused one bug: When delete a pricelist, it's price_list_item can't be deleted when price_list_id is NULL.
Comment | File | Size | Author |
---|---|---|---|
#6 | interdiff-2979250-5-6.txt | 440 bytes | lawxen |
#6 | 2979250-6.patch | 6.9 KB | lawxen |
| |||
#5 | interdiff-2979250-4-5.txt | 552 bytes | lawxen |
#5 | 2979250-5.patch | 6.87 KB | lawxen |
| |||
#4 | 2979250-4.patch | 6.87 KB | lawxen |
|
Comments
Comment #2
lawxen CreditAttribution: lawxen at Sparkpad commentedComment #3
lawxen CreditAttribution: lawxen at Sparkpad commentedComment #4
lawxen CreditAttribution: lawxen at Sparkpad commentedComment #5
lawxen CreditAttribution: lawxen at Sparkpad commentedUpdate comment
Comment #6
lawxen CreditAttribution: lawxen at Sparkpad commentedComment #7
alan_blake CreditAttribution: alan_blake at Sparkpad commentedI test the patch on my local environment, things goes well.
Comment #9
lawxen CreditAttribution: lawxen at Sparkpad commented@alan_blake Very thanks for the review