# Module plugin error invalid argument

**URL:** https://forum.getkirby.com/t/module-plugin-error-invalid-argument/5789
**Category:** Questions
**Tags:** v2
**Created:** [November 18, 2016, 11:19am UTC](https://forum.getkirby.com/t/module-plugin-error-invalid-argument/5789 "2016-11-18T11:19:23Z")
**Posts on this page:** 19
**Page:** 1

<div class="post-metadata">

### Author: ![TakioTk](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/takiotk/32/3133_2.png) [@TakioTk](https://forum.getkirby.com/u/TakioTk)
#### Post date: [November 18, 2016, 11:19am UTC](https://forum.getkirby.com/t/module-plugin-error-invalid-argument/5789/1 "2016-11-18T11:19:23Z")

</div>

Hi,  
first of all I like so much the module and the pattern plugin. It’s such a good thing to work with!

With the module plugin I get an error:

**Warning: Invalid argument supplied for foreach() in …**

my code seems easy:

```
if($page->hasModules()) {
	foreach($page->modules() as $module) {
		echo $module;
	}
}

```

Am I blind, It must be something stupid I can’t see?!

The module is displayed as expected.

But the module appears too in the menu?

---

<div class="post-metadata">

### Author: ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)
#### Post date: [November 18, 2016, 12:38pm UTC](https://forum.getkirby.com/t/module-plugin-error-invalid-argument/5789/2 "2016-11-18T12:38:30Z")

</div>

`$page->modules()` collects all modules and returns their HTML, so the foreach loop is unnecessary/results in an error, [see the readme](https://github.com/getkirby-plugins/modules-plugin).

---

<div class="post-metadata">

### Author: ![TakioTk](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/takiotk/32/3133_2.png) [@TakioTk](https://forum.getkirby.com/u/TakioTk)
#### Post date: [November 18, 2016, 1:05pm UTC](https://forum.getkirby.com/t/module-plugin-error-invalid-argument/5789/3 "2016-11-18T13:05:37Z")

</div>

Yes you are right!

But how to prevent the modules from displaying in the menu?

---

<div class="post-metadata">

### Author: ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)
#### Post date: [November 18, 2016, 1:10pm UTC](https://forum.getkirby.com/t/module-plugin-error-invalid-argument/5789/4 "2016-11-18T13:10:09Z")

</div>

Easiest way is to make the module pages invisible and only pull visible pages in your menu. But it all depends on your menu code.

---

<div class="post-metadata">

### Author: ![TakioTk](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/takiotk/32/3133_2.png) [@TakioTk](https://forum.getkirby.com/u/TakioTk)
#### Post date: [November 18, 2016, 1:14pm UTC](https://forum.getkirby.com/t/module-plugin-error-invalid-argument/5789/5 "2016-11-18T13:14:46Z")

</div>

I did, but then the module also disapeared.

my code is just this

```
if($page->hasModules()) {
 echo $page->modules();
}

```

I’am not asking for the visible ones in the code

---

<div class="post-metadata">

### Author: ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)
#### Post date: [November 18, 2016, 1:18pm UTC](https://forum.getkirby.com/t/module-plugin-error-invalid-argument/5789/6 "2016-11-18T13:18:07Z")

</div>

Ok, the module code filters by visible modules, so they should be visible. Then you need to change your menu code, you can use the `not()` method with a collection of pages to exclude from the menu.

---

<div class="post-metadata">

### Author: ![TakioTk](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/takiotk/32/3133_2.png) [@TakioTk](https://forum.getkirby.com/u/TakioTk)
#### Post date: [November 18, 2016, 1:22pm UTC](https://forum.getkirby.com/t/module-plugin-error-invalid-argument/5789/7 "2016-11-18T13:22:48Z")

</div>

Ahh, I was understanding the idea of modules more as parts of a page and why should they then be visible in the menu?!

**Is there no way to see if a item is a module?**

---

<div class="post-metadata">

### Author: ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)
#### Post date: [November 18, 2016, 1:26pm UTC](https://forum.getkirby.com/t/module-plugin-error-invalid-argument/5789/8 "2016-11-18T13:26:57Z")

</div>

No, to Kirby, modules are subpages just like any other subpage.

---

<div class="post-metadata">

### Author: ![lukasbestle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lukasbestle/32/11437_2.png) [@lukasbestle](https://forum.getkirby.com/u/lukasbestle)
#### Post date: [November 18, 2016, 5:15pm UTC](https://forum.getkirby.com/t/module-plugin-error-invalid-argument/5789/9 "2016-11-18T17:15:39Z")

</div>

You can filter by template (all module templates start with `module.`).

But even easier: You can use the [second, recommended approach](https://github.com/getkirby-plugins/modules-plugin#creating-modular-content-in-the-panel) and create a subpage called `modules` for each page where you need modules. This `modules` page can be invisible. Only the individual modules inside that directory need to be visible, but because of the hidden parent they won’t appear in menus.

BTW: Thanks for the feedback regarding “easily detecting if a page is a module”, I have [noted the idea for the next version](https://github.com/getkirby-plugins/modules-plugin/issues/9).

---

<div class="post-metadata">

### Author: ![TakioTk](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/takiotk/32/3133_2.png) [@TakioTk](https://forum.getkirby.com/u/TakioTk)
#### Post date: [November 18, 2016, 5:45pm UTC](https://forum.getkirby.com/t/module-plugin-error-invalid-argument/5789/10 "2016-11-18T17:45:10Z")

</div>

Ohh yes that would be great!

I have it like this now:  
in module -\> text.yml

```
parts:
    label: Module
    type: select
    text: module
    default: module
    options:
      module: Modul
      subpage: Unterseite

```

and in the menu:

`if($child->parts() != 'module' ):`

---

<div class="post-metadata">

### Author: ![lukasbestle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lukasbestle/32/11437_2.png) [@lukasbestle](https://forum.getkirby.com/u/lukasbestle)
#### Post date: [November 18, 2016, 7:40pm UTC](https://forum.getkirby.com/t/module-plugin-error-invalid-argument/5789/11 "2016-11-18T19:40:49Z")

</div>

I wouldn’t recommend this, manual options are always a bit error-prone. Consider using something like this instead:

```auto
if(!str::startsWith($child->template(), 'module.'):

```

Even better would be to filter the collection before the foreach loop, but the simple if is easier for you to drop in.

---

<div class="post-metadata">

### Author: ![TakioTk](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/takiotk/32/3133_2.png) [@TakioTk](https://forum.getkirby.com/u/TakioTk)
#### Post date: [November 18, 2016, 7:46pm UTC](https://forum.getkirby.com/t/module-plugin-error-invalid-argument/5789/12 "2016-11-18T19:46:07Z")

</div>

> [@lukasbestle](#):
>
> if(!str::startsWith($child-\>template(), ‘module.’):

Thank you, that works pretty!

---

<div class="post-metadata">

### Author: ![TakioTk](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/takiotk/32/3133_2.png) [@TakioTk](https://forum.getkirby.com/u/TakioTk)
#### Post date: [November 23, 2016, 12:12pm UTC](https://forum.getkirby.com/t/module-plugin-error-invalid-argument/5789/13 "2016-11-23T12:12:33Z")

</div>

Hi @lukasbestle,  
did you mean something like this:

```
// get all children for the current menu item accept modules
$children = $item->children()->visible()->filter(function($child) {
  if(!str::startsWith($child->template(), 'module.')) {
  return $child;
  }
});
```

---

<div class="post-metadata">

### Author: ![lukasbestle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lukasbestle/32/11437_2.png) [@lukasbestle](https://forum.getkirby.com/u/lukasbestle)
#### Post date: [November 23, 2016, 12:51pm UTC](https://forum.getkirby.com/t/module-plugin-error-invalid-argument/5789/14 "2016-11-23T12:51:42Z")

</div>

Yeah, but even simpler:

```auto
// get all children for the current menu item except modules
$children = $item->children()->visible()->filter(function($child) {
  return !str::startsWith($child->template(), 'module.');
});

```

The filter function only needs to return true or false, so there is no need for the if.

---

<div class="post-metadata">

### Author: ![TakioTk](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/takiotk/32/3133_2.png) [@TakioTk](https://forum.getkirby.com/u/TakioTk)
#### Post date: [November 24, 2016, 6:22pm UTC](https://forum.getkirby.com/t/module-plugin-error-invalid-argument/5789/15 "2016-11-24T18:22:18Z")

</div>

thank you, that’s even better!

---

<div class="post-metadata">

### Author: ![TakioTk](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/takiotk/32/3133_2.png) [@TakioTk](https://forum.getkirby.com/u/TakioTk)
#### Post date: [November 24, 2016, 6:54pm UTC](https://forum.getkirby.com/t/module-plugin-error-invalid-argument/5789/16 "2016-11-24T18:54:40Z")

</div>

Hi @lukasbestle,  
if I want to put my modules inside patterns, where do I have to change the root path?

```
kirby()->roots()->site() . DS . 'patterns' . DS . 'modules'

```

Ahh, I got it. Everything is okay and again GREAT WORK!

---

<div class="post-metadata">

### Author: ![lukasbestle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lukasbestle/32/11437_2.png) [@lukasbestle](https://forum.getkirby.com/u/lukasbestle)
#### Post date: [November 24, 2016, 7:00pm UTC](https://forum.getkirby.com/t/module-plugin-error-invalid-argument/5789/17 "2016-11-24T19:00:04Z")

</div>

For reference in case someone else has the same issue: There’s a [modules.directory](https://github.com/getkirby-plugins/modules-plugin#configuration) option.

---

<div class="post-metadata">

### Author: ![TakioTk](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/takiotk/32/3133_2.png) [@TakioTk](https://forum.getkirby.com/u/TakioTk)
#### Post date: [December 1, 2016, 7:59pm UTC](https://forum.getkirby.com/t/module-plugin-error-invalid-argument/5789/18 "2016-12-01T19:59:57Z")

</div>

> [@lukasbestle](#):
>
> second, recommended approach

just to end this post. In the end I used the second, recommende aproach and put all modules inside a invisible subpage named modules. This is the clean and easy way.

Thanks for great support and this great plugin 😀

---

<div class="post-metadata">

### Author: ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)
#### Post date: [February 6, 2025, 4:54pm UTC](https://forum.getkirby.com/t/module-plugin-error-invalid-argument/5789/19 "2025-02-06T16:54:39Z")

</div>


