Jump to content

Redirect admin view links for one page website?


photoman355
 Share

Recommended Posts

I'm building a template for a one page website and have run into a small problem with the admin view page links.  

Because my site is built with content blocks rather than pages there is no header and footer for each block, only the homepage needs to pull in the header and footer for the site.  As a result clicking to view a page in the admin shows the page content but with zero styling.  This is obviously not ideal for clients so I'd like to find a better solution.

Here's a rough idea of the site structure:

    Home  (header & footer)

        Page 1

            Content Block 1

            Content Block 2

        Page 2

            Content Block 3 etc etc

My page's use an anchor # for the url eg #contact so Ideally when the user clicks on the page they'd be redirected to that relevant anchor.  I know this might be complicated to achieve so all I really need to do is redirect all pages to the homepage (parent).  

Any idea how I would go about this? 

Link to comment
Share on other sites

If you use a different template on the pages that shouldn't be view-able (I assume everything but home), and that template does not have an associated file (example "basic-page" template has "basic-page.php") then the view button itself will not be shown. 

Another option is maybe to just use a repeater for the content blocks and then genuinely only have one page in the admin (home) and remove the other.

Hope that helps.

  • Like 1
Link to comment
Share on other sites

Thanks guys.  @adamspruijt I'm not quite sure what you mean by 

If you use a different template on the pages that shouldn't be view-able (I assume everything but home), and that template does not have an associated file (example "basic-page" template has "basic-page.php") then the view button itself will not be shown. 

Do you mean creating templates via admin without creating a physical template file?  All my blocks contain markup so they're built as separate eg block.php files.

I'm not familiar with the repeater field technique for content blocks.  Can you explain a bit more?

One workaround I thought of would be to add this to my admin template css which solves the problem up to a point.  

.content .PageListActionView {
  display: none !important;
}

 I was hoping there would be a nice simple way to handle it in Processwire. I know there's the redirect module but that's not ideal is it involves specifying each url and all I really need is a global rule that says - if child redirect to parent, or something like that.  

@Macrura can you give me an example of how I would add a redirect to a template?

Link to comment
Share on other sites

<?php 
$homepage = $pages->get('/'); 
if($page != $homepage) { // if we're not on the homepage do this
	$redurl = $homepage->url . "#" . $page->name; // this is where the page will redirect to
	$session->redirect($redurl);
}
?>

don't do it this way - it won't work right on the homepage - look at diogo's post below.

Edited by Macrura
Link to comment
Share on other sites

Thanks guys.  @adamspruijt I'm not quite sure what you mean by 

Do you mean creating templates via admin without creating a physical template file?  All my blocks contain markup so they're built as separate eg block.php files.

I'm not familiar with the repeater field technique for content blocks.  Can you explain a bit more?

Yes, I am talking about having a template without a file, you could probably move those templates that are not supposed to be view-able  like "block.php" into an "includes" folder and then use simple code to grab the right file based on the template name like this (assuming you've gotten the pages you need in the variable "$blocks"):

<?php 
foreach ($blocks as $block) {
	include("./includes/".$block->template.".php");
}
?>

 For the repeater method this would work only assuming the content in the "blocks" was very simple and could be handle with basic "Textarea/TinyMCE" fields, I didn't realize you mentioned #contact. Assuming the contact area requires a form or more advanced fields, what I mentioned first above would work best.

Link to comment
Share on other sites

Macrura, your solution won't work because inside the render() the $page object is the one from the rendered template, so ($page != $homepage) will always be true.

But the same can be achieved by testing agains the url:

if($page->url == $_SERVER[REQUEST_URI]) $session->redirect($pages->get(1)->url."#".$page->name);

and in the homepage, something like:

foreach($page->children as $p) {
    echo "<div id='{$p->name}'>";
    echo $p->render();
    echo "</div>";
}

  • Like 1
Link to comment
Share on other sites

@diogo - i thought that in this case the link from the admin was actually rendering the page at it's correct URL, like:

/page1/content-block-1

which is why i thought that would work... unless i'm missing something...

Link to comment
Share on other sites

What happens with render is that the $page object is momentarily replaced by the rendered page, so if you render a page inside the homepage, and in that page template you compare $page != $homepage, this will always be true. By doing like I did, you are comparing the real url in the browser with the rendered page url. This means that if the page it's called directly, the real url and the url from the $page object will match, but if the page is rendered from another page they won't.

  • Like 1
Link to comment
Share on other sites

Ok so I think what you are saying that when the homepage renders the content blocks, which are being done with the render method, the check I wrote won't work because the $page object is still not really 'home'; so i get that now, and will edit the code in my post..

Link to comment
Share on other sites

Thanks guys.  @adamspruijt, I never thought of adding blocks like that.  Definitely fast to deploy a site but one advantage of building with blocks as children is the blocks can be changed without touching the source.  I'm sure this could be set up via an of options page though.

@diogo Looks like a fairly straightforward solution, look forward to testing it out.  Much appreciated.

Link to comment
Share on other sites

Thanks guys.  @adamspruijt, I never thought of adding blocks like that.  Definitely fast to deploy a site but one advantage of building with blocks as children is the blocks can be changed without touching the source.  I'm sure this could be set up via an of options page though.

Why would you need to change the source? The structure of the block would be defined in the includes and ordered based on their order in the admin, unless your adding a new "block type", otherwise you should be able to add new content indefinitely. I don't think I know enough about what you're doing, you may not require the flexibility I'm picturing.

Regardless the redirect works, and allows you to have the view buttons intact on the back end, which could be nice.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...