Problem with "replace_link_with_first_in_nav"

Permalink
Hi,

I added "replace_link_with_first_in_nav" attribute for "EN" folder in my sitemap :

Home
- EN
-- page1
-- page2

So when i click on EN i am redirected to "page1". ok.

But i write in address bar "http://www.mysite.com/en", I'm not redirect to "page1"...

An idea ?

Thanks

orange73
 
Remo replied on at Permalink Best Answer Reply
Remo
well, there's no code that does that.

replace_link_with_first_in_nav is only used in autonav to create links to sub pages.

If you want to redirect to a subpage if you directly call a page you have to add some more code. header.php in your theme works for example.

Something like this might work..

$nh = Loader::helper('navigation');
if($c->getCollectionAttributeValue('replace_link_with_first_in_nav')){
   $subPage = $c->getFirstChild();
   if($subPage instanceof Page){
      $pageLink = $nh->getLinkToCollection($subPage, true);
      header("Location: " . $pageLink);
      exit;
   }
}


Todo:
- Add status code
- Make sure it works in a recursion to support sub-sub-pages
etc.
orange73 replied on at Permalink Reply
orange73
Hello Remo,

Thanks for your answer.

It's a good idea thanks.

Actually, to do that, I have created a page type which can be used by a page and do redirect link to his firschild if exist, else 404 error.

What do you think about that ?
Remo replied on at Permalink Reply
Remo
I usually try to keep things as simple as possible. A page type only used for such a feature seems to be a bit confusing for most of my customers.

The overhead you generate by putting this in header.php isn't huge and makes things easier.

If you use a page type, I recommend to delete the attribute. If the page type knows that the sub page is the one where the actual content is located, there's no need for an attribute.

By doing this, you'd have a simple solutions as well. I only recommend to use the attribute or a page type but not both at the same time.

However, if you want to use the page type only you'd have to modify autonav as well which is why I probably keep using the attribute in header.php..

Uhh so many options (: At the end, whatever you do, I guess it works ;-)
bhcarpenter replied on at Permalink Reply
bhcarpenter
I'm using the page type solution to do this as well.

I called the page type "Category", and implemented the redirect in the page type's controller like this:

// File: /controllers/page_types/category.php
class CategoryPageTypeController extends Controller
{
   public function on_start()
   {
      $subPage = $this->c->getFirstChild();
      if ( $subPage instanceof Page ) {
         $nh = Loader::helper('navigation');
         $url = $nh->getLinkToCollection($subPage);
      // This may happen if there are no children in the Category.
      } else {
         $url = '/';
      }
      header( 'Location: ' . $url );
      exit;


I've toyed around with the idea of using an optional attribute to let the user specify where to redirect. Comments and suggestions are welcome.
orange73 replied on at Permalink Reply
orange73
Yes, it's what i'm using actually, but i not create a controller for this page type but implement redirection directly in page type.

For the option which let user to choose page redirection, i have post in Beta Crew section :http://www.concrete5.org/developers/beta/beta_bitching/page-redirec...
Remo replied on at Permalink Reply
Remo
But autonav won't pick this up. You'll get an unnecessary redirect on your webserver if you don't create an autonav template which checks the page type.

Most people probably don't care, but redirects should be avoided. It's also the reason why there should be a slash at the end of a URL if it's not a file..