public function TestController::productKit in Dropdown Attributes 8
1 string reference to 'TestController::productKit'
- uc_dropdown_test.routing.yml in uc_dropdown_test/
uc_dropdown_test.routing.yml - uc_dropdown_test/uc_dropdown_test.routing.yml
File
- uc_dropdown_test/
src/ Controller/ TestController.php, line 274
Class
- TestController
- Additional test of Dropdown Attributes UI.
Namespace
Drupal\uc_dropdown_test\ControllerCode
public function productKit($user, $type) {
$node1 = $this
->createProduct();
// Add a product class.
$class = $this
->randGen(12);
$values = array(
'type' => strtolower($class),
);
$product_class = new NodeType($values, 'node_type');
$product_class
->set('entityTypeId', 'node_type');
$product_class
->set('name', $class);
$product_class
->setThirdPartySetting('uc_product', 'product', TRUE);
$product_class->original = new NodeType($values, 'node_type');
$product_class
->save();
$node2 = $this
->createProduct(array(
'type' => $product_class
->id(),
));
$kit = $this
->createProductKit(array(
$node1,
$node2,
));
switch ($type) {
default:
case 'select':
$display = 1;
break;
case 'radios':
$display = 2;
break;
case 'checkboxes':
$display = 3;
break;
}
$data = array(
'display' => $display,
'name' => 'parent',
'label' => 'Parent',
'required' => TRUE,
);
$parent_attribute = $this
->createAttribute($data);
$data = array(
'display' => $display,
'name' => 'child',
'label' => 'Child',
);
$child_attribute = $this
->createAttribute($data);
if ($type == 'textfield') {
// Textfields are only supported as children.
$display = 0;
}
$data = array(
'display' => $display,
'name' => 'grandchild',
'label' => 'Grandchild',
);
$grandchild_attribute = $this
->createAttribute($data);
// Add some options.
$parent_options = array();
$options1 = array();
for ($i = 0; $i < 3; $i++) {
$option = $this
->createAttributeOption(array(
'aid' => $parent_attribute->aid,
));
$parent_options[$option->oid] = $option;
if ($i == 0) {
$show_child = $option->oid;
}
if ($i < 2) {
$options1[$option->oid] = $option->oid;
}
if ($i == 0) {
$oid = $option->oid;
}
}
$child_options = array();
$options2 = array();
for ($i = 0; $i < 3; $i++) {
$option = $this
->createAttributeOption(array(
'aid' => $child_attribute->aid,
));
$child_options[$option->oid] = $option;
if ($i < 2) {
$options2[$option->oid] = $option->oid;
}
}
if ($type != 'textfield') {
$grandchild_options = array();
for ($i = 0; $i < 3; $i++) {
$option = $this
->createAttributeOption(array(
'aid' => $grandchild_attribute->aid,
));
$grandchild_options[$option->oid] = $option;
}
}
// Attach the attributes to a product.
uc_attribute_subject_save($parent_attribute, 'product', $node1
->id());
uc_attribute_subject_save($child_attribute, 'product', $node1
->id());
uc_attribute_subject_save($grandchild_attribute, 'product', $node1
->id());
foreach ($parent_options as $parent_option) {
$this
->attachProductOption($parent_option->oid, $node1
->id());
}
foreach ($child_options as $child_option) {
$this
->attachProductOption($child_option->oid, $node1
->id());
}
foreach ($grandchild_options as $grandchild_option) {
$this
->attachProductOption($grandchild_option->oid, $node1
->id());
}
// Create dependent attribute.
uc_dropdown_attributes_product_create_dependency($node1
->id(), $child_attribute->aid, $parent_attribute->aid, $options1, 1);
uc_dropdown_attributes_product_create_dependency($node1
->id(), $grandchild_attribute->aid, $child_attribute->aid, $options2, 1);
switch ($type) {
default:
case 'select':
$display = 1;
break;
case 'radios':
$display = 2;
break;
case 'checkboxes':
$display = 3;
break;
}
$data = array(
'display' => $display,
'name' => 'classparent',
'label' => 'Class parent',
'required' => TRUE,
);
$class_parent_attribute = $this
->createAttribute($data);
$data = array(
'display' => $display,
'name' => 'classchild',
'label' => 'Class child',
);
$class_child_attribute = $this
->createAttribute($data);
if ($type == 'textfield') {
// Textfields are only supported as children.
$display = 0;
}
$data = array(
'display' => $display,
'name' => 'classgrandchild',
'label' => 'Class grandchild',
);
$class_grandchild_attribute = $this
->createAttribute($data);
// Add some options.
$class_parent_options = array();
$class_options1 = array();
for ($i = 0; $i < 3; $i++) {
$option = $this
->createAttributeOption(array(
'aid' => $class_parent_attribute->aid,
));
$class_parent_options[$option->oid] = $option;
if ($i == 0) {
$show_child = $option->oid;
}
if ($i < 2) {
$class_options1[$option->oid] = $option->oid;
}
if ($i == 0) {
$oid = $option->oid;
}
}
$class_child_options = array();
$class_options2 = array();
for ($i = 0; $i < 3; $i++) {
$option = $this
->createAttributeOption(array(
'aid' => $class_child_attribute->aid,
));
$class_child_options[$option->oid] = $option;
if ($i < 2) {
$class_options2[$option->oid] = $option->oid;
}
}
if ($type != 'textfield') {
$class_grandchild_options = array();
for ($i = 0; $i < 3; $i++) {
$option = $this
->createAttributeOption(array(
'aid' => $class_grandchild_attribute->aid,
));
$class_grandchild_options[$option->oid] = $option;
}
}
// Attach the attributes to a product.
uc_attribute_subject_save($class_parent_attribute, 'class', $product_class
->id());
uc_attribute_subject_save($class_child_attribute, 'class', $product_class
->id());
uc_attribute_subject_save($class_grandchild_attribute, 'class', $product_class
->id());
foreach ($class_parent_options as $parent_option) {
$this
->attachClassOption($parent_option->oid, $product_class
->id());
}
foreach ($class_child_options as $child_option) {
$this
->attachClassOption($child_option->oid, $product_class
->id());
}
foreach ($class_grandchild_options as $grandchild_option) {
$this
->attachClassOption($grandchild_option->oid, $product_class
->id());
}
// Create dependent attribute.
uc_dropdown_attributes_class_create_dependency($product_class
->id(), $class_child_attribute->aid, $class_parent_attribute->aid, $class_options1, 1);
uc_dropdown_attributes_class_create_dependency($product_class
->id(), $class_grandchild_attribute->aid, $class_child_attribute->aid, $class_options2, 1);
$response = array();
$response['status'] = TRUE;
$response['user'] = $user;
$response['nid'] = $kit
->id();
return new JsonResponse($response);
}