You are here

public function TestController::product in Dropdown Attributes 8

1 string reference to 'TestController::product'
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 20

Class

TestController
Additional test of Dropdown Attributes UI.

Namespace

Drupal\uc_dropdown_test\Controller

Code

public function product($user, $type) {
  $node = $this
    ->createProduct();
  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', $node
    ->id());
  uc_attribute_subject_save($child_attribute, 'product', $node
    ->id());
  uc_attribute_subject_save($grandchild_attribute, 'product', $node
    ->id());
  foreach ($parent_options as $parent_option) {
    $this
      ->attachProductOption($parent_option->oid, $node
      ->id());
  }
  foreach ($child_options as $child_option) {
    $this
      ->attachProductOption($child_option->oid, $node
      ->id());
  }
  foreach ($grandchild_options as $grandchild_option) {
    $this
      ->attachProductOption($grandchild_option->oid, $node
      ->id());
  }

  // Create dependent attribute.
  uc_dropdown_attributes_product_create_dependency($node
    ->id(), $child_attribute->aid, $parent_attribute->aid, $options1, 1);
  uc_dropdown_attributes_product_create_dependency($node
    ->id(), $grandchild_attribute->aid, $child_attribute->aid, $options2, 1);
  $response = array();
  $response['status'] = TRUE;
  $response['user'] = $user;
  $response['nid'] = $node
    ->id();
  return new JsonResponse($response);
}