Skip to content
This repository was archived by the owner on Jan 25, 2021. It is now read-only.

Commit b1ca5aa

Browse files
committed
Add helper methods
1 parent 3b9de02 commit b1ca5aa

File tree

6 files changed

+101
-15
lines changed

6 files changed

+101
-15
lines changed

etc/template.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
<?php
22

3-
namespace Kirby\Modules;
4-
53
// Redirect to the page where the module appears
6-
if($page->parent()->uid() === Modules::parentUid()) {
7-
go($page->parent()->parent());
8-
} else {
9-
go($page->parent());
10-
}
4+
go($page->page());

lib/modulepage.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Kirby\Modules;
4+
5+
// Kirby dependencies
6+
use Page;
7+
8+
/**
9+
* ModulePage
10+
*
11+
* @package Kirby Modules Plugin
12+
* @author Lukas Bestle <[email protected]>
13+
* @license MIT
14+
*/
15+
class ModulePage extends Page {
16+
/**
17+
* Returns the page where the module appears
18+
*
19+
* @return Page
20+
*/
21+
public function page() {
22+
if($this->parent()->uid() === Modules::parentUid()) {
23+
return $this->parent()->parent();
24+
} else {
25+
return $this->parent();
26+
}
27+
}
28+
29+
/**
30+
* Returns the module object
31+
*
32+
* @return Module
33+
*/
34+
public function module() {
35+
return new Module($this);
36+
}
37+
}

lib/modules.php

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function modules() {
8888
}
8989

9090
/**
91-
* Registers the page method and all blueprints within Kirby
91+
* Registers the page methods, page models and all blueprints within Kirby
9292
* Called only once when the plugin is loaded
9393
*/
9494
public static function register() {
@@ -98,10 +98,33 @@ public static function register() {
9898
// Calling it will call the modules() helper
9999
$kirby->set('page::method', 'modules', 'modules');
100100

101-
// Register blueprints and dummy templates for all modules
101+
// Register $page->moduleList() method
102+
$kirby->set('page::method', 'moduleList', function($page) {
103+
$modules = new static($page);
104+
return $modules->modules();
105+
});
106+
107+
// Register $page->moduleCount($module) method
108+
$kirby->set('page::method', 'moduleCount', function($page, $module = null) {
109+
$moduleList = $page->moduleList();
110+
if($module) {
111+
$module = new Module($module);
112+
$moduleList = $moduleList->filterBy('intendedTemplate', $module->template());
113+
}
114+
115+
return $moduleList->count();
116+
});
117+
118+
// Register $page->hasModules($module) method
119+
$kirby->set('page::method', 'hasModules', function($page, $module = null) {
120+
return $page->moduleCount($module) > 0;
121+
});
122+
123+
// Register blueprints, page models and dummy templates for all modules
102124
foreach(static::allModules() as $module) {
103-
$kirby->set('blueprint', $module->template(), $module->blueprintFile());
104-
$kirby->set('template', $module->template(), dirname(__DIR__) . DS . 'etc' . DS . 'template.php');
125+
$kirby->set('blueprint', $module->template(), $module->blueprintFile());
126+
$kirby->set('page::model', $module->template(), 'kirby\\modules\\modulepage');
127+
$kirby->set('template', $module->template(), dirname(__DIR__) . DS . 'etc' . DS . 'template.php');
105128
}
106129
}
107130

modules.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88

99
// Define autoloader
1010
load(array(
11-
'kirby\\modules\\modules' => __DIR__ . DS . 'lib' . DS . 'modules.php',
12-
'kirby\\modules\\module' => __DIR__ . DS . 'lib' . DS . 'module.php',
11+
'kirby\\modules\\modules' => __DIR__ . DS . 'lib' . DS . 'modules.php',
12+
'kirby\\modules\\module' => __DIR__ . DS . 'lib' . DS . 'module.php',
13+
'kirby\\modules\\modulepage' => __DIR__ . DS . 'lib' . DS . 'modulepage.php',
1314
));
1415

15-
// Register page method and blueprints
16+
// Register page methods, blueprints and page models
1617
Kirby\Modules\Modules::register();
1718

1819
/**

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
"description": "Kirby Modules Plugin",
44
"author": "Lukas Bestle <[email protected]>",
55
"license": "MIT",
6-
"version": "1.0.1",
6+
"version": "1.1.0",
77
"type": "kirby-plugin"
88
}

readme.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,37 @@ c::set('modules.parent.uid', 'modules');
131131
c::set('modules.template.prefix', 'module.');
132132
```
133133

134+
## Helper methods
135+
136+
Besides the main `$page->modules()` method that is used to output the module snippets, there are also a few other helper methods you can use:
137+
138+
### `$page->moduleList()`
139+
140+
Returns an array of the module pages for the given page.
141+
142+
### `$page->moduleCount($type)`
143+
144+
Returns the number of modules. If `$type` is given, returns the number of modules of that type.
145+
146+
### `$page->hasModules($type)`
147+
148+
Returns whether the page has any modules. If `$type` is given, returns whether the page has modules of that type.
149+
150+
### `$module->page()`
151+
152+
Returns the page where the module appears. Depending on your setup, it's either the parent page or the grandparent page.
153+
154+
### `$module->module()`
155+
156+
Returns the module object. You can use it to get more information about the module:
157+
158+
```
159+
var_dump($module->module()->name()); // Name of the module
160+
var_dump($module->module()->template()); // Template name of the module
161+
```
162+
163+
There are also a few other values of the module object, see `lib/module.php`.
164+
134165
## Using together with the Patterns plugin
135166

136167
Since the plugin only requires the modules to have a snippet and a blueprint, modules can be stored inside the `site/patterns` directory if you use the [Patterns plugin](https://github.com/getkirby-plugins/patterns-plugin). This is useful if you want to present the different modules in the Patterns interface.

0 commit comments

Comments
 (0)