You are here

function uc_attribute_subject_delete in Ubercart 8.4

Same name and namespace in other branches
  1. 6.2 uc_attribute/uc_attribute.module \uc_attribute_subject_delete()
  2. 7.3 uc_attribute/uc_attribute.module \uc_attribute_subject_delete()

Deletes an attribute and all options associated with it.

Parameters

int $aid: The base attribute ID.

string $type: The type of attribute: 'product' or 'class'. Any other type will fetch a base attribute.

int $id: The product/class ID.

Return value

int The Drupal SAVED_DELETED flag.

2 calls to uc_attribute_subject_delete()
AttributeTest::testAttributeApi in uc_attribute/tests/src/Functional/AttributeTest.php
Tests the basic attribute API.
uc_attribute_delete in uc_attribute/uc_attribute.module
Deletes an attribute from the database.

File

uc_attribute/uc_attribute.module, line 802
Ubercart Attribute module.

Code

function uc_attribute_subject_delete($aid, $type, $id = NULL) {
  $sql = uc_attribute_type_info($type);
  $connection = \Drupal::database();
  $query = $connection
    ->select('uc_attribute_options', 'a')
    ->fields('a', [
    'oid',
  ]);
  $query
    ->join($sql['opt_table'], 'subject', 'a.oid = subject.oid');

  // Base conditions, and an ID check if necessary.
  $conditions = new Condition('AND');
  $conditions
    ->condition('aid', $aid);
  if ($id) {
    $conditions
      ->condition($sql['id'], $id);
  }
  $query
    ->condition($conditions);
  $result = $query
    ->execute();
  while ($oid = $result
    ->fetchField()) {

    // Don't delete the adjustments one at a time. We'll do it in bulk soon for
    // efficiency.
    uc_attribute_subject_option_delete($oid, $type, $id, FALSE);
  }
  $connection
    ->delete($sql['attr_table'])
    ->condition($conditions)
    ->execute();

  // If this is a product attribute, wipe any associated adjustments.
  if ($type == 'product') {
    uc_attribute_adjustments_delete([
      'aid' => $aid,
      'nid' => $id,
    ]);
  }
  return SAVED_DELETED;
}