/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { $form['name'] = array('#type' => 'textfield', '#title' => $this->t('Name'), '#description' => $this->t('The name of the attribute used in administrative forms'), '#default_value' => '', '#required' => TRUE); $form['label'] = array('#type' => 'textfield', '#title' => $this->t('Label'), '#description' => $this->t("Enter a label that customers will see instead of the attribute name. Use <none> if you don't want a title to appear at all."), '#default_value' => ''); $form['description'] = array('#type' => 'textfield', '#title' => $this->t('Help text'), '#description' => $this->t('<b>Optional.</b> Enter the help text that will display beneath the attribute on product add to cart forms.'), '#default_value' => '', '#maxlength' => 255); $form['required'] = array('#type' => 'checkbox', '#title' => $this->t('Make this attribute required, forcing the customer to choose an option.'), '#description' => $this->t('Selecting this for an attribute will disregard any default option you specify.<br />May be overridden at the product level.'), '#default_value' => 0); $form['display'] = array('#type' => 'select', '#title' => $this->t('Display type'), '#description' => $this->t('This specifies how the options for this attribute will be presented.<br />May be overridden at the product level.'), '#options' => _uc_attribute_display_types(), '#default_value' => 1); $form['ordering'] = array('#type' => 'weight', '#delta' => 25, '#title' => $this->t('List position'), '#description' => $this->t('Multiple attributes on an add to cart form are sorted by this value and then by their name.<br />May be overridden at the product level.'), '#default_value' => 0); $form['actions'] = array('#type' => 'actions'); $form['actions']['submit'] = array('#type' => 'submit', '#value' => $this->t('Submit'), '#suffix' => Link::createFromRoute($this->t('Cancel'), 'uc_attribute.overview')->toString()); return $form; }
/** * Displays a paged list and overview of existing product attributes. */ public function overview() { $header = array(array('data' => $this->t('Name'), 'field' => 'a.name', 'sort' => 'asc'), array('data' => $this->t('Label'), 'field' => 'a.label'), $this->t('Required'), array('data' => $this->t('List position'), 'field' => 'a.ordering'), $this->t('Number of options'), $this->t('Display type'), array('data' => $this->t('Operations'), 'colspan' => 1)); $display_types = _uc_attribute_display_types(); $query = db_select('uc_attributes', 'a')->extend('Drupal\\Core\\Database\\Query\\PagerSelectExtender')->extend('Drupal\\Core\\Database\\Query\\TableSortExtender')->fields('a', array('aid', 'name', 'label', 'required', 'ordering', 'display'))->orderByHeader($header)->limit(30); $build['attributes'] = array('#type' => 'table', '#header' => $header, '#empty' => $this->t('No product attributes have been added yet.')); $result = $query->execute(); foreach ($result as $attr) { $attr->options = db_query('SELECT COUNT(*) FROM {uc_attribute_options} WHERE aid = :aid', [':aid' => $attr->aid])->fetchField(); if (empty($attr->label)) { $attr->label = $attr->name; } $build['attributes'][] = array('name' => array('#markup' => $attr->name), 'label' => array('#markup' => $attr->label), 'required' => array('#markup' => $attr->required == 1 ? $this->t('Yes') : $this->t('No')), 'ordering' => array('#markup' => $attr->ordering), 'options' => array('#markup' => $attr->options), 'display' => array('#markup' => $display_types[$attr->display]), 'operations' => array('#type' => 'operations', '#links' => array('edit' => array('title' => $this->t('Edit'), 'url' => Url::fromRoute('uc_attribute.edit', ['aid' => $attr->aid])), 'options' => array('title' => $this->t('Options'), 'url' => Url::fromRoute('uc_attribute.options', ['aid' => $attr->aid])), 'delete' => array('title' => $this->t('Delete'), 'url' => Url::fromRoute('uc_attribute.delete', ['aid' => $attr->aid]))))); } $build['pager'] = array('#type' => 'pager'); return $build; }
/** * Displays a paged list and overview of existing product attributes. */ public function overview() { $header = array(array('data' => t('Name'), 'field' => 'a.name', 'sort' => 'asc'), array('data' => t('Label'), 'field' => 'a.label'), t('Required'), array('data' => t('List position'), 'field' => 'a.ordering'), t('Number of options'), t('Display type'), array('data' => t('Operations'), 'colspan' => 3)); $display_types = _uc_attribute_display_types(); $query = db_select('uc_attributes', 'a')->extend('Drupal\\Core\\Database\\Query\\PagerSelectExtender')->extend('Drupal\\Core\\Database\\Query\\TableSortExtender')->fields('a', array('aid', 'name', 'label', 'required', 'ordering', 'display'))->orderByHeader($header)->limit(30); $rows = array(); $result = $query->execute(); foreach ($result as $attr) { $attr->options = db_query('SELECT COUNT(*) FROM {uc_attribute_options} WHERE aid = :aid', [':aid' => $attr->aid])->fetchField(); if (empty($attr->label)) { $attr->label = $attr->name; } $rows[] = array(SafeMarkup::checkPlain($attr->name), SafeMarkup::checkPlain($attr->label), $attr->required == 1 ? t('Yes') : t('No'), $attr->ordering, $attr->options, $display_types[$attr->display], \Drupal::l(t('edit'), new Url('uc_attribute.edit', ['aid' => $attr->aid])), \Drupal::l(t('options'), new Url('uc_attribute.options', ['aid' => $attr->aid])), \Drupal::l(t('delete'), new Url('uc_attribute.delete', ['aid' => $attr->aid]))); } $build['attributes'] = array('#theme' => 'table', '#header' => $header, '#rows' => $rows, '#empty' => t('No product attributes have been added yet.')); $build['pager'] = array('#type' => 'pager'); return $build; }
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state, $attributes = NULL) { $form['attributes'] = array('#type' => 'table', '#header' => array($this->t('Remove'), $this->t('Name'), $this->t('Label'), $this->t('Default'), $this->t('Required'), $this->t('List position'), $this->t('Display')), '#empty' => $this->t('No attributes available.'), '#tabledrag' => array(array('action' => 'order', 'relationship' => 'sibling', 'group' => 'uc-attribute-table-ordering'))); foreach ($attributes as $aid => $attribute) { $option = isset($attribute->options[$attribute->default_option]) ? $attribute->options[$attribute->default_option] : NULL; $form['attributes'][$aid]['#attributes']['class'][] = 'draggable'; $form['attributes'][$aid]['remove'] = array('#type' => 'checkbox', '#title' => $this->t('Remove'), '#title_display' => 'invisible'); $form['attributes'][$aid]['name'] = array('#markup' => $attribute->name); $form['attributes'][$aid]['label'] = array('#type' => 'textfield', '#title' => $this->t('Label'), '#title_display' => 'invisible', '#default_value' => empty($attribute->label) ? $attribute->name : $attribute->label, '#size' => 20); $form['attributes'][$aid]['option'] = array('#markup' => $option ? $option->name . ' (' . uc_currency_format($option->price) . ')' : $this->t('n/a')); $form['attributes'][$aid]['required'] = array('#type' => 'checkbox', '#title' => $this->t('Required'), '#title_display' => 'invisible', '#default_value' => $attribute->required); $form['attributes'][$aid]['ordering'] = array('#type' => 'weight', '#title' => $this->t('List position'), '#title_display' => 'invisible', '#default_value' => $attribute->ordering, '#attributes' => array('class' => array('uc-attribute-table-ordering'))); $form['attributes'][$aid]['display'] = array('#type' => 'select', '#title' => $this->t('Display'), '#title_display' => 'invisible', '#default_value' => $attribute->display, '#options' => _uc_attribute_display_types()); } $form['actions'] = array('#type' => 'actions'); $form['actions']['save'] = array('#type' => 'submit', '#value' => $this->t('Save changes')); return $form; }
/** * Tests the "add attribute" user interface. */ public function testAttributeUIAddAttribute() { $this->drupalGet('admin/store/products/attributes/add'); $this->assertText(t('The name of the attribute used in administrative forms'), 'Attribute add form working.'); $edit = (array) $this->createAttribute(array(), FALSE); $this->drupalPostForm('admin/store/products/attributes/add', $edit, t('Submit')); if ($edit['display'] != 0) { // We redirect to add options page ONLY for non-textfield attributes. $this->assertText('Options for ' . $edit['name']); $this->assertText('No options for this attribute have been added yet.'); } else { // For textfield attributes we redirect to attribute list. $this->assertText($edit['name'], 'Attribute name created'); $this->assertText($edit['label'], 'Attribute label created'); } $this->drupalGet('admin/store/products/attributes'); $this->assertRaw('<td>' . $edit['name'] . '</td>', 'Verify name field.'); $this->assertRaw('<td>' . $edit['label'] . '</td>', 'Verify label field.'); $this->assertRaw('<td>' . ($edit['required'] ? t('Yes') : t('No')) . '</td>', 'Verify required field.'); $this->assertRaw('<td>' . $edit['ordering'] . '</td>', 'Verify ordering field.'); $types = _uc_attribute_display_types(); $this->assertRaw('<td>' . $types[$edit['display']] . '</td>', 'Verify display field.'); $aid = db_query('SELECT aid FROM {uc_attributes} WHERE name = :name', [':name' => $edit['name']])->fetchField(); $this->assertTrue($aid, 'Attribute was created.'); $attribute = uc_attribute_load($aid); $fields_ok = TRUE; foreach ($edit as $field => $value) { if ($attribute->{$field} != $value) { $this->showVar($attribute); $this->showVar($edit); $fields_ok = FALSE; break; } } $this->assertTrue($fields_ok, 'Attribute created properly.'); }