Kirby Modules Plugin (Kirby 2)

Ok, then two options:

$image = $module->bgimage()->toFile();
echo $image->url():

or

$image = $module->image($module->bgimage());
echo $image->url()

both gives me : Fatal error: Call to a member function url() on a non-object :crying_cat_face:

This one looks weird but gives me the correct URL:

$image = $module->url()."/".$module->bgimage();
echo $image;

strange. have to use this i think. thank you for the input!

greetings,
sven

That’s weird, there should be another solution.

1 Like

That’s very strange. Actually Sonja’s example should work, $module is a simple page object. Which Kirby version are you using?

my version is 2.3.2.

Thanks, I will do some testing in the next few days.

1 Like

I have tested with your setup and can’t reproduce the issue. The “non-object” error generally occurs if the image does not actually exist. Have you verified that your module has an image with that file name (attention: not your displayed page, but your module page)?

Hello,

Just to let you know that there is a bug when updating programmatically a structure field in Kirby if the modules plugin and at least one module is present.

I have opened an issue with more details and code example on your Github repository https://github.com/getkirby-plugins/modules-plugin/issues/11

Modules Plugin v1.3.0

May I present: A brand-new release of the Modules plugin. :tada:
This time:

You can download the plugin here. Please let me know if there are any issues.

1 Like

10 posts were split to a new topic: Modules plugin: Structure fields

Hello,

i have a problem exporting a site made with modules as a static page.
How can i set the right URL for images inside modules ‘by hand’?

Look here for some more explainations: StaticBuilder - Kirby as a static site generator

Short: how can i get a URL from a image field like the (image:) tag does?

Also this gives a correct URL:

$module->images()->first()->url()

But this:

$module->image('element-33.png')->url()

gives: Call to a member function url() on a non-object

How i get a URL like the one below from a image field (image field gives just the filename… the file is in the module dir)?

http://localhost/kirby/site/content/3-products/columns-1p79fng/element-33.png

S.

Hello,

how could a “search controller” can look like for pages made with the modules plugin?
The normal search:

return function($site, $pages, $page) {

      $query   = get('q');
      $results = $site->search($query, 'title|text');
      $total   = $results->count();
      $results = $results->paginate(20);

      return array(
        'query'      => $query,
        'results'    => $results,
        'total'      => $total,
        'pagination' => $results->pagination()
      );
    };

… it should search in modules and if there is a hit inside a module the result should be the parent page.

Any ideas how to do this?

S.

Sorry for the huge delay, I forgot about your other two posts! :frowning:

I just tried to reproduce this with the Modules plugin 1.3.0 and Kirby 2.4.1, but I couldn’t. Using the image and file Kirbytags inside a structure field inside a module page works. Did you make sure that you are using the toStructure() method in all places where you previously used the yaml() method?

It looks like this has been solved in the StaticBuilder topic. If not, please let me know.

That’s strange as module pages are just ordinary page objects. Have you verified that the filename is correct?

In your search result template, you can check if the page is a module and then output the parent page accordingly:

<?php foreach($results as $result): ?>
  <?php if($result->isModule()) $result = $result->page(); ?>
  <a href="<?= $result->url() ?>"><?= $result->title() ?></a>
<?php endforeach; ?>

This solution will not only use the URL of the parent page but also the title if the result is a module.

1 Like

This might seem like a good idea for some sites, but most of the time it leads to multiple instances of the same page in the search results. You will probably have to check for duplicates somehow. In my projects I use the following code, but maybe somebody has a more elegant solution:


<?php
  $noDuplicates = array();
  foreach($results as $result):
  if($result->isModule()) $result = $result->page(); ?>
  if(in_array($result, $noDuplicates)) continue;
  $noDuplicates[] = $result;
?>
  <a href="<?= $result->url() ?>"><?= $result->title() ?></a>
<?php endforeach; ?>
1 Like

That’s a good point! However your solution will lead to issues with pagination and the total count of results.

Let’s try to implement our two ideas a bit more elegantly in the controller:

<?php

return function($site, $pages, $page) {
  $query   = get('q');
  $results = $site->search($query, 'title|text');
  
  $results = $results->map(function($result) {
    if($result->isModule()) {
      return $result->page();
    } else {
      return $result;
    }
  });
  $results->data = array_unique($results->data);
  
  $total   = $results->count();
  $results = $results->paginate(20);

  return array(
    'query'      => $query,
    'results'    => $results,
    'total'      => $total,
    'pagination' => $results->pagination()
  );
};

I haven’t tested this (especially the array_unique() part), but it is worth a try. :wink:
With the controller, the template can be clean again.

2 Likes

hello,

how do i use the ‘data’ here?

modules($page, $data = [], $return = false);

Is it to pass just additional data or can i overwrite existing data (fields) with this?

S.

As the documentation says, this can be used to pass additional data to the module, you can’t overwrite existing data (same as with snippets).

Hey @lukasbestle how does this plugin work in the panel? Is it possible to build pages using a combination of modules right from a panel?

For example, I want to build an article page that can have different image layouts. Maybe it starts with a full widith image and then a grid and then 2 images size by side. Each article would be different.

Hope this makes sense.

@iam_timm: Yes, you can define a set of modules to be used in your article in your article blueprint. For each part, e.g… two images side by side, you would define a separate module. You could sort your modules freely. You can combine the modules plugin with the Kirby Sortable plugin which allows you to sort your modules as you like.

An alternative to using modules, with everything in the same page, would be Tim Oettings builder plugin, which is an extension of the structure field (with the difference that each item can have different fields, whereas the structure field always contains the same set of fields).

Ahh the Page Builder plugin looks like exactly what I need. Thanks so much for suggesting it. The article page will be the only page that will allow customisation like this.