Magento also has magic methods for unsetting, and checking for the existence
of a property
$this - > u n s T h e K e y (); $this - > h a s T h e K e y ();
Checkout the source of
Varien Objectfor more information
p u b l i c f u n c t i o n _ _ c a l l ( $method , $ a r g s ) { s w i t c h ( s u b s t r ( $method , 0 , 3)) { c a s e ’ get ’ : // V a r i e n _ P r o f i l e r :: s t a r t ( ’ G E T T E R : ’. g e t _ c l a s s ($t h i s ). ’:: ’.$m e t h o d ); $ k e y = $this - > _ u n d e r s c o r e ( s u b s t r ( $method , 3 ) ) ; $ d a t a = $this - > g e t D a t a ( $key , i s s e t ( $ a r g s [ 0 ] ) ? $ a r g s [0] : n u l l ); // V a r i e n _ P r o f i l e r :: s t o p ( ’ G E T T E R : ’. g e t _ c l a s s ($t h i s ). ’:: ’.$m e t h o d ); r e t u r n $ d a t a ; c a s e ’ set ’ : // V a r i e n _ P r o f i l e r :: s t a r t ( ’ S E T T E R : ’. g e t _ c l a s s ($t h i s ). ’:: ’.$m e t h o d ); $ k e y = $this - > _ u n d e r s c o r e ( s u b s t r ( $method , 3 ) ) ; $ r e s u l t = $this - > s e t D a t a ( $key , i s s e t ( $ a r g s [ 0 ] ) ? $ a r g s [0] : n u l l ); // V a r i e n _ P r o f i l e r :: s t o p ( ’ S E T T E R : ’. g e t _ c l a s s ($t h i s ). ’:: ’.$m e t h o d ); r e t u r n $ r e s u l t ; c a s e ’ uns ’ : // V a r i e n _ P r o f i l e r :: s t a r t ( ’ U N S : ’. g e t _ c l a s s ($t h i s ). ’:: ’.$m e t h o d ); $ k e y = $this - > _ u n d e r s c o r e ( s u b s t r ( $method , 3 ) ) ; $ r e s u l t = $this - > u n s e t D a t a ( $ k e y ); // V a r i e n _ P r o f i l e r :: s t o p ( ’ U N S : ’. g e t _ c l a s s ($t h i s ). ’:: ’.$m e t h o d ); r e t u r n $ r e s u l t ; c a s e ’ has ’ : // V a r i e n _ P r o f i l e r :: s t a r t ( ’ H A S : ’. g e t _ c l a s s ($t h i s ). ’:: ’.$m e t h o d ); $ k e y = $this - > _ u n d e r s c o r e ( s u b s t r ( $method , 3 ) ) ; // V a r i e n _ P r o f i l e r :: s t o p ( ’ H A S : ’. g e t _ c l a s s ($t h i s ). ’:: ’.$m e t h o d ); r e t u r n i s s e t ( $this - > _ d a t a [ $ k e y ]); } t h r o w new V a r i e n _ E x c e p t i o n ( " I n v a l i d m e t h o d " . g e t _ c l a s s ( $ t h i s ) . " :: " . $ m e t h o d . " ( " . p r i n t _ r ( $args , 1 ) . " ) " ); }Visit http://www.pulsestorm.net/nofrills-layout-appendix-g to join the discus-
sion online.
Widget Field Rendering
Options
The simplest configuration for a Magento widget data parameter is
< p a r a m e t e r s >
< o u r _ p a r a m e t e r >
< visible >0 </ visible > < r e q u i r e d >1 </ r e q u i r e d > < value > f o o b a z b a r </ value > < type > text </ type >
</ o u r _ p a r a m e t e r > </ p a r a m e t e r s >
This creates a hidden field (
<visible>0</visible>) that will always be populated
with the value ’foobazbar’ (
<value>foobazbar</value>).
While hidden fields are useful for widgets entered via a CMS content area, it’s
far more common for parameters to have a visible user interface element that
allows end-system-users to enter data. That is to say, a visible text field is more
common
< p a r a m e t e r s >
< o u r _ p a r a m e t e r >
< visible >1 </ visible > < r e q u i r e d >1 </ r e q u i r e d >
< label > L a b e l for our P a r a m e t e r </ label > < type > text </ type >
< value > b a z b a r f o o </ value > < s o r t _ o r d e r >10 </ s o r t _ o r d e r > </ o u r _ p a r a m e t e r >
</ p a r a m e t e r s >
We’ve changed the
<visible/>tag so it contains the value ”1” (boolean for
true). We’ve also added a
<label>tag which will be used as the text label
which describes the field, and a
<sort order>field which control where (above
or below) a particular UI element will show up compared to others.
The
<value>bazbarfoo</value>
tag will set thedefaultvalue for the UI element.
Figure H.1
You can also augment your fields with some instructional text by using the
<description/>
node.
< p a r a m e t e r s >
< o u r _ p a r a m e t e r >
< visible >1 </ visible > < r e q u i r e d >1 </ r e q u i r e d >
< label > L a b e l for our P a r a m e t e r </ label > < type > text </ type >
< value > b a z b a r f o o </ value > < s o r t _ o r d e r >10 </ s o r t _ o r d e r > < d e s c r i p t i o n >
T h i s is the f i e l d w h e r e we put the t h i n g </ d e s c r i p t i o n >
</ o u r _ p a r a m e t e r > </ p a r a m e t e r s >
Sometimes a free form text field gives users too much control over what values
they enter in a widget. For cases where we want to restrict a user’s choices, we
can use a
selector
multiselect.
< p a r a m e t e r s >
< o u r _ p a r a m e t e r >
< visible >1 </ visible > < r e q u i r e d >1 </ r e q u i r e d >
< label > S h o u l d I S t a y or S h o u l d I Go ? </ label > < type > select </ type >
< value > stay </ value > < values >
< staying >
< value > stay </ value >
< label > T h e r e w i l l be Trouble </ label > </ staying >
< going >
< value > go </ value >
< label > T h e r e w i l l be Double </ label > </ going >
</ values >
< s o r t _ o r d e r >10 </ s o r t _ o r d e r > </ o u r _ p a r a m e t e r >
The important changes here are we’ve changed our to
<type/>tag to
select,
and added a new
<values/>node. The sub-nodes of the
<values/>node will be
used to create the label/value pairs for the HTML
<select>elements generated
for the front end. Alternatly, you can provide the name of a source model.
< p a r a m e t e r s >
< o u r _ p a r a m e t e r >
< visible >1 </ visible > < r e q u i r e d >1 </ r e q u i r e d >
< label > S h o u l d I S t a y or S h o u l d I Go ? </ label > < type > select </ type >
< value > stay </ value >
< s o u r c e _ m o d e l > a d m i n h t m l / s y s t e m _ c o n f i g _ s o u r c e _ y e s n o </ s o u r c e _ m o d e l > < s o r t _ o r d e r >10 </ s o r t _ o r d e r >
</ o u r _ p a r a m e t e r > </ p a r a m e t e r s >
The string
adminhtml/system config source yesnois a class alias for a Magento
model (in this case
Mage Adminhtml Model System Config Source Yesno). Source mod-
els are special model classes with a
toOptionArraymethod.
c l a s s M a g e _ A d m i n h t m l _ M o d e l _ S y s t e m _ C o n f i g _ S o u r c e _ Y e s n o { /* * * O p t i o n s g e t t e r * * @ r e t u r n a r r a y */ p u b l i c f u n c t i o n t o O p t i o n A r r a y () { r e t u r n a r r a y ( a r r a y ( ’ v a l u e ’ = > 1 , ’ l a b e l ’ = > M a g e :: h e l p e r ( ’ a d m i n h t m l ’ ) - > __ ( ’ Yes ’ )) , a r r a y ( ’ v a l u e ’ = > 0 , ’ l a b e l ’ = > M a g e :: h e l p e r ( ’ a d m i n h t m l ’ ) - > __ ( ’ No ’ )) , ); } }This method returns a set of key/value pairs for your select. You may create your
own source models, or use one the models that ships with Magento. Checkout
the PHP files in
app / c o d e / c o r e / M a g e / A d m i n h t m l / M o d e l / S y s t e m / C o n f i g / S o u r c e
for a list of the source models that ship with Magento.
H.1
Creating Your Own Form Elements
Sometimes you’re going to want a form element that’s more interactive than a
single text or a select. The widget system has a mechanism that allows you to
build your own form elements for the widget UI.
First, your parameter configuration should look like the following
< o u r _ p a r a m e t e r >
< visible >1 </ visible > < r e q u i r e d >1 </ r e q u i r e d >
< label > S h o u l d I S t a y or S h o u l d I Go ? </ label > < type > label </ type >
< h e l p e r _ b l o c k >
< type > y o u r p a c k a g e _ y o u r m o d u l e / w i d g e t t e s t </ type > </ h e l p e r _ b l o c k >
< s o r t _ o r d e r >10 </ s o r t _ o r d e r >
< d e s c r i p t i o n > T h i s is the f i e l d w h e r e you ’ ll put the s y n e r g y . </ d e s c r i p t i o n > </ o u r _ p a r a m e t e r >