/** * Tests deleting a product type via a form. */ public function testProductTypeDeletion() { $variation_type = $this->createEntity('commerce_product_variation_type', ['id' => 'foo', 'label' => 'foo']); $product_type = $this->createEntity('commerce_product_type', ['id' => 'foo', 'label' => 'foo', 'variationType' => $variation_type->id()]); commerce_product_add_stores_field($product_type); commerce_product_add_variations_field($product_type); $product = $this->createEntity('commerce_product', ['type' => $product_type->id(), 'title' => $this->randomMachineName()]); // @todo Make sure $product_type->delete() also does nothing if there's // a product of that type. Right now the check is done on the form level. $this->drupalGet('admin/commerce/config/product-types/' . $product_type->id() . '/delete'); $this->assertRaw(t('%type is used by 1 product on your site. You can not remove this product type until you have removed all of the %type products.', ['%type' => $product_type->label()]), 'The product type will not be deleted until all products of that type are deleted.'); $this->assertNoText(t('This action cannot be undone.'), 'The product type deletion confirmation form is not available'); $product->delete(); $this->drupalGet('admin/commerce/config/product-types/' . $product_type->id() . '/delete'); $this->assertRaw(t('Are you sure you want to delete the product type %type?', ['%type' => $product_type->label()]), 'The product type is available for deletion'); $this->assertText(t('This action cannot be undone.'), 'The product type deletion confirmation form is available'); $this->drupalPostForm(NULL, NULL, t('Delete')); $exists = (bool) ProductType::load($product_type->id()); $this->assertFalse($exists, 'The new product type has been deleted from the database.'); }
/** * {@inheritdoc} */ public function save(array $form, FormStateInterface $form_state) { $status = $this->entity->save(); drupal_set_message($this->t('The product type %label has been successfully saved.', ['%label' => $this->entity->label()])); $form_state->setRedirect('entity.commerce_product_type.collection'); if ($status == SAVED_NEW) { commerce_product_add_stores_field($this->entity); commerce_product_add_body_field($this->entity); commerce_product_add_variations_field($this->entity); } }
/** * {@inheritdoc} */ public function save(array $form, FormStateInterface $form_state) { $status = $this->entity->save(); // Update the default value of the status field. $product = $this->entityTypeManager->getStorage('commerce_product')->create(['type' => $this->entity->id()]); $value = (bool) $form_state->getValue('product_status'); if ($product->status->value != $value) { $fields = $this->entityFieldManager->getFieldDefinitions('commerce_product', $this->entity->id()); $fields['status']->getConfig($this->entity->id())->setDefaultValue($value)->save(); $this->entityFieldManager->clearCachedFieldDefinitions(); } drupal_set_message($this->t('The product type %label has been successfully saved.', ['%label' => $this->entity->label()])); $form_state->setRedirect('entity.commerce_product_type.collection'); if ($status == SAVED_NEW) { commerce_product_add_stores_field($this->entity); commerce_product_add_body_field($this->entity); commerce_product_add_variations_field($this->entity); } }