AttributeDeleteForm.php in Ubercart 8.4
File
uc_attribute/src/Form/AttributeDeleteForm.php
View source
<?php
namespace Drupal\uc_attribute\Form;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
class AttributeDeleteForm extends ConfirmFormBase {
protected $attribute;
public function getQuestion() {
return $this
->t('Are you sure you want to delete the attribute %name?', [
'%name' => $this->attribute->name,
]);
}
public function getDescription() {
$count = \Drupal::database()
->query("SELECT COUNT(*) FROM {uc_product_attributes} WHERE aid = :aid", [
':aid' => $this->attribute->aid,
])
->fetchField();
return $this
->formatPlural($count, 'There is 1 product with this attribute.', 'There are @count products with this attribute.');
}
public function getConfirmText() {
return $this
->t('Delete');
}
public function getCancelUrl() {
return Url::fromRoute('uc_attribute.overview');
}
public function getFormId() {
return 'uc_attribute_delete_form';
}
public function buildForm(array $form, FormStateInterface $form_state, $aid = NULL) {
$this->attribute = uc_attribute_load($aid);
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$options = array_keys($this->attribute->options);
if ($options) {
\Drupal::database()
->delete('uc_class_attribute_options')
->condition('oid', $options, 'IN')
->execute();
\Drupal::database()
->delete('uc_product_options')
->condition('oid', $options, 'IN')
->execute();
}
if ($nodes = \Drupal::database()
->query("SELECT nid FROM {uc_product_attributes} WHERE aid = :aid", [
':aid' => $this->attribute->aid,
])
->fetchCol()) {
\Drupal::database()
->delete('uc_product_adjustments')
->condition('nid', $nodes, 'IN')
->execute();
}
\Drupal::database()
->delete('uc_class_attributes')
->condition('aid', $this->attribute->aid)
->execute();
\Drupal::database()
->delete('uc_product_attributes')
->condition('aid', $this->attribute->aid)
->execute();
\Drupal::database()
->delete('uc_attribute_options')
->condition('aid', $this->attribute->aid)
->execute();
\Drupal::database()
->delete('uc_attributes')
->condition('aid', $this->attribute->aid)
->execute();
$this
->messenger()
->addMessage($this
->t('Product attribute deleted.'));
$form_state
->setRedirect('uc_attribute.overview');
}
}