/** * Get the element as HTML * * @return string */ public function getElementHtml() { $this->addClass('select multiselect'); $html = ''; if ($this->getCanBeEmpty()) { $html .= '<input type="hidden" name="' . parent::getName() . '" value="" />'; } $html .= '<select id="' . $this->getHtmlId() . '" name="' . $this->getName() . '" ' . $this->serialize($this->getHtmlAttributes()) . $this->_getUiId() . ' multiple="multiple">' . "\n"; $value = $this->getValue(); if (!is_array($value)) { $value = explode(',', $value); } $values = $this->getValues(); if ($values) { foreach ($values as $option) { if (is_array($option['value'])) { $html .= '<optgroup label="' . $option['label'] . '">' . "\n"; foreach ($option['value'] as $groupItem) { $html .= $this->_optionToHtml($groupItem, $value); } $html .= '</optgroup>' . "\n"; } else { $html .= $this->_optionToHtml($option, $value); } } } $html .= '</select>' . "\n"; $html .= $this->getAfterElementHtml(); return $html; }
/** * Output the region element and javasctipt that makes it dependent from country element * * @param \Magento\Framework\Data\Form\Element\AbstractElement $element * @return string * * @SuppressWarnings(PHPMD.UnusedLocalVariable) */ public function render(\Magento\Framework\Data\Form\Element\AbstractElement $element) { if ($country = $element->getForm()->getElement('country_id')) { $countryId = $country->getValue(); } else { return $element->getDefaultHtml(); } $regionId = $element->getForm()->getElement('region_id')->getValue(); $html = '<div class="field field-state required">'; $element->setClass('input-text'); $element->setRequired(true); $html .= $element->getLabelHtml() . '<div class="control">'; $html .= $element->getElementHtml(); $selectName = str_replace('region', 'region_id', $element->getName()); $selectId = $element->getHtmlId() . '_id'; $html .= '<select id="' . $selectId . '" name="' . $selectName . '" class="select required-entry" style="display:none">'; $html .= '<option value="">' . __('Please select') . '</option>'; $html .= '</select>'; $html .= '<script>' . "\n"; $html .= 'require(["prototype", "mage/adminhtml/form"], function(){'; $html .= '$("' . $selectId . '").setAttribute("defaultValue", "' . $regionId . '");' . "\n"; $html .= 'new regionUpdater("' . $country->getHtmlId() . '", "' . $element->getHtmlId() . '", "' . $selectId . '", ' . $this->_directoryHelper->getRegionJson() . ');' . "\n"; $html .= '});'; $html .= '</script>' . "\n"; $html .= '</div></div>' . "\n"; return $html; }
/** * @param AbstractElement $element * @return string * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function render(AbstractElement $element) { $countryId = false; $isRegionRequired = false; if ($country = $element->getForm()->getElement('country_id')) { $countryId = $country->getValue(); $isRegionRequired = $this->_directoryHelper->isRegionRequired($countryId); } $html = '<div class="field field-region ' . ($isRegionRequired ? 'required' : '') . '">' . "\n"; $regionCollection = false; if ($countryId) { if (!isset(self::$_regionCollections[$countryId])) { self::$_regionCollections[$countryId] = $this->_countryFactory->create()->setId($countryId)->getLoadedRegionCollection()->toOptionArray(); } $regionCollection = self::$_regionCollections[$countryId]; } $regionId = intval($element->getForm()->getElement('region_id')->getValue()); $htmlAttributes = $element->getHtmlAttributes(); foreach ($htmlAttributes as $key => $attribute) { if ('type' === $attribute) { unset($htmlAttributes[$key]); break; } } // Output two elements - for 'region' and for 'region_id'. // Two elements are needed later upon form post - to properly set data to address model, // otherwise old value can be left in region_id attribute and saved to DB. // Depending on country selected either 'region' (input text) or 'region_id' (selectbox) is visible to user $regionHtmlName = $element->getName(); $regionIdHtmlName = str_replace('region', 'region_id', $regionHtmlName); $regionHtmlId = $element->getHtmlId(); $regionIdHtmlId = str_replace('region', 'region_id', $regionHtmlId); if ($isRegionRequired) { $element->addClass('required-entry'); } if ($regionCollection && count($regionCollection) > 0) { $elementClass = $element->getClass(); $html .= '<label class="label" for="' . $regionIdHtmlId . '"><span>' . $element->getLabel() . '</span>' . '</label>'; $html .= '<div class="control">'; $html .= '<select id="' . $regionIdHtmlId . '" name="' . $regionIdHtmlName . '" ' . $element->serialize($htmlAttributes) . '>' . "\n"; foreach ($regionCollection as $region) { $selected = $regionId == $region['value'] ? ' selected="selected"' : ''; $regionVal = 0 == $region['value'] ? '' : (int) $region['value']; $html .= '<option value="' . $regionVal . '"' . $selected . '>' . $this->_escaper->escapeHtml(__($region['label'])) . '</option>'; } $html .= '</select>' . "\n"; $html .= '<input type="hidden" name="' . $regionHtmlName . '" id="' . $regionHtmlId . '" value=""/>'; $html .= '</div>'; $element->setClass($elementClass); } else { $html .= '<label class="label" for="' . $regionHtmlId . '"><span>' . $element->getLabel() . '</span></label>'; $html .= '<div class="control">'; $html .= '<input id="' . $regionHtmlId . '" name="' . $regionHtmlName . '" value="' . $element->getEscapedValue() . '" ' . $element->serialize($htmlAttributes) . "/>" . "\n"; $html .= '<input type="hidden" name="' . $regionIdHtmlName . '" id="' . $regionIdHtmlId . '" value=""/>'; $html .= '</div>' . "\n"; } $html .= '</div>' . "\n"; return $html; }
/** * @return mixed */ public function getName() { $name = parent::getName(); if (strpos($name, '[]') === false) { $name .= '[]'; } return $name; }
protected function _getElementHtml(\Magento\Framework\Data\Form\Element\AbstractElement $element) { $html = "<input class=' input-text' type='hidden' id='" . $element->getHtmlId() . "' name='" . $element->getName() . "' value='" . $element->getEscapedValue() . "' '" . $element->serialize($element->getHtmlAttributes()) . "/>"; $html .= "<table cellpadding='2' style='width:600px !important;width:auto;' class='wl-cron'>\n <thead> \n <tr><th>Days of the week</th><th width='20'></th><th colspan='4'>Hours of the day</th></tr>\n </thead>\n <tr>\n <td width='300' align='left'>\n <div>" . $this->checkbox("Monday", "d-Monday", "Monday", 'd') . "</div>\n <div>" . $this->checkbox("Tuesday", "d-Tuesday", "Tuesday", 'd') . "</div>\n <div>" . $this->checkbox("Wednesday", "d-Wednesday", "Wednesday", 'd') . "</div>\n <div>" . $this->checkbox("Thursday", "d-Thursday", "Thurday", 'd') . "</div>\n <div>" . $this->checkbox("Friday", "d-Friday", "Friday", 'd') . "</div>\n <div>" . $this->checkbox("Saturday", "d-Saturday", "Saturday", 'd') . "</div>\n <div>" . $this->checkbox("Sunday", "d-Sunday", "Sunday", 'd') . "</div>\n </td>\n <td width='175' class='morning-half'>\n <div>" . $this->checkbox("00:00", "h-0000", "00:00 AM", 'h') . "</div>\n <div>" . $this->checkbox("01:00", "h-0100", "01:00 AM", 'h') . "</div>\n <div>" . $this->checkbox("02:00", "h-0200", "02:00 AM", 'h') . "</div>\n <div>" . $this->checkbox("03:00", "h-0300", "03:00 AM", 'h') . "</div>\n <div>" . $this->checkbox("04:00", "h-0400", "04:00 AM", 'h') . "</div>\n <div>" . $this->checkbox("05:00", "h-0500", "05:00 AM", 'h') . "</div>\n <div>" . $this->checkbox("06:00", "h-0600", "06:00 AM", 'h') . "</div>\n <div>" . $this->checkbox("07:00", "h-0700", "07:00 AM", 'h') . "</div>\n <div>" . $this->checkbox("08:00", "h-0800", "08:00 AM", 'h') . "</div>\n <div>" . $this->checkbox("09:00", "h-0900", "09:00 AM", 'h') . "</div>\n <div>" . $this->checkbox("10:00", "h-1000", "10:00 AM", 'h') . "</div>\n <div>" . $this->checkbox("11:00", "h-1100", "11:00 AM", 'h') . "</div>\n </td>\n <td width='175' class='morning'>\n <div>" . $this->checkbox("00:30", "h-0030", "00:30 AM", 'h') . "</div>\n <div>" . $this->checkbox("01:30", "h-0130", "01:30 AM", 'h') . "</div>\n <div>" . $this->checkbox("02:30", "h-0230", "02:30 AM", 'h') . "</div>\n <div>" . $this->checkbox("03:30", "h-0330", "03:30 AM", 'h') . "</div>\n <div>" . $this->checkbox("04:30", "h-0430", "04:30 AM", 'h') . "</div>\n <div>" . $this->checkbox("05:30", "h-0530", "05:30 AM", 'h') . "</div>\n <div>" . $this->checkbox("06:30", "h-0630", "06:30 AM", 'h') . "</div>\n <div>" . $this->checkbox("07:30", "h-0730", "07:30 AM", 'h') . "</div>\n <div>" . $this->checkbox("08:30", "h-0830", "08:30 AM", 'h') . "</div>\n <div>" . $this->checkbox("09:30", "h-0930", "09:30 AM", 'h') . "</div>\n <div>" . $this->checkbox("10:30", "h-1030", "10:30 AM", 'h') . "</div>\n <div>" . $this->checkbox("11:30", "h-1130", "11:30 AM", 'h') . "</div>\n </td>\n <td width='175' class='afternoon-half'>\n <div>" . $this->checkbox("12:00", "h-1200", "12:00 AM", 'h') . "</div>\n <div>" . $this->checkbox("13:00", "h-1300", "01:00 PM", 'h') . "</div>\n <div>" . $this->checkbox("14:00", "h-1400", "02:00 PM", 'h') . "</div>\n <div>" . $this->checkbox("15:00", "h-1500", "03:00 PM", 'h') . "</div>\n <div>" . $this->checkbox("16:00", "h-1600", "04:00 PM", 'h') . "</div>\n <div>" . $this->checkbox("17:00", "h-1700", "05:00 PM", 'h') . "</div>\n <div>" . $this->checkbox("18:00", "h-1800", "06:00 PM", 'h') . "</div>\n <div>" . $this->checkbox("19:00", "h-1900", "07:00 PM", 'h') . "</div>\n <div>" . $this->checkbox("20:00", "h-2000", "08:00 PM", 'h') . "</div>\n <div>" . $this->checkbox("21:00", "h-2100", "09:00 PM", 'h') . "</div>\n <div>" . $this->checkbox("22:00", "h-2200", "10:00 PM", 'h') . "</div>\n <div>" . $this->checkbox("23:00", "h-2300", "11:00 PM", 'h') . "</div>\n </td>\n <td width='175' class='afternoon'>\n <div>" . $this->checkbox("12:30", "h-1230", "12:30 AM", 'h') . "</div>\n <div>" . $this->checkbox("13:30", "h-1330", "01:30 PM", 'h') . "</div>\n <div>" . $this->checkbox("14:30", "h-1430", "02:30 PM", 'h') . "</div>\n <div>" . $this->checkbox("15:30", "h-1530", "03:30 PM", 'h') . "</div>\n <div>" . $this->checkbox("16:30", "h-1630", "04:30 PM", 'h') . "</div>\n <div>" . $this->checkbox("17:30", "h-1730", "05:30 PM", 'h') . "</div>\n <div>" . $this->checkbox("18:30", "h-1830", "06:30 PM", 'h') . "</div>\n <div>" . $this->checkbox("19:30", "h-1930", "07:30 PM", 'h') . "</div>\n <div>" . $this->checkbox("20:30", "h-2030", "08:30 PM", 'h') . "</div>\n <div>" . $this->checkbox("21:30", "h-2130", "09:30 PM", 'h') . "</div>\n <div>" . $this->checkbox("22:30", "h-2230", "10:30 PM", 'h') . "</div>\n <div>" . $this->checkbox("23:30", "h-2330", "11:30 PM", 'h') . "</div>\n </td>\n </tr>\n </table>"; $html .= "<script>\nrequire([\n 'jquery',\n 'mage/mage'\n], function (\$) {\n \$(function () {\n jQuery(document).ready(function () {\n\n if (jQuery('#" . $element->getHtmlId() . "').length > 0) {\n\n if (jQuery('#" . $element->getHtmlId() . "').val() === '') {\n jQuery('#" . $element->getHtmlId() . "').val('{\"days\":[],\"hours\":[]}');\n }\n var cron = jQuery.parseJSON(jQuery('#" . $element->getHtmlId() . "').val());\n\n for (var i = 0; i < cron.days.length; i++) {\n if (jQuery('#d-' + cron.days[i])) {\n jQuery('#d-' + cron.days[i]).prop('checked', true);\n }\n }\n\n for (var i = 0; i < cron.hours.length; i++) {\n if (jQuery('#h-' + cron.hours[i].replace(':', ''))) {\n jQuery('#h-' + cron.hours[i].replace(':', '')).prop('checked', true);\n }\n }\n\n jQuery('.cron-box').on('click', function () {\n var d = new Array();\n jQuery('.cron-d-box').each(function () {\n if (jQuery(this).prop('checked')) {\n d.push(jQuery(this).val());\n }\n });\n var h = new Array();\n jQuery('.cron-h-box').each(function () {\n if (jQuery(this).prop('checked')) {\n h.push(jQuery(this).val());\n }\n });\n jQuery('#" . $element->getHtmlId() . "').val(Object.toJSON({days: d, hours: h}));\n });\n }\n });\n });\n});\n</script>"; $html .= $element->getAfterElementHtml(); return $html; }
/** * @covers \Magento\Framework\Data\Form\Element\AbstractElement::getName() */ public function testGetNameWithSuffix() { $returnValue = 'some_value'; $formMock = $this->getMock('Magento\\Framework\\Data\\Form\\AbstractForm', ['getFieldNameSuffix', 'addSuffixToName'], [], '', false); $formMock->expects($this->once())->method('getFieldNameSuffix')->will($this->returnValue(true)); $formMock->expects($this->once())->method('addSuffixToName')->will($this->returnValue($returnValue)); $this->_model->setForm($formMock); $this->assertEquals($returnValue, $this->_model->getName()); }
/** * Render inheritance checkbox (Use Default or Use Website) * * @param \Magento\Framework\Data\Form\Element\AbstractElement $element * @return string */ protected function _renderInheritCheckbox(\Magento\Framework\Data\Form\Element\AbstractElement $element) { $htmlId = $element->getHtmlId(); $namePrefix = preg_replace('#\\[value\\](\\[\\])?$#', '', $element->getName()); $checkedHtml = $element->getInherit() == 1 ? 'checked="checked"' : ''; $html = '<td class="use-default">'; $html .= '<input id="' . $htmlId . '_inherit" name="' . $namePrefix . '[inherit]" type="checkbox" value="1"' . ' class="checkbox config-inherit" ' . $checkedHtml . ' onclick="toggleValueElements(this, Element.previous(this.parentNode))" /> '; $html .= '<label for="' . $htmlId . '_inherit" class="inherit">' . $this->_getInheritCheckboxLabel($element) . '</label>'; $html .= '</td>'; return $html; }
/** * @param AbstractElement $element * @return string */ protected function _getElementHtml(AbstractElement $element) { $element->setStyle('width:70px;')->setName($element->getName() . '[]'); if ($element->getValue()) { $values = explode(',', $element->getValue()); } else { $values = []; } $from = $element->setValue(isset($values[0]) ? $values[0] : null)->getElementHtml(); $to = $element->setValue(isset($values[1]) ? $values[1] : null)->getElementHtml(); return __('<label class="label"><span>from</span></label>') . $from . __('<label class="label"><span>to</span></label>') . $to; }
/** * @param AbstractElement $element * @return string * @SuppressWarnings(PHPMD.NPathComplexity) */ protected function _getElementHtml(AbstractElement $element) { $_options = ['d' => __('Day'), 'm' => __('Month'), 'y' => __('Year')]; $element->setValues($_options)->setClass('select-date')->setName($element->getName() . '[]'); if ($element->getValue()) { $values = explode(',', $element->getValue()); } else { $values = []; } $_parts = []; $_parts[] = $element->setValue(isset($values[0]) ? $values[0] : null)->getElementHtml(); $_parts[] = $element->setValue(isset($values[1]) ? $values[1] : null)->getElementHtml(); $_parts[] = $element->setValue(isset($values[2]) ? $values[2] : null)->getElementHtml(); return implode(' <span>/</span> ', $_parts); }
protected function _getElementHtml(\Magento\Framework\Data\Form\Element\AbstractElement $element) { $html = ''; if ($element->getBeforeElementHtml() && $element->getBeforeElementHtml() != '') { $html .= '<label class="addbefore" for="' . $element->getHtmlId() . '">' . $element->getBeforeElementHtml() . '</label>'; } $html .= '<input id="' . $element->getHtmlId() . '" name="' . $element->getName() . '" ' . ' value="0" type="hidden"/>'; if ($element->getAfterElementJs() && $element->getAfterElementJs() != '') { $html .= $element->getAfterElementJs(); } if ($element->getAfterElementHtml() && $element->getAfterElementHtml() != '') { $html .= '<label class="addafter" for="' . $element->getHtmlId() . '">' . $element->getAfterElementHtml() . '</label>'; } return $html; }
/** * Render HTML of the element using the rule engine. * * @return string */ public function getInputHtml() { $this->rule->setElementName($this->element->getName()); if ($this->element->getValue()) { /* Hack : reload in a new instance to have element name set. * can not be done in afterLoad of the backend model * since we do not know yet the form structure */ $conditions = $this->element->getValue()->getConditions()->asArray(); $this->rule->getConditions()->loadArray($conditions); $this->element->setRule($this->rule); } $this->input = $this->elementFactory->create('text'); $this->input->setRule($this->rule)->setRenderer($this->conditions); return $this->input->toHtml(); }
/** * @param AbstractElement $element * @return string */ public function render(AbstractElement $element) { $html = '<div class="field field-' . $element->getHtmlId() . '">'; $html .= $element->getLabelHtml(); $html .= '<div class="control">' . $element->getElementHtml(); $html .= '<div class="nested">'; $html .= '<div class="field choice">'; $html .= '<label for="account-send-pass" class="addbefore"><span>' . __('or ') . '</span></label>'; $html .= '<input type="checkbox" id="account-send-pass" name="' . $element->getName() . '" value="auto" onclick="setElementDisable(\'' . $element->getHtmlId() . '\', this.checked)" />'; $html .= '<label class="label" for="account-send-pass"><span>' . __(' Send auto-generated password') . '</span></label>'; $html .= '</div>' . "\n"; $html .= '</div>' . "\n"; $html .= '</div>' . "\n"; $html .= '</div>' . "\n"; return $html; }
/** * @param AbstractElement $element * @return string * @SuppressWarnings(PHPMD.NPathComplexity) */ protected function _getElementHtml(AbstractElement $element) { $_months = []; for ($i = 1; $i <= 12; $i++) { $_months[$i] = $this->_localeDate->date(mktime(null, null, null, $i))->format('m'); } $_days = []; for ($i = 1; $i <= 31; $i++) { $_days[$i] = $i < 10 ? '0' . $i : $i; } if ($element->getValue()) { $values = explode(',', $element->getValue()); } else { $values = []; } $element->setName($element->getName() . '[]'); $_monthsHtml = $element->setStyle('width:100px;')->setValues($_months)->setValue(isset($values[0]) ? $values[0] : null)->getElementHtml(); $_daysHtml = $element->setStyle('width:50px;')->setValues($_days)->setValue(isset($values[1]) ? $values[1] : null)->getElementHtml(); return sprintf('%s %s', $_monthsHtml, $_daysHtml); }
/** * Instantiate form and fields * * @return \Magento\Framework\Data\Form */ protected function _prepareForm() { $form = $this->_formFactory->create(); $form->setFieldsetRenderer($this->getLayout()->createBlock('Magento\\Backend\\Block\\Widget\\Form\\Renderer\\Fieldset')); $form->setFieldsetElementRenderer($this->getLayout()->createBlock('Magento\\Catalog\\Block\\Adminhtml\\Form\\Renderer\\Fieldset\\Element')); /** * if there is a parent element defined, it will be replaced by a hidden element with the same name * and overridden by the form elements * It is needed to maintain HTML consistency of the parent element's form */ if ($this->_parentElement) { $form->setHtmlIdPrefix($this->_parentElement->getHtmlId())->setFieldNameSuffix($this->_parentElement->getName()); $form->addField('', 'hidden', array('name' => '')); } $noYes = array(__('No'), __('Yes')); // schedule $schedule = $form->addFieldset('schedule_fieldset', array('legend' => __('Schedule'), 'disabled' => $this->_isReadOnly)); $schedule->addField('start_date_is_editable', 'select', array('name' => 'start_date_is_editable', 'label' => __('Customer Can Define Start Date'), 'comment' => __('Select whether buyer can define the date when billing for the payment begins.'), 'options' => $noYes, 'disabled' => $this->_isReadOnly)); $this->_addField($schedule, 'schedule_description'); $this->_addField($schedule, 'suspension_threshold'); $this->_addField($schedule, 'bill_failed_later', array('options' => $noYes), 'select'); // billing $billing = $form->addFieldset('billing_fieldset', array('legend' => __('Billing'), 'disabled' => $this->_isReadOnly)); $this->_addField($billing, 'period_unit', array('options' => $this->_getPeriodUnitOptions(__('-- Please Select --'))), 'select'); $this->_addField($billing, 'period_frequency'); $this->_addField($billing, 'period_max_cycles'); // trial $trial = $form->addFieldset('trial_fieldset', array('legend' => __('Trial Period'), 'disabled' => $this->_isReadOnly)); $this->_addField($trial, 'trial_period_unit', array('options' => $this->_getPeriodUnitOptions(__('-- Not Selected --'))), 'select'); $this->_addField($trial, 'trial_period_frequency'); $this->_addField($trial, 'trial_period_max_cycles'); $this->_addField($trial, 'trial_billing_amount'); // initial fees $initial = $form->addFieldset('initial_fieldset', array('legend' => __('Initial Fees'), 'disabled' => $this->_isReadOnly)); $this->_addField($initial, 'init_amount'); $this->_addField($initial, 'init_may_fail', array('options' => $noYes), 'select'); return $form; }
/** * Render element * * @param \Magento\Framework\Data\Form\Element\AbstractElement $element * @return string * * @see RendererInterface::render() */ public function render(\Magento\Framework\Data\Form\Element\AbstractElement $element) { $element->addClass('element-value-changer'); $valueName = $element->getValueName(); if ($valueName === '') { $valueName = '...'; } if ($element->getShowAsText()) { $html = ' <input type="hidden" class="hidden" id="' . $element->getHtmlId() . '" name="' . $element->getName() . '" value="' . $element->getValue() . '"/> ' . htmlspecialchars($valueName) . ' '; } else { $html = ' <span class="rule-param"' . ($element->getParamId() ? ' id="' . $element->getParamId() . '"' : '') . '>' . '<a href="javascript:void(0)" class="label">'; if ($this->inlineTranslate->isAllowed()) { $html .= $this->escapeHtml($valueName); } else { $html .= $this->escapeHtml($this->filterManager->truncate($valueName, ['length' => 33, 'etc' => '...'])); } $html .= '</a><span class="element"> ' . $element->getElementHtml(); if ($element->getExplicitApply()) { $html .= ' <a href="javascript:void(0)" class="rule-param-apply"><img src="' . $this->getViewFileUrl('images/rule_component_apply.gif') . '" class="v-middle" alt="' . __('Apply') . '" title="' . __('Apply') . '" /></a> '; } $html .= '</span></span> '; } return $html; }
/** * @return mixed */ public function getParentName() { return parent::getName(); }
/** * Return html code of hidden element * * @return string */ protected function _getHiddenInput() { return '<input type="hidden" name="' . parent::getName() . '[value]" value="' . $this->getValue() . '" />'; }
/** * 2016-01-08 * @param AE $e * @param string $uidSuffix [optional] * @return array(string => string) */ function df_fe_attrs(AE $e, $uidSuffix = null) { return ['id' => $e->getHtmlId(), 'name' => $e->getName()] + df_fe_uid($e, $uidSuffix) + dfa_select($e->getData(), $e->getHtmlAttributes()); }