New Kirby Related Pages plugin

The Kirby Related Pages plugin provides a page method that fetches related pages based on the number of matches in a given field. You can set a minimum number of required matches. The result is sorted by number of matches.

Usage

$relatedPages = $page->related($options);

More details in the readme.

You can find the plugin on GitHub: https://github.com/texnixe/kirby-related-pages

I’m well aware of the fact that there is already the RelatedPages plugin by @Adspectus. This one works a bit differently and uses other options.

3 Likes

You say in the description that the plugin fetch pages related to the current page based on matching values in a given field.

Does it work if the given field is a structure field?

No, it’s only intended for fields that contain entries separated by a delimiter. How would you expect that to work, could you give me an example, like search in a field within the structure field, or in all fields of a structure field?

For example, if I have some “project” subpages (parent page is “projects”) for an agency portfolio and in a project there is a structure field to list all the people who were involved in this project.

This structure field has tree fields: name, department, occupation.

In the parent page “Projects” may I fetch all the projects ( = children “project”) which the department = “design” and occupation = “webdesigner”

Maybe I misunderstood how your plugin works, with my bad english sometime it’s happened :wink:

Well, your use case is related, but that’s not what the plugin does. What you can do instead to filter the pages:

$filteredPages = page('projects')->children()->visible()->filter(function($p) {
  $structureField =  $p->structurefield()->toStructure();
  foreach($structureField as $item) {
        if($item->occupation() == 'webdesign' && $item->department() == 'design') return $p;
      }
});

Instead of hardcoding the values, you would need to get them from an URL parameter.

But if you want a little plugin:

<?php
// /site/plugins/methods.php
pages::$methods['filterByStructure'] = function($pages, $field, $options) {
  $filteredPages = $pages->filter(function($p)use($field, $options) {
    $structureField =  $p->$field()->yaml();
    foreach($structureField as $item) {
      if(!array_diff($options, $item)) return $p;
    }
  });
  return $filteredPages;
};

Use like this in your controller/template:

$filteredPages = page('projects')->children()->filterByStructure('fieldName', array('occupation' => 'Webdesign', 'department' => 'Design')));
foreach($filteredPages as $p) {
  echo $p->title();
}

Awesome!
Thank you so much Sonja

This litle plugin is going to be very useful to me :slight_smile:

Hi Sonja (@texnixe )

I was using you wonderful plugin on my personal portfolio site. In the moment I’m rebuilding my whole site with Kirby 3. And i really love it how it works out.
have you planned to update your plugin for kirby 3? Because unfortunately it seems to not working any longer.

I know we have the “related field” now. And I’m using this one as well but what i miss, is the possibility to filter a field for matches how you did it in the related pages plugin.

Yes, I actually wanted to update it but just haven’t done it yet. Should be pretty straightforward, though, so I’ll put it on the list.

3 Likes

That would be fantastic. :pray:

@obear A first version of the updated plugin is now on GitHub. Probably needs some cleaning up and I haven’t tested if it works in a multi-lang environment yet.

I have, however, extended the functionality to work for files as well.

Thank you! :ok_hand:

… Unfortunately i get the following error. Do you have any idea where this can come from?


Whoops\Exception\ErrorException thrown with message "Array to string conversion"

When doing what? Or just when installing the plugin?

This is in my template:


<?php
$relatedPages = $page->related(array(
  'searchField'      => 'colors',
  'matches'          => 2,
  'delimiter'        => ','
  ));
?>


<?= $relatedPages() ?>

And this is my text-file:


Title: Blue Dianne - Cinnebar
----
FarbeBg: #1D4458
----
FarbeAkzent: #EC4C43
----
FarbeSchrift: #FFECEA
----
colors: blue, red, white
----

Hm, I just encountered a problem with the cache not being properly flushed, but that is unrelated.

Could you please provide more information where the error is thrown?

This is the full error message:


Whoops\Exception\ErrorException thrown with message "Array to string conversion"

Stacktrace:

#12 Whoops\Exception\ErrorException in [..]/site/plugins/kirby-related/src/Related.php:92

#11 Whoops\Run:handleError in [..]/site/plugins/kirby-related/src/Related.php:92

#10 Texnixe\Related\Related:getRelated in [..]/site/plugins/kirby-related/index.php:29

#9 Kirby\Cms\Page:{closure} in [internal]:0

#8 Closure:call in [..]/kirby/src/Cms/HasMethods.php:25

#7 Kirby\Cms\Page:callMethod in [..]/kirby/src/Cms/Page.php:168

#6 Kirby\Cms\Page:__call in [..]/site/templates/color.php:50

#5 require in [..]/kirby/src/Toolkit/Tpl.php:39

#4 Kirby\Toolkit\Tpl:load in [..]/kirby/src/Cms/Template.php:164

#3 Kirby\Cms\Template:render in [..]/kirby/src/Cms/Page.php:1105

#2 Kirby\Cms\Page:render in [..]/kirby/src/Cms/App.php:516

#1 Kirby\Cms\App:io in [..]/kirby/src/Cms/App.php:773

#0 Kirby\Cms\App:render in [..]/index.php:5

@obear Thanks a lot, that was a stupid one, just wondering why it didn’t throw an error on my side. Just published release 0.9.2.

1 Like

I have to say thank you. It works like a charm. :smiley:

Glad to hear that.

@obear And thanks for the donation!