function uc_attribute_load in Ubercart 8.4
Same name and namespace in other branches
- 5 uc_attribute/uc_attribute.module \uc_attribute_load()
- 6.2 uc_attribute/uc_attribute.module \uc_attribute_load()
- 7.3 uc_attribute/uc_attribute.module \uc_attribute_load()
Loads an attribute from the database.
Parameters
int $aid: The ID of the attribute.
int $id: The ID of the product/class this attribute belongs to.
string $type: The type of attribute: 'product' or 'class'. Any other type will fetch a base attribute.
Return value
object|false The attribute object, or FALSE if it doesn't exist.
19 calls to uc_attribute_load()
- AttributeDeleteForm::buildForm in uc_attribute/
src/ Form/ AttributeDeleteForm.php - Form constructor.
- AttributeEditForm::buildForm in uc_attribute/
src/ Form/ AttributeEditForm.php - Form constructor.
- AttributeOptionsForm::buildForm in uc_attribute/
src/ Form/ AttributeOptionsForm.php - Form constructor.
- 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.
File
- uc_attribute/
uc_attribute.module, line 507 - Ubercart Attribute module.
Code
function uc_attribute_load($aid, $id = NULL, $type = '') {
$sql = uc_attribute_type_info($type);
$connection = \Drupal::database();
switch ($type) {
case 'product':
case 'class':
// Read attribute data.
$query = $connection
->select('uc_attributes', 'a')
->fields('a', [
'aid',
'name',
'description',
])
->condition('a.aid', $aid);
$query
->leftJoin($sql['attr_table'], 'pa', "a.aid = pa.aid AND pa.{$sql['id']} = :id", [
':id' => $id,
]);
$query
->fields('pa', [
'label',
'default_option',
'required',
'ordering',
'display',
$sql['id'],
]);
$query
->addField('a', 'label', 'default_label');
$query
->addField('a', 'ordering', 'default_ordering');
$query
->addField('a', 'required', 'default_required');
$query
->addField('a', 'display', 'default_display');
$attribute = $query
->execute()
->fetchObject();
// Don't try to build it further if it failed already.
if (!$attribute) {
return FALSE;
}
// Set any missing defaults.
foreach ([
'ordering',
'required',
'display',
'label',
] as $field) {
if (isset($attribute->{"default_{$field}"}) && is_null($attribute->{$field})) {
$attribute->{$field} = $attribute->{"default_{$field}"};
}
}
if (empty($attribute->label)) {
$attribute->label = $attribute->name;
}
// Read option data.
$query = $connection
->select($sql['opt_table'], 'po')
->fields('po', [
$sql['id'],
'oid',
'cost',
'price',
'weight',
'ordering',
]);
$query
->leftJoin('uc_attribute_options', 'ao', "po.oid = ao.oid AND po.{$sql['id']} = :id", [
':id' => $id,
]);
$query
->fields('ao', [
'name',
'aid',
])
->condition('aid', $aid)
->orderBy('po.ordering')
->orderBy('ao.name');
$result = $query
->execute();
break;
default:
// Read attribute and option data.
$attribute = $connection
->query("SELECT * FROM {uc_attributes} WHERE aid = :aid", [
':aid' => $aid,
])
->fetchObject();
$result = $connection
->query("SELECT * FROM {uc_attribute_options} WHERE aid = :aid ORDER BY ordering, name", [
':aid' => $aid,
]);
// Don't try to build it further if it failed already.
if (!$attribute) {
return FALSE;
}
break;
}
// Got an attribute?
if ($attribute) {
// Get its options, too.
$attribute->options = [];
foreach ($result as $option) {
$attribute->options[$option->oid] = $option;
}
}
return $attribute;
}