public function preprocess(&$vars) { parent::preprocess($vars); $vars['selected'] = []; if (isset($vars['attrs']['multiple']) && $vars['attrs']['multiple'] && isset($vars['value']) && is_array($vars['value'])) { foreach ($vars['value'] as $selected) { $vars['selected'][$selected] = TRUE; } } elseif (isset($vars['value']) && !is_array($vars['value'])) { $vars['selected'][$vars['value']] = TRUE; } $vars['select_attrs'] = NULL; $tag_attributes = []; foreach ($vars['attrs'] as $attr => $val) { $tag_attributes[] = sprintf('%s = "%s"', $attr, $val); } $vars['select_attrs'] = implode(' ', $tag_attributes); }
public function getSelectField($renderApi, $unit, $fieldId, $postRequestValue = null) { $inputName = strlen($renderApi->getFormValue($unit, 'inputName')) > 0 ? $renderApi->getFormValue($unit, 'inputName') : $fieldId; $formField = new \SelectField(); $formField->getElementProperties()->addAttribute("name", $inputName); $formField->getElementProperties()->addAttribute("id", $fieldId); $listOptions = $this->listOptions->getListOptions($renderApi, $unit); if ($listOptions->hasOptions()) { foreach ($listOptions->getOptions() as $option) { /* @var $option \Option */ $optionField = new \OptionField(); $optionField->setContent($option->getName()); $elementProperties = $optionField->getElementProperties(); $elementProperties->addAttribute("value", $option->getValue()); if (is_null($postRequestValue) && $option->isChecked() || $postRequestValue === $option->getValue()) { $elementProperties->addAttribute("selected", null); } $formField->add($optionField); } } if ($this->formSubmit->isValid($renderApi, $unit) && !$this->isValidValue($unit, $postRequestValue)) { $formField->getElementProperties()->addClass('vf__error'); } return $formField; }
public static function clearCountCache() { self::$count_cache = array(); }
/** * Add fields for editing a Variation in the CMS popup. * * @return FieldSet */ public function getCMSFields_forPopup() { $fields = $this->getCMSFields(array('includeRelations' => false)); $fields->removeByName('Image'); $fields->removeByName('StockLevelID'); $fields->removeByName('Version'); $fields->addFieldToTab("Root", new Tab('Advanced')); $product = $this->Product(); $attributes = $product->Attributes(); if ($attributes && $attributes->exists()) { foreach ($attributes as $attribute) { $options = DataObject::get('Option', "ProductID = {$product->ID} AND AttributeID = {$attribute->ID}"); $currentOptionID = ($currentOption = $this->Options()->find('AttributeID', $attribute->ID)) ? $currentOption->ID : null; $optionField = new OptionField($attribute->ID, $attribute->Title, $options, $currentOptionID); $optionField->setHasEmptyDefault(false); $fields->addFieldToTab('Root.Main', $optionField); } } //Stock level field $level = $this->StockLevel()->Level; $fields->addFieldToTab('Root.Main', new StockField('Stock', null, $level, $this)); $fields->addFieldToTab('Root.Advanced', new DropdownField('Status', 'Status (you can disable a variation to prevent it being sold)', $this->dbObject('Status')->enumValues())); $amountField = new VariationMoneyField('Amount', 'Amount that this variation will increase the base product price by'); $amountField->setAllowedCurrencies(Product::$allowed_currency); $fields->addFieldToTab('Root.Advanced', $amountField); return $fields; }
/** * Add fields for editing a Variation in the CMS popup. * * @return FieldList */ public function getCMSFields() { $fields = new FieldList($rootTab = new TabSet('Root', $tabMain = new Tab('Variation'))); $product = $this->Product(); $attributes = $product->Attributes(); if ($attributes && $attributes->exists()) { foreach ($attributes as $attribute) { $options = $attribute->Options(); $currentOptionID = ($currentOption = $this->Options()->find('AttributeID', $attribute->ID)) ? $currentOption->ID : null; $optionField = new OptionField($attribute->ID, $attribute->Title, $options, $currentOptionID); $optionField->setHasEmptyDefault(false); $fields->addFieldToTab('Root.Variation', $optionField); } } $fields->addFieldToTab('Root.Variation', PriceField::create('Price', 'Price')->setRightTitle('Amount that this variation will increase the base product price by')); $fields->addFieldToTab('Root.Variation', DropdownField::create('Status', 'Status', $this->dbObject('Status')->enumValues())->setRightTitle('You can disable a variation to prevent it being sold')); $this->extend('updateCMSFields', $fields); return $fields; }