Skip to content

Commit f1f89cf

Browse files
committed
Initial commit
0 parents  commit f1f89cf

File tree

4 files changed

+229
-0
lines changed

4 files changed

+229
-0
lines changed

ExampleModule.php

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
<?php
2+
3+
/**
4+
* Example module.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace ExampleNamespace;
10+
11+
use Fisharebest\Localization\Translation;
12+
use Fisharebest\Webtrees\I18N;
13+
use Fisharebest\Webtrees\Module\AbstractModule;
14+
use Fisharebest\Webtrees\Module\ModuleCustomInterface;
15+
use Fisharebest\Webtrees\Module\ModuleCustomTrait;
16+
17+
/**
18+
* Class ExampleModule
19+
*
20+
* This example shows how to create a custom module.
21+
* All the functions are optional. Just implement the ones you need.
22+
*
23+
* Modules *must* implement ModuleCustomInterface. They *may* also implement other interfaces.
24+
*/
25+
class ExampleModule extends AbstractModule implements ModuleCustomInterface
26+
{
27+
// For every module interface that is implemented, the corresponding trait *should* also use be used.
28+
use ModuleCustomTrait;
29+
30+
/**
31+
* The constructor is called on all modules, even ones that are disabled.
32+
* Note that you cannot rely on other modules (such as languages) here, as they may not yet exist.
33+
*
34+
*/
35+
public function __construct()
36+
{
37+
}
38+
39+
/**
40+
* Bootstrap. This function is called on *enabled* modules.
41+
* It is a good place to register routes and views.
42+
* Note that it is only called on genealogy pages - not on admin pages.
43+
*
44+
* @return void
45+
*/
46+
public function boot(): void
47+
{
48+
}
49+
50+
/**
51+
* How should this module be identified in the control panel, etc.?
52+
*
53+
* @return string
54+
*/
55+
public function title(): string
56+
{
57+
return I18N::translate('Example module');
58+
}
59+
60+
/**
61+
* A sentence describing what this module does.
62+
*
63+
* @return string
64+
*/
65+
public function description(): string
66+
{
67+
return I18N::translate('This module does not do anything');
68+
}
69+
70+
/**
71+
* The person or organisation who created this module.
72+
*
73+
* @return string
74+
*/
75+
public function customModuleAuthorName(): string
76+
{
77+
return 'Greg Roach';
78+
}
79+
80+
/**
81+
* The version of this module.
82+
*
83+
* @return string
84+
*/
85+
public function customModuleVersion(): string
86+
{
87+
return '1.0.0';
88+
}
89+
90+
/**
91+
* A URL that will provide the latest version of this module.
92+
*
93+
* @return string
94+
*/
95+
public function customModuleLatestVersionUrl(): string
96+
{
97+
return 'https://github.com/webtrees/example-module/raw/main/latest-version.txt';
98+
}
99+
100+
/**
101+
* Where to get support for this module. Perhaps a github repository?
102+
*
103+
* @return string
104+
*/
105+
public function customModuleSupportUrl(): string
106+
{
107+
return 'https://github.com/webtrees/example-module';
108+
}
109+
110+
/**
111+
* Additional/updated translations.
112+
*
113+
* @param string $language
114+
*
115+
* @return array<string>
116+
*/
117+
public function customTranslations(string $language): array
118+
{
119+
switch ($language) {
120+
case 'en-AU':
121+
case 'en-GB':
122+
case 'en-US':
123+
// Note the special characters used in plural and context-sensitive translations.
124+
return [
125+
'Individual' => 'Fish',
126+
'Individuals' => 'Fishes',
127+
'%s individual' . I18N::PLURAL . '%s individuals' => '%s fish' . I18N::PLURAL . '%s fishes',
128+
'Unknown given name' . I18N::CONTEXT . '' => '?fish?',
129+
'Unknown surname' . I18N::CONTEXT . '' => '?FISH?',
130+
];
131+
132+
case 'fr':
133+
case 'fr-CA':
134+
return [
135+
// These are new translations:
136+
'Example module' => 'Exemple module',
137+
'This module does not do anything' => 'Ce module ne fait rien',
138+
// These are updates to existing translations:
139+
'Individual' => 'Poisson',
140+
'Individuals' => 'Poissons',
141+
'%s individual' . I18N::PLURAL . '%s individuals' => '%s poisson' . I18N::PLURAL . '%s poissons',
142+
'Unknown given name' . I18N::CONTEXT . '' => '?poission?',
143+
'Unknown surname' . I18N::CONTEXT . '' => '?POISSON?',
144+
];
145+
146+
case 'some-other-language':
147+
// Arrays are preferred, and are faster.
148+
// If your module uses .MO files, then you can convert them to arrays like this.
149+
return (new Translation('path/to/file.mo'))->asArray();
150+
151+
default:
152+
return [];
153+
}
154+
}
155+
}

README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# EXAMPLE MODULE
2+
3+
This example shows how to create a custom module.
4+
5+
All the functions are optional - edit the ones you need, and
6+
delete those that you do not need.
7+
8+
For example, the functions regarding versions, author, support,
9+
etc. are only relevant for modules that you intend to share publicly.
10+
11+
This module does not actually do anything. Look at the other examples to
12+
see how to add or change specific features.
13+
14+
## How to extend/modify an existing modules
15+
16+
This example creates a new module from scratch (by extending `AbstractModule`).
17+
18+
Instead, you can extend an existing module. For example:
19+
20+
```php
21+
<?php
22+
use Fisharebest\Webtrees\Webtrees;
23+
use Fisharebest\Webtrees\Module\ModuleCustomInterface;
24+
use Fisharebest\Webtrees\Module\ModuleCustomTrait;
25+
use Fisharebest\Webtrees\Module\PedigreeChartModule;
26+
27+
/**
28+
* Creating an anonymous class will prevent conflicts with other custom modules.
29+
*/
30+
class ExampleModule extends PedigreeChartModule implements ModuleCustomInterface {
31+
use ModuleCustomTrait;
32+
33+
/**
34+
* @return string
35+
*/
36+
public function description(): string
37+
{
38+
return 'A modified version of the pedigree chart';
39+
}
40+
41+
// Change the default layout...
42+
protected const DEFAULT_STYLE = self::STYLE_DOWN;
43+
44+
protected const DEFAULT_PARAMETERS = [
45+
'generations' => self::DEFAULT_GENERATIONS,
46+
'style' => self::DEFAULT_STYLE,
47+
];
48+
};
49+
50+
// The PedigreeChartModule constructor takes some parameters,
51+
// so we tell webtrees to make them for us.
52+
return Webtrees::make(ExampleModule::class);
53+
```

latest-version.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1.0.0

module.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/**
4+
* Example module.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace ExampleNamespace;
10+
11+
require __DIR__ . '/ExampleModule.php';
12+
13+
// This script must return an object that implements ModuleCustomInterface.
14+
// If the module's constructor does not take any parameters, you can simply instantiate it.
15+
//
16+
// If you are using dependency-injection in your module, you would ask webtrees to make the object for you.
17+
// return Webtrees::make(ExampleModule::class);
18+
// For an example, see the server-config module.
19+
20+
return new ExampleModule();

0 commit comments

Comments
 (0)