eZ Community » Forums » Extensions » eZ Flow » How to store an array in a...
expandshrink

How to store an array in a CustomAttributes of a home made Block Type ?

How to store an array in a CustomAttributes of a home made Block Type ?

Wednesday 22 August 2012 9:33:28 am - 2 replies

Here is my block type :

[MyBlockType]
Name=My Block Type
ManualAddingOfItems=disabled
CustomAttributes[]=selectmultiple
CustomAttributeTypes[selectmultiple]=selectmultiple
UseBrowseMode[selectmultiple]=false
ViewList[]=page
ViewName[page]=Page

I've overridden the block/edit/edit.tpl template, to deal with the selectmultiple custom attribute type :

{case match = 'selectmultiple'}
  {def $elements=fetch( 'my_module', 'my_function', hash( 'param1', 1 ) )}
  <select name="ContentObjectAttribute_ezpage_block_custom_attribute_{$attribute.id}[{$zone_id}][{$block_id}][{$custom_attrib}][]"  multiple="multiple">
    {foreach $elements as $e}
      <option value="{$e.id}"{if $block.custom_attributes[$custom_attrib]|contains($e.id)}selected="selected"{/if}>{$p.label|wash}</option>
    {/foreach}
  </select>
{/case}

The problem is that the posted array is not stored. I've look the ezpageblock.php file to see how it works. I've try to use serialize and unserialize to deal with my case.

It works. But is it the right way ?

Monday 01 April 2013 4:51:07 pm

Quote from Simon Terrien :

Here is my block type :

[MyBlockType]
Name=My Block Type
ManualAddingOfItems=disabled
CustomAttributes[]=selectmultiple
CustomAttributeTypes[selectmultiple]=selectmultiple
UseBrowseMode[selectmultiple]=false
ViewList[]=page
ViewName[page]=Page

I've overridden the block/edit/edit.tpl template, to deal with the selectmultiple custom attribute type :

{case match = 'selectmultiple'}
  {def $elements=fetch( 'my_module', 'my_function', hash( 'param1', 1 ) )}
  <select name="ContentObjectAttribute_ezpage_block_custom_attribute_{$attribute.id}[{$zone_id}][{$block_id}][{$custom_attrib}][]"  multiple="multiple">
    {foreach $elements as $e}
      <option value="{$e.id}"{if $block.custom_attributes[$custom_attrib]|contains($e.id)}selected="selected"{/if}>{$p.label|wash}</option>
    {/foreach}
  </select>
{/case}

The problem is that the posted array is not stored. I've look the ezpageblock.php file to see how it works. I've try to use serialize and unserialize to deal with my case.

It works. But is it the right way ?

Hi everybody,

I have the same problem with multi-checkbox. 

Is possible post and save an array of values in custom attributes?

Anyone can help me?

Tuesday 06 August 2013 1:28:58 pm

Just in case anyone needs it:

1. Copy the file extension/ezflow/classes/ezpageblock.php to extension/[your_extension]/classes/ezpageblock.php

2. Apply the following changes:

 
         127,130c127,142
<                         $tmp = $dom->createElement( $arrayItemKey );
<                         $tmpValue = $dom->createTextNode( $arrayItemValue );
<                         $tmp->appendChild( $tmpValue );
<                         $node->appendChild( $tmp );
---
>                         if ($arrayItemKey != '')
>                         {
>                             $tmp = $dom->createElement( $arrayItemKey );
>                             /*
>                              * We need to serialize the custom attribute, if it
>                              * is an array. At the moment this means it comes 
>                              * from a multi-select.
>                              */
>                             if ( is_array( $arrayItemValue ) )
>                             {
>                                 $arrayItemValue = serialize( $arrayItemValue );
>                             }
>                             $tmpValue = $dom->createTextNode( $arrayItemValue );
>                             $tmp->appendChild( $tmpValue );
>                             $node->appendChild( $tmp );
>                         }
199c211,229
<                         $attrValue[$subNode->nodeName] = $subNode->nodeValue;
---
>                     {
>                         $nodeValue = @unserialize( $subNode->nodeValue );
>                         if ( $subnode->nodeValue === 'b:0;' || $nodeValue !== false )
>                         {
>                             /*
>                              * block custom attribute was serialized!
>                              * atm: only multi-select is serialized.
>                              */
>                         }
>                         else
>                         {
>                             /*
>                              * block custom attribute was _not_ serialized
>                              */
>                             $nodeValue = $subNode->nodeValue;
>                         }
>                         $attrValue[$subNode->nodeName] = $nodeValue;
>                     }
>                         
301a332,346
>             if (is_array( $value ) )
>             {
>                 foreach ( $value as $key => $val )
>                 {
>                     if ( is_array( $val ) )
>                     {
>                         $tmpValue[$key] = implode( '#*~+', $val );
>                     }
>                     else
>                     {
>                         $tmpValue[$key] = $val;
>                     }
>                 }
>                 #echo '+'.$name.": ".print_r($value,true).'###'.print_r($tmpValue,true).+';
>             }

3. I also copied the extension/ezflow/design/standard/templates/block/edit/edit.tpl to my extension and added the following to the switch-case:

                         {case match = 'select'}
                        {def $options= ezini( $block.type, concat('CustomAttributeParams',$custom_attrib), 'block.ini' )}
                            <select class="select block-control" name="ContentObjectAttribute_ezpage_block_custom_attribute_{$attribute.id}[{$zone_id}][{$block_id}][{$custom_attrib}]">
                                {foreach $options as $option}
                                    {def $option_array = $option|explode(';')}
                                    <option value="{$option_array[0]}"{if $block.custom_attributes[$custom_attrib]|eq($option_array[0])} selected="selected"{/if}>{$option_array[1]}</option>
                                    {undef $option_array}
                                {/foreach}
                            </select>
                        {/case}
                        {case match = 'multiselect'}
                        {def $options= ezini( $block.type, concat('CustomAttributeParams',$custom_attrib), 'block.ini' )}
                            <select class="multiple select block-control" name="ContentObjectAttribute_ezpage_block_custom_attribute_{$attribute.id}[{$zone_id}][{$block_id}][{$custom_attrib}][]" multiple="multiple">
                                {foreach $options as $option}
                                    {def $option_array = $option|explode(';')}
                                    <option value="{$option_array[0]}"{if $block.custom_attributes[$custom_attrib]|contains($option_array[0])} selected="selected"{/if}>{$option_array[1]}</option>
                                    {undef $option_array}
                                {/foreach}
                            </select>
                        {/case}

4. You can set as follows in your block.ini:

 [MultiSelectBlock]
Name=My Multi Select Block
NumberOfValidItems=0
NumberOfArchivedItems=0
ManualAddingOfItems=disabled
ViewList[]=standard
ViewName[standard]=Standard
CustomAttributes[]
CustomAttributeTypes[]
CustomAttributes[]=MyCustomMultiSelectName
CustomAttributeTypes[MyCustomMultiSelectName]=multiselect
CustomAttributeParamsMyCustomMultiSelectName[]
CustomAttributeParamsMyCustomMultiSelectName[]=value1;Label 1
CustomAttributeParamsMyCustomMultiSelectName[]=value2;Label 2

Hope that helps. If you find a mistake, please tell me. It is a little too unstructured for a pull request to ezflow though but in my opinion, this is a clean way to handle this.

expandshrink

You must be logged in to post messages in this topic!

36 542 Users on board!

Forums menu

Proudly Developed with from