Jump to content

404 when using pagination on multi language (via url-presegment) site


neildaemond
 Share

Recommended Posts

i've used "Example 3: Display a different language based on URL pre-segment" from the guide to make a site multi-language capable..

my language gateway looks like:

<?php 

    $user->language = $languages->get($page->name);
    $path = '/';
    foreach($input->urlSegments as $segment) {
    $path .= $segment . '/';
    }
    $mypage = $pages->get($path);
    if(!$mypage->id || !$mypage->viewable()) throw new Wire404Exception();

//replace local navigation links
    $out = $mypage->render();
    $regex = '{(<a [^>]*href=["\']' . $config->urls->root .')(["\']';
    $topnav = $page->parent->children();
    foreach($topnav as $p) $regex .= "|" . $p->name;
    $regex .= ")}";
    $out = preg_replace($regex, '$1' . $page->name . '/$2', $out);

    echo $out; 

the regex works quite well for everything else, but but kind of skews up on the 'blog/news' page which uses pagination.

besides the fact that the pagination links end up as 'site.com/blog/blog/page2' instead of 'site.com/blog/page2', even when manually entering 'site.com/blog/page2', 4040 error is thrown.

I'm guessing that things go bad here:

    $mypage = $pages->get($path);
    if(!$mypage->id || !$mypage->viewable()) throw new Wire404Exception();

since  "path/to/page/page2" will not not return a page. Also, removing the "throw new 404" line, I end up with a blank page.

Any suggestions?

Thanks,

Link to comment
Share on other sites

Just an idea - you could try to remove the pagination from the path before getting the page:

$path = preg_replace('/page[0-9]+$/', '', $path);

Or skip the last segment:

$n = count($input->urlSegments);
$i = 1;
foreach($input->urlSegments => $segment) {
  if ($i == $n) {
    if (preg_match('/page[0-9]+/', $segment)) break;
  }  
  $path .= $segment . '/';
}
Link to comment
Share on other sites

How is the pagination segment "page2" ending up in the path? The page number isn't considered one of the URL segments returned by $input->urlSegments, so it shouldn't be showing up in there unless the "allow page numbers" option is turned off for that template. 

  • Like 1
Link to comment
Share on other sites

good question, Ryan and it lead me to the solution.

Although I had "allow page numbers" enabled on the template containing the paginationMarkup (the blog/news feed), I didn't have it enabled on the 'language-gateway' template.

So, settings "allow page numbers" on the 'language-gateway' template solved the issue :)

Perhaps we can mention this little caveat in the documentation ;)

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Similar Content

    • By jds43
      Hello, how would I write a condition to output markup only to pages that have a template with multi-language support? I'm not finding any references to this particular topic.
       
    • By fruid
      $limit = 12; $start = $limit * ($input->pageNum() - 1); $allcasts = pages("template=cast"); $casts = $allcasts->find("has_parent=$page, sort=$sort"); $casts = sortOutEmpty($casts, $decider); $casts->setTotal($casts->count()); $casts->setStart($start); $casts->setLimit($limit); // just to test echo $casts->getStart(); // 0 or 12 or 24 or 30 respectively echo $casts->getLimit(); // 12 echo $casts->getTotal(); // 31 or whatever the total echo casts($casts); // never mind this, it's just for markup creation and works fine $pager->render($casts, $pagerOptions); // I store the name of a specific field as a string in a variable so I can dynamically sort out content of which that field is empty in the current language  function sortOutEmpty($items, $decider) {     $casts = new PaginatedArray;     foreach ($items as $item) :         if ($item->$decider != '') :             $casts->add($item);         endif;     endforeach;     return $casts; } I get the right number of pages in the pager. Pager works but the results never paginate.
      This pagination API is driving me nuts, it just doesn't work!
      I can't be the only one who's having issues with this, it's hard to find any topic where issues are discussed and the API-documentation is, as usual, very laconic.
      Thanks for help!
    • By fedeb
      Hi,
      I am trying to style the pager navigation bar based on a simple example I found on bootstrap:
      <nav aria-label="..."> <ul class="pagination"> <li class="page-item disabled"> <a class="page-link" href="#" tabindex="-1" aria-disabled="true">Previous</a> </li> <li class="page-item"><a class="page-link" href="#">1</a></li> <li class="page-item active" aria-current="page"> <a class="page-link" href="#">2</a> </li> <li class="page-item"><a class="page-link" href="#">3</a></li> <li class="page-item"> <a class="page-link" href="#">Next</a> </li> </ul> </nav> The code I am using is the following:
      <?php echo $entries->renderPager(array( 'nextItemLabel' => "Next", 'previousItemLabel' => "Prev", 'listMarkup' => "<ul class='pagination'>{out}</ul>", 'itemMarkup' => "<li class='page-item'>{out}</li>", 'linkMarkup' => "<a class='page-link' href='{url}'>{out}</a>", 'currentItemClass' => "page-item active", )); ?> which renders this result: the current page is not styled!

      If I run the original bootstrap example everything works fine, but not through the renderPager options. The problem seems trivial but since I am new to ProcessWire (also to web design) I cannot find it! My main reference is this one.
      Thanks in advance!
       
    • By joeck
      I am struggling with an issue where my language switcher with flags sometimes doesn't show the flag.
      It works in the default language (german):

      but when the site is in english the german flag is not shown (path to image is not found):

      I don't really understand why it works if the site is shown in german but not in english..
      Code for language switcher:
      // remember what language is set to $savedLanguage = $user->language; $languageImage = $savedLanguage->image->url; echo "<li><a href='#' class='dropdown-toggle' data-toggle='dropdown'><img src='$languageImage' alt='$savedLanguage->title'> <span uk-icon='icon: chevron-down'></span></a>"; echo "<div class='uk-navbar-dropdown'>"; echo " <ul class='uk-nav uk-navbar-dropdown-nav'>"; foreach($languages as $language) { //go through all languages // if user is already viewing the page in this language, skip it if($language->id == $savedLanguage->id) continue; // if this page isn't viewable (active) for the language, display root page $viewable = true; if(!$page->viewable($language)) $viewable = false; // set the user's language, so that the $page->url and any other // fields we access from it will be reflective of the $language $user->language = $language; // output a link to this page in the other language $path = $language->image->url; $pagePath = $page->url; if(!$viewable) $pagePath = $pages->get(1)->url; echo "<li><a class='uk-text-medium' href='$pagePath' alt='$language->title'><img src='$path' alt='$language->title flag'> $language->title</a></li>"; } echo " </ul>"; echo "</div>"; echo "</li>"; // restore the original language setting $user->language = $savedLanguage; Language template:

      image field is configured as automatic (array if multiple)
      in language page (both german and english) only one image is saved
      html output of language switcher dropwon (image couldn't be loaded):

      html output when image can be displayed:

    • By fruid
      I'm having and always have a hard time building PaginatedArrays, I never know where to put the "limit=24" selector so please enlighten me.
      Here's what I'm doing…
      $categories = $page->protable('start=0, limit=999999'); // this I need in order to retrieve the pages's "categories" and I don't know how to make use of $rows instead for that purpose because of this confusing limit-API $rows = $page->protable; // put ("limit=20") here? $custom = buildSelector($input, $rows); // this function returns an array of selectors depending on the user input $items = new PaginatedArray; // or here? // or here? $items->find("limit=20") // or like this? $items("limit=20") // or like this? $items = $items("limit=20") // or like this? $items = $items->find("limit=20") foreach ($rows as $r) : if ($custom->get('selector')->matches($r)) : $items->add($r); endif; endforeach; // or at this point? and then
      if ($items) { // or maybe somewhere here?     echo '<span class="grey">'.$items->getPaginationString(array(     'label' => 'entries',     'zeroLabel' => '0 entries', // 3.0.127+ only     'usePageNum' => false,     'count' => count($items),     'start' => $items->getStart(),     'limit' => count($items),     'total' => $items->getTotal()     )); and then of course…
      $pager = $modules->get("MarkupPagerNav"); echo '<div class="uk-flex uk-flex-center">'.$pager->render($items, $options).'</div>'; I'm out of ideas and confused cause it doesn't make sense to me either way.
      ____
      The buildSelector function above returns and array…
      $selected = new Selectors("$letter, $searchterm, $category"); $custom = new Wirearray; $custom->set('selector', $selected); $built->set('sort', $sort); return $custom; and each of the new selectors are basically strings ("category=whatever")
      Also, you cannot put the "sort=title" or whatever as a selector for the Selectors function (see above). Why, I know not.
      I don't know if that is the proper way but selecting kind of works now as opposed to many other ways I tried. Selectors always require a lot of trial and error, it seems to have a very sensitive API, always depends on double quotes, single quotes and how you concatenate.
      Thanks for help!
×
×
  • Create New...