1. 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();
    

    .

  2. 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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

caseylau created an issue. See original summary.

lawxen’s picture

Issue summary: View changes
lawxen’s picture

Issue summary: View changes
lawxen’s picture

Status: Active » Needs review
FileSize
6.87 KB
lawxen’s picture

Update comment

lawxen’s picture

alan_blake’s picture

Status: Needs review » Reviewed & tested by the community

I test the patch on my local environment, things goes well.

  • caseylau committed f82fac2 on 8.x-2.x
    Issue #2979250 by caseylau, alan_blake: The logic of deleting...
lawxen’s picture

Status: Reviewed & tested by the community » Fixed

@alan_blake Very thanks for the review

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.