Forums / Setup & design / Auto redirect to a sub-item.

Auto redirect to a sub-item.

Author Message

Rémy-Christophe Schermesser

Wednesday 21 June 2006 7:50:35 am

Hi,

I have a tree of content in eZ publish :

Root
|_ Folder1
   |_ Article11
   |_ Article12
|_ Folder2
   |_ Article21
   |_ Article22

What I want is when a user accesses to the Folder1 he is redirected immediatly to the Article11.

Anyone has an idea ?

Thanks

Rcs

Nabil Alimi

Thursday 22 June 2006 2:40:52 pm

Hi Rémy-Christophe,

I don't think this can be achieved easily if you intend to keep the common ezpublish content access process. I mean accessing a content via his uri or node id. (content/view/full/189)
So instead of phisically redirecting your visitor to the sub article, you should try to display the article detail when people access to the folder by for example, retrieving the last article published under the current node.

My blog : http://www.starnab.com/ezpublish / http://www.starnab.com/ / http://www.assiki-consulting.com
eZ Publish Freelance developper. Feel free to contact me +33 674 367 057
nabil at assiki d0t fr

Roy Bøhmer

Thursday 22 June 2006 3:40:39 pm

Hi!
Take a look at Kristof's redirect module at http://ez.no/community/contribs/applications/redirect
I've never tried it, but if your nodes are only primary nodes (no other locations for the content object) this should help.

Also, overriding the folder template and only let it output {node_view_gui view=full content_node=$aNodeYouFetchedInTheTemplate} should do the trick. The downside (or upside?) is that there is noe particular redirect going on - so the url will stay the same.

Happy coding!

Roy

Matthew Carroll

Friday 23 June 2006 12:55:49 pm

Hi Rémy-Christophe

I'm using Kristof's extension to try to achieve something like this already. You could set all folders to redirect to their first child node, but in my case it's not every folder I want to do that. It's not finished yet, but the idea for the site I am working on is:

1. Install redirect operators
2. Edit 'Folder' class and add a new checkbox called 'redirect_first_child'
3. Override Folder template and add code that looks checks the tickbox and redirects if it is ticked, something like this:

{if $node.data_map.redirect_first_child.content}
{redirectrelative( $node.children[0].url_alias|ezurl )}
{/if}

Note: I'm still working on this so that code may not be quite right, but hopefully it should give you enough to go on. If you want every folder to behave like this, then simply use the redirectrelative in an override template.

Good luck
Matthew

http://carroll.org.uk

Philipp Simon

Saturday 24 June 2006 4:04:16 am

A quick and dirty hack would be using URL alias.

Add an virtual alias for /folder1 to folder1/article11

To get it working go to ezurlalias.php and change function "translate" to:

    function translate( &$uri, $reverse = false )
    {
		global $_SERVER;

                ...................

		if( false === strpos( $_SERVER[ 'SERVER_NAME' ], "admin." ) )
		{
			$tmpSortOrder = "DESC";
		}
		else
		{
			$tmpSortOrder = "ASC";		
		}
		
        $db =& eZDB::instance();
        if ( $reverse )
        {
            $query = "SELECT source_url as destination_url, forward_to_id
FROM ezurlalias
WHERE destination_url = '" . $db->escapeString( $internalURIString ) . "' AND
      forward_to_id = 0 AND
      is_wildcard = 0
ORDER BY forward_to_id ".$tmpSortOrder;
        }
        else
        {
            $query = "SELECT destination_url, forward_to_id
FROM ezurlalias
WHERE source_md5 = '" . md5( $internalURIString ) . "' AND
      is_wildcard = 0
ORDER BY forward_to_id ".$tmpSortOrder.", is_internal ASC";
        }

Not that I simply check the SERVER_NAME for string "admin." to identify admin access. This is possible because I use host setup admin.mydomain.com. (The rewrite should only happen on public site, cause editing on admin site should still be possible)

Perhaps this is an easier way for you.

Have luck.

Phil

@Matthew: This is exactly what I am searching for. You already got it working?

Matthew Carroll

Saturday 24 June 2006 10:40:17 am

@ Philipp: I do now :-)

As I suspected my late-night code was buggy - I should know better than to write code in a forum post without trying it first ;) ...also $node.children.0 doesn't return the correct node because it doesn't take account of the order I have set for that folder. (Is that a bug?) So... here's what is working:

{if $node.object.data_map.redirect_first_child.content}
	{def $children = fetch( content, list, hash( parent_node_id, $node.node_id, sort_by, $node.sort_array ) )}
	{redirectrelative( $children.0.url_alias|ezurl('no') )}
	{undef $children}
{/if}

...and a sneak preview of it in action:

http://new.coinet.org.uk/

(Click on 'About Us' in the dropdowns.)

Have fun :-)
Matthew

http://carroll.org.uk

Philipp Simon

Saturday 24 June 2006 11:36:27 am

Hi Metthew,

thanks a lot for sharing this solution.

I am not so familiar yet with the ezPublish interna. Perhaps you can tell me, which PHP (?) class I need to edit, to add this redirect flag. Think I have to add a tinyint(1) to the db table as well. Whould be really happy if you can help me out with this issue.

Phil

Matthew Carroll

Saturday 24 June 2006 11:50:08 am

Hi Philipp

There is no need to touch the ez internals at all, just install Kristof's excellent 'Redirect Operators' extension, available here:

http://ez.no/community/contribs/template_plugins/redirect_operators

Edit: Sorry, my mistake, I'm getting my extensions in a muddle and miscrediting them! RedirectOperators is by Esben Visfeldt. :-S

Good luck
Matthew

http://carroll.org.uk

Philipp Simon

Saturday 24 June 2006 12:05:07 pm

Hi Metthew,

ok I will do so. What about your second point:

"2. Edit 'Folder' class and add a new checkbox called 'redirect_first_child'"

I though I have to do this on my own!?

Phil

Matthew Carroll

Saturday 24 June 2006 12:52:04 pm

Yes, you do have add a new class attribute to the folder class to give the 'redirect to first child' behaviour an on/off switch on a folder by folder basis. In your admin site:

1. go to Setup - Classes - Content - Folder, which on a default ez installation is something like this: http://example.com/admin/class/view/1
2. edit the class
3. at the bottom there is a dropdown, select 'Checkbox', click 'Add Attribute'
4. enter name: Redirect First Child, identifier: redirect_first_child, and the only tickbox that needs to be ticked is 'disable translation'
5. at the bottom, click ok.
6. go back to the content tree and edit your folder - there should now be an extra tickbox at the bottom you can tick, which is what the template code then looks for when deciding whether to redirect to the node's first child or not.

...and you should be done. For more reading on content classes, I suggest:

http://ez.no/doc/ez_publish/technical_manual/3_8/concepts_and_basics/content_management/the_content_class

Matthew

http://carroll.org.uk

Philipp Simon

Sunday 25 June 2006 1:33:29 am

Oh damn, I have not realized yet, how easy it is. Thanks a lot for pointing this out. This will make my work really easier.

Now everything works fine. Again: THANKS!

Phil