• No results found

Other Magic Methods

In document No Frills Magento Layout. Alan Storm (Page 154-159)

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 Object

for 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

select

or

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 yesno

is 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

toOptionArray

method.

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 >

The key nodes here are

<type>

and

<helper block>

. Our field type here is

label

.

Normally, a field type of label will render the label for a field without any

form element. In other words, a form element with no functional value, only in-

structional/branding/experience. However, we’ve also included a

<helper block>

node. This node (via the

<type/>

sub-node) configures a block class that will

render our form.

After configuring this parameter, you’ll need to define your block class. Despite

living in the standard block heirarchy, wedo notwant to implement our ren-

dering in its

toHtml

method. Instead, this class needs a special method named

prepareElementHtml # F i l e : a p p / c o d e / l o c a l / Y o u r p a c k a g e / Y o u r m o d u l e / B l o c k / W i d g e t t e s t . p h p c l a s s Y o u r p a c k a g e _ Y o u r m o d u l e _ B l o c k _ W i d g e t t e s t e x t e n d s M a g e _ C o r e _ B l o c k _ A b s t r a c t { /* * * O v e r l y s i m p l e e x a m p l e */ p u b l i c f u n c t i o n p r e p a r e E l e m e n t H t m l ( V a r i e n _ D a t a _ F o r m _ E l e m e n t _ A b s t r a c t $ e l e m e n t ) { $ s i m p l e _ i n p u t = ’ < i n p u t t y p e =" t e x t " n a m e =" ’ . s t r i p _ t a g s ( $ e l e m e n t - > g e t N a m e ()) . ’ " v a l u e =" ’ . s t r i p _ t a g s ( $ e l e m e n t - > g e t V a l u e ( ) ) . ’ "/ > ’ ; $ e l e m e n t - > s e t D a t a ( ’ a f t e r _ e l e m e n t _ h t m l ’ , $ s i m p l e _ i n p u t ); $ e l e m e n t - > s e t V a l u e ( ’ ’ ); // b l a n k o u t v a l u e r e t u r n $ e l e m e n t ; } }

During the rendering of your parameter’s UI, Magento will call the

prepareElementHtml

method of your

helper block

, passing in a

Varien Data Form Element Abstract

object.

This

$element

is the object that Magento will use to render out your form ele-

ment. To implement a custom form element, your job is

1. Grab the form element’s name and value from

$element

. The value will

for the HTML form element to ensure the form data is saved on post

2. Add HTML to the form element’s rendering process to implement your

custom element

3. Optionally, if you don’t want the default value rendering to take place,

clear the value from

$element

before returning it.

In our example above, we’ve created a ludicrously simple example to demon-

strate how you might acheive this. We’ve

1. Created a HTML element

($simple input

) using string concatenation.

2. Added this HTML to the element with

$element->setData(’after element html’, $simple input);

3. Zeroed out the value of

$element

In document No Frills Magento Layout. Alan Storm (Page 154-159)