Is there a way to get filterBy('template', 'mytemplate') work on a multilang site?

#21

I’m sorry I can’t really help you with this at the moment. Maybe someone else has another idea. With nothing I have tried so far could I reproduce the issue. What do you have in your config?

I agree it’s a bad user experience if you end up with a protected page in the search results.

0 Likes

#22
// Turn on/off cache:
c::set('cache', false);

// Exclude pages from caching:
c::set('cache.ignore', array('sitemap'));

// Date handlers:
c::set('date.handler', 'strftime');

// Use index as name for the start page:
c::set('home', 'index');

// Panel CSS:
// c::set('panel.stylesheet', 'assets/css/panel.css');

// ---------------------------------------------------------
// Image configuration
// ---------------------------------------------------------

// Use ImageMagick as thumbnail driver:
c::set('thumbs.driver', 'im');

// Define thumbs filename:
c::set('thumbs.filename', '{safeName}-{hash}-{width}x{height}.{extension}');

// Set thumbs quality to 90:
c::set('thumbs.quality', 90);

// Progressive JPEG:
c::set('thumbs.interlace', true);

// ---------------------------------------------------------
// Routes setup
// ---------------------------------------------------------

c::set('routes', array(
  array(
    'pattern' => 'sitemap.xml',
    'action' => function() {
      return page('sitemap');
    }
  ),
  array(
    'pattern' => 'en/sitemap.xml',
    'action' => function() {
      return site()->visit('sitemap', 'en');
    }
  ),
  array(
    'pattern' => 'logga-ut',
    'action' => function() {
      if($user = site()->user()) $user->logout();
      return page('databas');
    }
  ),
  array(
    'pattern' => 'en/logout',
    'action' => function() {
      if($user = site()->user()) $user->logout();
      return site()->visit('database', 'en');
    }
  )
));

// ---------------------------------------------------------
// Language configuration
// ---------------------------------------------------------

// Setup multisite:
c::set('languages', array(
  array(
    'code' => 'sv',
    'name' => 'Svenska',
    'default' => true,
    'locale' => 'sv_SE',
    'url' => '/',
  ),
  array(
    'code' => 'en',
    'name' => 'English',
    'locale' => 'en_US',
    'url' => '/en',
  )
));

// Automatic language detection for the visitor:
c::set('language.detect', true);

// Proper translation of åäö:
str::$ascii = a::merge(str::$ascii, array(
  '/Ä/' => 'A',
  '/æ|ǽ|ä/' => 'a',
  '/œ|ö/' => 'o',
  '/Ö/' => 'O',
  '/ö/' => 'o',
));

// ---------------------------------------------------------
// Authentication configuration
// ---------------------------------------------------------

c::set('roles', array(
  array(
    'id' => 'admin',
    'name' => 'Admin',
    'default' => true,
    'panel' => true
  ),
  array(
    'id' => 'editor',
    'name' => 'Editor',
    'panel' => true
  ),
  array(
    'id' => 'client',
    'name' => 'Client',
    'panel' => false
  )
));
0 Likes

#23

I managed to get it work on a clean Starterkit. Wondering what’s upset in my project… I’ll try to find the bug out of curiosity.

0 Likes

#24

I have been testing some more with a clean Kirby Langkit. And it seems there is a bug here.

Exampel 1: Works as expected, no pages with the template “projects” are shown.

<pre>
      <?php
      $example_1 = $site->index()->filterBy('template', 'projects');
      print_r($site->index()->not($example_1));
      ?>
</pre>

Exampel 2: Works as expected, no pages with the template “project” are shown.

<pre>
      <?php
      $example_2 = $site->index()->filterBy('template', 'project');
      print_r($site->index()->not($example_2));
      ?>
</pre>

Exampel 3: Works not as expected, pages with the template “about” are shown in german but works in english.

<pre>
      <?php
      $example_3 = $site->index()->filterBy('template', 'about');
      print_r($site->index()->not($example_3));
      ?>
</pre>

Exampel 4: Works not as expected, pages with the template “error” are shown in german and in english.

<pre>
      <?php
      $example_4 = $site->index()->filterBy('template', 'error');
      print_r($site->index()->not($example_4));
      ?>
</pre>
0 Likes

#25

Thanks for testing! The reason the last example doesn’t work is because there is no error.php template in the Langkit, you’d have to use intendedTemplate() to exclude that page. Don’t know why the about page doesn’t work, though, because there is an about template, hm.

Edit: Interesting, the German About page uses an URL-Key field. If I remove that field, the page is excluded as expected. But I have still to find out what the URL key has to do with the page template. I think this is a bug. I created an issue on GitHub:

As a workaround, instead of using not(), I suggest you filter your results.

$result = $site->search($query, $settings)->visible()->filterBy('template', 'not in', ['single.database', 'list.database']);

(Additional advantage: less code :wink:)

2 Likes

#26

I can confirm this. The issue is related to the Url-key.

Thanks for the provided snippet. It works like a charm!

Less is more :smiley:

0 Likes