You are here

function uc_attribute_subject_save in Ubercart 8.4

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

Saves a product/class attribute.

Parameters

&$attribute: The product/class attribute.

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

string $id: The product/class ID.

bool $save_options: Save the product/class attribute's options, too?

Return value

\Drupal\Core\Database\StatementInterface|null A prepared statement, or NULL if the query is not valid.

11 calls to uc_attribute_subject_save()
AttributeTest::testAttributeAddToCart in uc_attribute/tests/src/Functional/AttributeTest.php
Tests that product in cart has the selected attribute option.
AttributeTest::testAttributeApi in uc_attribute/tests/src/Functional/AttributeTest.php
Tests the basic attribute API.
AttributeTest::testAttributeUiClassAttributeOptionOverview in uc_attribute/tests/src/Functional/AttributeTest.php
Tests the product class attribute option user interface.
AttributeTest::testAttributeUiClassAttributeOverview in uc_attribute/tests/src/Functional/AttributeTest.php
Tests the product class attribute user interface.
AttributeTest::testAttributeUiProductAdjustments in uc_attribute/tests/src/Functional/AttributeTest.php
Tests the "product adjustments" page.

... See full list

File

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

Code

function uc_attribute_subject_save(&$attribute, $type, $id, $save_options = FALSE) {
  $sql = uc_attribute_type_info($type);

  // First, save the options. First because if this is an insert, we'll set
  // a default option for the product/class attribute.
  if ($save_options && is_array($attribute->options)) {
    foreach ($attribute->options as $option) {

      // Sanity check!
      $option = (object) $option;
      uc_attribute_subject_option_save($option, $type, $id);
    }

    // Is this an insert? If so, we'll set the default option.
    if (!uc_attribute_subject_exists($attribute->aid, $type, $id)) {
      $default_option = 0;

      // Make the first option (if any) the default.
      if (!empty($attribute->options) && is_array($attribute->options)) {
        $option = (object) reset($attribute->options);
        $default_option = $option->oid;
      }
      $attribute->default_option = $default_option;
    }
  }

  // Ensure the attribute's ID property is set.
  $attribute->{$sql['id']} = $id;
  $connection = \Drupal::database();
  return $connection
    ->merge($sql['attr_table'])
    ->keys([
    $sql['id'] => $id,
    'aid' => $attribute->aid,
  ])
    ->fields([
    'label' => $attribute->label,
    'ordering' => $attribute->ordering,
    'default_option' => isset($attribute->default_option) ? $attribute->default_option : 0,
    'required' => (int) $attribute->required,
    'display' => $attribute->display,
  ])
    ->execute();
}