You are here

class UcQuantity in Ubercart 8.4

Provides a form element for Ubercart quantity input.

Plugin annotation

@FormElement("uc_quantity");

Hierarchy

  • class \Drupal\uc_store\Element\UcQuantity extends \Element\FormElement

Expanded class hierarchy of UcQuantity

7 #type uses of UcQuantity
AddToCartForm::buildForm in uc_product/src/Form/AddToCartForm.php
Form constructor.
hook_uc_cart_display in uc_cart/uc_cart.api.php
Controls the display of an item in the cart.
Products::addProductForm in uc_order/src/Plugin/Ubercart/OrderPane/Products.php
Sets the quantity and attributes of a product added to the order.
Products::editProductsForm in uc_order/src/Plugin/Ubercart/OrderPane/Products.php
Form to allow ordered products' data to be changed.
uc_product_kit_form_node_form_alter in uc_product_kit/uc_product_kit.module
Implements hook_form_BASE_FORM_ID_alter() for node_form().

... See full list

File

uc_store/src/Element/UcQuantity.php, line 13

Namespace

Drupal\uc_store\Element
View source
class UcQuantity extends Element\FormElement {

  /**
   * {@inheritdoc}
   */
  public function getInfo() {
    $class = get_class($this);
    return [
      '#input' => TRUE,
      '#size' => 5,
      '#maxlength' => 6,
      '#process' => [
        [
          $class,
          'processAjaxForm',
        ],
      ],
      '#element_validate' => [
        [
          $class,
          'validateQuantity',
        ],
      ],
      '#pre_render' => [
        [
          $class,
          'preRenderQuantity',
        ],
      ],
      '#theme' => 'input__textfield',
      '#theme_wrappers' => [
        'form_element',
      ],
      '#allow_zero' => FALSE,
    ];
  }

  /**
   * Form element validation handler for #type 'uc_quantity'.
   *
   * Note that #required is validated by _form_validate() already.
   */
  public static function validateQuantity(&$element, FormStateInterface $form_state, &$complete_form) {
    if (!preg_match('/^\\d+$/', $element['#value'])) {
      $form_state
        ->setError($element, t('The quantity must be an integer.'));
    }
    elseif (empty($element['#allow_zero']) && !$element['#value']) {
      $form_state
        ->setError($element, t('The quantity cannot be zero.'));
    }
  }

  /**
   * Prepares a #type 'uc_quantity' render element for theme_input().
   *
   * @param array $element
   *   An associative array containing the properties of the element.
   *   Properties used: #title, #value, #description, #size, #maxlength,
   *   #placeholder, #min, #max, #step, #required, #attributes.
   *
   * @return array
   *   The $element with prepared variables ready for theme_input().
   */
  public static function preRenderQuantity(array $element) {
    $element['#attributes']['type'] = 'number';
    $element['#attributes']['min'] = 0;
    $element['#attributes']['step'] = 1;
    Element::setAttributes($element, [
      'id',
      'name',
      'value',
      'size',
      'maxlength',
      'placeholder',
      'min',
      'max',
      'step',
    ]);
    static::setAttributes($element, [
      'form-uc-quantity',
    ]);
    return $element;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
UcQuantity::getInfo public function
UcQuantity::preRenderQuantity public static function Prepares a #type 'uc_quantity' render element for theme_input().
UcQuantity::validateQuantity public static function Form element validation handler for #type 'uc_quantity'.