Preserve the order of nodes in fetch function

Author Message

Simone D'Amico

Friday 21 January 2011 9:04:22 am

Hi guys,

I've an array in ini file that contains the node ID of contents to display in one page. Something like this:

[PLPMenu]
MenuArray[]
MenuArray[]=121
MenuArray[]=60
MenuArray[]=61
MenuArray[]=62
MenuArray[]=63
MenuArray[]=111

To retrieve the content nodes, I've used the following code:

{def $menu = ezini('PLPMenu', 'MenuArray', 'plp.ini')}
{def $menu_items = fetch( 'content', 'node', hash( 'node_id', $menu ))}

It works fine but I need to preserve the order of nodes. The fetch function, instead, returns the nodes ordered by node ID. It returns the nodes in the order 60, 61, 62, 63, 111, 121 but I need the array order.

How can I preserve the right order of nodes?

Franck Magnan

Sunday 23 January 2011 7:36:48 am

Hello Simone,
it seems you made a mistakes in your template code:

  • The content/node fetch function retrieve a node and you use it to retrieve an array.
    In your code, $menu_item is not an array but a node.
  • The content/node fetch function needs a node_id in argument and you passed it $menu which is an array

If you want to get an array of nodes, you need to use such a code :

{def
     $menu = ezini('PLPMenu', 'MenuArray', 'plp.ini')
     $nodes = array()
     $current_node = false()
}
   {foreach $menu as $node_id}
     {set $current_node=fetch( 'content', 'node', hash( 'node_id', $node_id ))}
     {if is_object($current_node)}
       {set $nodes = $nodes|append($current_node)}
     {/if}
   {/foreach}
{undef $menu $nodes}

Then, in your config file, you can get an order if you use an index.
For example, you can modify your code to get:

[PLPMenu]
MenuArray[]
MenuArray[1]=121
MenuArray[2]=60
MenuArray[3]=61
MenuArray[4]=62
MenuArray[5]=63
MenuArray[6]=111

--
Developer at Open Wide

Simone D'Amico

Monday 24 January 2011 1:15:32 am

Hi Frank,

your post helped me a lot in finding the right solution!

Instead of calling content/fetch node function outside foreach loop, now i use the foreach with $menu values and, inside I use the fetch function with single node.

In this way I preserve the right order. :)

Thanks a lot! :)

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.