Skip to content

Commit 9897285

Browse files
committed
Issue #2884849: UsageLimitWidget should not load usage for unsaved entities
1 parent 2fd5fd7 commit 9897285

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

modules/promotion/src/Plugin/Field/FieldWidget/UsageLimitWidget.php

+9-6
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,16 @@ public static function create(ContainerInterface $container, array $configuratio
7373
*/
7474
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
7575
$value = isset($items[$delta]->value) ? $items[$delta]->value : NULL;
76-
$entity = $items[$delta]->getEntity();
7776
$usage = 0;
78-
if ($entity instanceof PromotionInterface) {
79-
$usage = $this->usage->getUsage($entity);
80-
}
81-
elseif ($entity instanceof CouponInterface) {
82-
$usage = $this->usage->getUsage($entity->getPromotion(), $entity);
77+
/** @var \Drupal\Core\Entity\EntityInterface $entity */
78+
$entity = $items[$delta]->getEntity();
79+
if (!$entity->isNew()) {
80+
if ($entity instanceof PromotionInterface) {
81+
$usage = $this->usage->getUsage($entity);
82+
}
83+
elseif ($entity instanceof CouponInterface) {
84+
$usage = $this->usage->getUsage($entity->getPromotion(), $entity);
85+
}
8386
}
8487
$formatted_usage = $this->formatPlural($usage, '1 use', '@count uses');
8588
$radio_parents = array_merge($form['#parents'], [$this->fieldDefinition->getName(), 0, 'limit']);

0 commit comments

Comments
 (0)