Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
Chooser Class Reference
Inheritance diagram for Chooser:
Template Template AbstractBlock DataObject BlockInterface

Public Member Functions

 __construct (\Magento\Backend\Block\Template\Context $context, \Magento\Framework\Json\EncoderInterface $jsonEncoder, \Magento\Framework\Data\Form\Element\Factory $elementFactory, array $data=[])
 
 getSourceUrl ()
 
 getElement ()
 
 getConfig ()
 
 getUniqId ()
 
 getFieldsetId ()
 
 getHiddenEnabled ()
 
- Public Member Functions inherited from Template
 __construct (\Magento\Backend\Block\Template\Context $context, array $data=[])
 
 getFormKey ()
 
 isOutputEnabled ($moduleName=null)
 
 getAuthorization ()
 
 getToolbar ()
 
- Public Member Functions inherited from Template
 __construct (Template\Context $context, array $data=[])
 
 setTemplateContext ($templateContext)
 
 getTemplate ()
 
 setTemplate ($template)
 
 getTemplateFile ($template=null)
 
 getArea ()
 
 assign ($key, $value=null)
 
 fetchView ($fileName)
 
 getBaseUrl ()
 
 getObjectData (\Magento\Framework\DataObject $object, $key)
 
 getCacheKeyInfo ()
 
- Public Member Functions inherited from AbstractBlock
 __construct (\Magento\Framework\View\Element\Context $context, array $data=[])
 
 getJsLayout ()
 
 getRequest ()
 
 getParentBlock ()
 
 setLayout (\Magento\Framework\View\LayoutInterface $layout)
 
 getLayout ()
 
 setNameInLayout ($name)
 
 getChildNames ()
 
 setAttribute ($name, $value=null)
 
 setChild ($alias, $block)
 
 addChild ($alias, $block, $data=[])
 
 unsetChild ($alias)
 
 unsetCallChild ($alias, $callback, $result, $params)
 
 unsetChildren ()
 
 getChildBlock ($alias)
 
 getChildHtml ($alias='', $useCache=true)
 
 getChildChildHtml ($alias, $childChildAlias='', $useCache=true)
 
 getBlockHtml ($name)
 
 insert ($element, $siblingName=0, $after=true, $alias='')
 
 append ($element, $alias='')
 
 getGroupChildNames ($groupName)
 
 getChildData ($alias, $key='')
 
 getUiId ($arg1=null, $arg2=null, $arg3=null, $arg4=null, $arg5=null)
 
 getJsId ($arg1=null, $arg2=null, $arg3=null, $arg4=null, $arg5=null)
 
 getUrl ($route='', $params=[])
 
 getViewFileUrl ($fileId, array $params=[])
 
 formatDate ( $date=null, $format=\IntlDateFormatter::SHORT, $showTime=false, $timezone=null)
 
 formatTime ( $time=null, $format=\IntlDateFormatter::SHORT, $showDate=false)
 
 getModuleName ()
 
 escapeHtml ($data, $allowedTags=null)
 
 escapeJs ($string)
 
 escapeHtmlAttr ($string, $escapeSingleQuote=true)
 
 escapeCss ($string)
 
 stripTags ($data, $allowableTags=null, $allowHtmlEntities=false)
 
 escapeUrl ($string)
 
 escapeXssInUrl ($data)
 
 escapeQuote ($data, $addSlashes=false)
 
 escapeJsQuote ($data, $quote='\'')
 
 getNameInLayout ()
 
 getCacheKeyInfo ()
 
 getCacheKey ()
 
 getVar ($name, $module=null)
 
 isScopePrivate ()
 
- Public Member Functions inherited from DataObject
 __construct (array $data=[])
 
 addData (array $arr)
 
 setData ($key, $value=null)
 
 unsetData ($key=null)
 
 getData ($key='', $index=null)
 
 getDataByPath ($path)
 
 getDataByKey ($key)
 
 setDataUsingMethod ($key, $args=[])
 
 getDataUsingMethod ($key, $args=null)
 
 hasData ($key='')
 
 toArray (array $keys=[])
 
 convertToArray (array $keys=[])
 
 toXml (array $keys=[], $rootName='item', $addOpenTag=false, $addCdata=true)
 
 convertToXml (array $arrAttributes=[], $rootName='item', $addOpenTag=false, $addCdata=true)
 
 toJson (array $keys=[])
 
 convertToJson (array $keys=[])
 
 toString ($format='')
 
 __call ($method, $args)
 
 isEmpty ()
 
 serialize ($keys=[], $valueSeparator='=', $fieldSeparator=' ', $quote='"')
 
 debug ($data=null, &$objects=[])
 
 offsetSet ($offset, $value)
 
 offsetExists ($offset)
 
 offsetUnset ($offset)
 
 offsetGet ($offset)
 
- Public Member Functions inherited from BlockInterface
 toHtml ()
 

Protected Member Functions

 _toHtml ()
 
- Protected Member Functions inherited from Template
 _toHtml ()
 
- Protected Member Functions inherited from Template
 _construct ()
 
 _toHtml ()
 
 getRootDirectory ()
 
 getMediaDirectory ()
 
- Protected Member Functions inherited from AbstractBlock
 _construct ()
 
 _prepareLayout ()
 
 _beforeToHtml ()
 
 _afterToHtml ($html)
 
 _toHtml ()
 
 _getNotFoundUrl ($route='', $params=['_direct'=> 'core/index/notFound'])
 
 getCacheTags ()
 
 getCacheLifetime ()
 
 _loadCache ()
 
 _saveCache ($data)
 
 _getSidPlaceholder ($cacheKey=null)
 
- Protected Member Functions inherited from DataObject
 _getData ($key)
 
 _underscore ($name)
 

Protected Attributes

 $_elementFactory
 
 $_jsonEncoder
 
- Protected Attributes inherited from Template
 $_authorization
 
 $mathRandom
 
 $_backendSession
 
 $formKey
 
 $nameBuilder
 
- Protected Attributes inherited from Template
 $_viewVars = []
 
 $_baseUrl
 
 $_jsUrl
 
 $_allowSymlinks
 
 $_filesystem
 
 $_template
 
 $templateEnginePool
 
 $_storeManager
 
 $_appState
 
 $directory
 
 $templateContext
 
 $pageConfig
 
 $resolver
 
 $validator
 
- Protected Attributes inherited from AbstractBlock
 $_design
 
 $_session
 
 $_sidResolver
 
 $_nameInLayout
 
 $_layout
 
 $jsLayout = []
 
 $_request
 
 $_urlBuilder
 
 $_eventManager
 
 $_frontController
 
 $_assetRepo
 
 $_viewConfig
 
 $_cacheState
 
 $_logger
 
 $_escaper
 
 $filterManager
 
 $_localeDate
 
 $inlineTranslation
 
 $_isScopePrivate = false
 
 $_scopeConfig
 
 $_cache
 
- Protected Attributes inherited from DataObject
 $_data = []
 

Additional Inherited Members

- Static Public Member Functions inherited from AbstractBlock
static extractModuleName ($className)
 
- Data Fields inherited from Template
const XML_PATH_TEMPLATE_ALLOW_SYMLINK = 'dev/template/allow_symlink'
 
- Data Fields inherited from AbstractBlock
const CACHE_GROUP = \Magento\Framework\App\Cache\Type\Block::TYPE_IDENTIFIER
 
const CACHE_KEY_PREFIX = 'BLOCK_'
 
- Static Protected Attributes inherited from DataObject
static $_underscoreCache = []
 

Detailed Description

Chooser widget block.

Definition at line 17 of file Chooser.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\Backend\Block\Template\Context  $context,
\Magento\Framework\Json\EncoderInterface  $jsonEncoder,
\Magento\Framework\Data\Form\Element\Factory  $elementFactory,
array  $data = [] 
)
Parameters
\Magento\Backend\Block\Template\Context$context
\Magento\Framework\Json\EncoderInterface$jsonEncoder
\Magento\Framework\Data\Form\Element\Factory$elementFactory
array$data

Definition at line 35 of file Chooser.php.

40  {
41  $this->_jsonEncoder = $jsonEncoder;
42  $this->_elementFactory = $elementFactory;
43  parent::__construct($context, $data);
44  }

Member Function Documentation

◆ _toHtml()

_toHtml ( )
protected

Return chooser HTML and init scripts

Returns
string

Definition at line 137 of file Chooser.php.

138  {
139  $element = $this->getElement();
140  /* @var $fieldset \Magento\Framework\Data\Form\Element\Fieldset */
141  $fieldset = $element->getForm()->getElement($this->getFieldsetId());
142  $chooserId = $this->getUniqId();
143  $config = $this->getConfig();
144 
145  // add chooser element to fieldset
146  $chooser = $fieldset->addField(
147  'chooser' . $element->getId(),
148  'note',
149  ['label' => $config->getLabel() ? $config->getLabel() : '', 'value_class' => 'value2']
150  );
151  $hiddenHtml = '';
152  if ($this->getHiddenEnabled()) {
153  $hidden = $this->_elementFactory->create('hidden', ['data' => $element->getData()]);
154  $hidden->setId("{$chooserId}value")->setForm($element->getForm());
155  if ($element->getRequired()) {
156  $hidden->addClass('required-entry');
157  }
158  $hiddenHtml = $hidden->getElementHtml();
159  $element->setValue('');
160  }
161 
162  $buttons = $config->getButtons();
163  $chooseButton = $this->getLayout()->createBlock(
164  \Magento\Backend\Block\Widget\Button::class
165  )->setType(
166  'button'
167  )->setId(
168  $chooserId . 'control'
169  )->setClass(
170  'btn-chooser'
171  )->setLabel(
172  $buttons['open']
173  )->setOnclick(
174  $chooserId . '.choose()'
175  )->setDisabled(
176  $element->getReadonly()
177  );
178  $chooser->setData('after_element_html', $hiddenHtml . $chooseButton->toHtml());
179 
180  // render label and chooser scripts
181  $configJson = $this->_jsonEncoder->encode($config->getData());
182  return '
183  <label class="widget-option-label" id="' .
184  $chooserId .
185  'label">' .
186  ($this->getLabel() ? $this->escapeHtml($this->getLabel()) : __(
187  'Not Selected'
188  )) .
189  '</label>
190  <div id="' .
191  $chooserId .
192  'advice-container" class="hidden"></div>
193  <script>
194  require(["prototype", "mage/adminhtml/wysiwyg/widget"], function(){
195  //<![CDATA[
196  (function() {
197  var instantiateChooser = function() {
198  window.' .
199  $chooserId .
200  ' = new WysiwygWidget.chooser(
201  "' .
202  $chooserId .
203  '",
204  "' .
205  $this->getSourceUrl() .
206  '",
207  ' .
208  $configJson .
209  '
210  );
211  if ($("' .
212  $chooserId .
213  'value")) {
214  $("' .
215  $chooserId .
216  'value").advaiceContainer = "' .
217  $chooserId .
218  'advice-container";
219  }
220  }
221 
222  jQuery(instantiateChooser);
223  })();
224  //]]>
225  });
226  </script>
227  ';
228  }
$config
Definition: fraud_order.php:17
__()
Definition: __.php:13
$element
Definition: element.phtml:12

◆ getConfig()

getConfig ( )

Convert Array config to Object

Returns
\Magento\Framework\DataObject

Definition at line 71 of file Chooser.php.

72  {
73  if ($this->_getData('config') instanceof \Magento\Framework\DataObject) {
74  return $this->_getData('config');
75  }
76 
77  $configArray = $this->_getData('config');
78  $config = new \Magento\Framework\DataObject();
79  $this->setConfig($config);
80  if (!is_array($configArray)) {
81  return $this->_getData('config');
82  }
83 
84  // define chooser label
85  if (isset($configArray['label'])) {
86  $config->setData('label', __($configArray['label']));
87  }
88 
89  // chooser control buttons
90  $buttons = ['open' => __('Choose...'), 'close' => __('Close')];
91  if (isset($configArray['button']) && is_array($configArray['button'])) {
92  foreach ($configArray['button'] as $id => $label) {
93  $buttons[$id] = __($label);
94  }
95  }
96  $config->setButtons($buttons);
97 
98  return $this->_getData('config');
99  }
$config
Definition: fraud_order.php:17
$id
Definition: fieldset.phtml:14
__()
Definition: __.php:13
$label
Definition: details.phtml:21

◆ getElement()

getElement ( )

Chooser form element getter

Returns
\Magento\Framework\Data\Form\Element\AbstractElement

Definition at line 61 of file Chooser.php.

62  {
63  return $this->_getData('element');
64  }

◆ getFieldsetId()

getFieldsetId ( )

Form element fieldset id getter for working with form in chooser

Returns
string

Definition at line 116 of file Chooser.php.

117  {
118  return $this->_getData('fieldset_id');
119  }

◆ getHiddenEnabled()

getHiddenEnabled ( )

Flag to indicate include hidden field before chooser or not

Returns
bool @SuppressWarnings(PHPMD.BooleanGetMethodName)

Definition at line 127 of file Chooser.php.

128  {
129  return $this->hasData('hidden_enabled') ? (bool)$this->_getData('hidden_enabled') : true;
130  }

◆ getSourceUrl()

getSourceUrl ( )

Chooser source URL getter

Returns
string

Definition at line 51 of file Chooser.php.

52  {
53  return $this->_getData('source_url');
54  }

◆ getUniqId()

getUniqId ( )

Unique identifier for block that uses Chooser

Returns
string

Definition at line 106 of file Chooser.php.

107  {
108  return $this->_getData('uniq_id');
109  }

Field Documentation

◆ $_elementFactory

$_elementFactory
protected

Definition at line 22 of file Chooser.php.

◆ $_jsonEncoder

$_jsonEncoder
protected

Definition at line 27 of file Chooser.php.


The documentation for this class was generated from the following file: