diff --git a/src/Entity/PriceList.php b/src/Entity/PriceList.php index c4847da..76002fd 100644 --- a/src/Entity/PriceList.php +++ b/src/Entity/PriceList.php @@ -176,13 +176,38 @@ class PriceList extends CommerceContentEntityBase implements PriceListInterface return $this; } + /** + * {@inheritdoc} + */ + public function getItemsIds() { + $price_list_item_ids = []; + foreach ($this->get('field_price_list_item') as $field_item) { + $price_list_item_ids[] = $field_item->target_id; + } + return $price_list_item_ids; + } + /** * {@inheritdoc} */ public function getItems() { - // TODO: Implement getItems() method. - $storage = $this->entityTypeManager()->getStorage('price_list_item'); - return $storage->loadMultipleByPriceList($this->id()); + return $this->getTranslatedReferencedEntities('field_price_list_item'); + } + + /** + * {@inheritdoc} + */ + public function postSave(EntityStorageInterface $storage, $update = TRUE) { + parent::postSave($storage, $update); + + // Ensure there's a back-reference on each product variation. + foreach ($this->field_price_list_item as $item) { + $price_list_item = $item->entity; + if ($price_list_item->price_list_id->isEmpty()) { + $price_list_item->price_list_id = $this->id(); + $price_list_item->save(); + } + } } /** @@ -191,10 +216,12 @@ class PriceList extends CommerceContentEntityBase implements PriceListInterface 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)) { + if (empty($entity->field_price_list_item)) { continue; } + foreach ($entity->field_price_list_item as $item) { + $price_list_items[$item->target_id] = $item->entity; + } $price_list_item_storage = \Drupal::service('entity_type.manager') ->getStorage('price_list_item'); diff --git a/src/Entity/PriceListItem.php b/src/Entity/PriceListItem.php index c14ea04..ad10043 100644 --- a/src/Entity/PriceListItem.php +++ b/src/Entity/PriceListItem.php @@ -26,7 +26,6 @@ use Drupal\user\UserInterface; * ), * bundle_label = @Translation("price list item type"), * handlers = { - * "storage" = "Drupal\commerce_pricelist\PriceListItemStorage", * "access" = "Drupal\commerce\EmbeddedEntityAccessControlHandler", * "view_builder" = "Drupal\Core\Entity\EntityViewBuilder", * "views_data" = "Drupal\views\EntityViewsData", diff --git a/src/Form/PriceListForm.php b/src/Form/PriceListForm.php index 00b0de2..8669ba2 100644 --- a/src/Form/PriceListForm.php +++ b/src/Form/PriceListForm.php @@ -190,7 +190,6 @@ class PriceListForm extends ContentEntityForm { foreach ($entity->field_price_list_item as $item) { $itemEntity = $item->get('entity')->getTarget()->getValue(); $itemEntity->setWeight($item->getValue()['weight']); - $itemEntity->setPriceListId($this->entity->id()); $itemEntity->save(); } diff --git a/src/Form/PriceListItemInlineForm.php b/src/Form/PriceListItemInlineForm.php index 9d05cb8..3bb253a 100644 --- a/src/Form/PriceListItemInlineForm.php +++ b/src/Form/PriceListItemInlineForm.php @@ -80,11 +80,6 @@ class PriceListItemInlineForm extends EntityInlineForm { */ public function getTableFields($bundles) { $fields = parent::getTableFields($bundles); - $fields['price_list_id'] = [ - 'type' => 'field', - 'label' => t('Price List'), - 'weight' => 2, - ]; $fields['price'] = [ 'type' => 'field', 'label' => t('Price'), @@ -96,15 +91,6 @@ class PriceListItemInlineForm extends EntityInlineForm { 'weight' => 4, ]; - $routeName = $this->routeMatch->getRouteName(); - switch ($routeName) { - case 'entity.price_list.add_page':unset($fields['price_list_id']); - break; - - case 'entity.price_list.edit_form':unset($fields['price_list_id']); - break; - } - return $fields; } @@ -120,32 +106,6 @@ class PriceListItemInlineForm extends EntityInlineForm { ]; $entity_form = $this->priceForm($entity_form, $form_state); $entity_form['price']['#attributes']['id'] = 'purchased_entity_refresh'; - $routeName = $this->routeMatch->getRouteName(); - switch ($routeName) { - case 'entity.price_list.add_page':unset($entity_form['price_list_id']); - break; - - case 'entity.price_list.edit_form':unset($entity_form['price_list_id']); - break; - - case 'entity.commerce_product.add_form':unset($entity_form['purchased_entity']); - break; - - case 'entity.commerce_product.edit_form':unset($entity_form['purchased_entity']); - break; - - case 'entity.commerce_product_bundle.add_form':unset($entity_form['purchased_entity']); - break; - - case 'entity.commerce_product_bundle.edit_form':unset($entity_form['purchased_entity']); - break; - - case 'entity.commerce_product_variation.add_form':unset($entity_form['purchased_entity']); - break; - - case 'entity.commerce_product_variation.edit_form':unset($entity_form['purchased_entity']); - break; - } return $entity_form; } diff --git a/src/PriceListItemStorage.php b/src/PriceListItemStorage.php deleted file mode 100644 index 9e6eff2..0000000 --- a/src/PriceListItemStorage.php +++ /dev/null @@ -1,23 +0,0 @@ -getQuery(); - $entity_query->condition('price_list_id', $price_list_id); - $entity_query->sort('weight'); - $result = $entity_query->execute(); - return $result ? $this->loadMultiple($result) : []; - } - -} diff --git a/src/PriceListItemStorageInterface.php b/src/PriceListItemStorageInterface.php deleted file mode 100644 index 47e3624..0000000 --- a/src/PriceListItemStorageInterface.php +++ /dev/null @@ -1,23 +0,0 @@ -