diff --git a/CHANGELOG.md b/CHANGELOG.md index c982f21a..46df5b05 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +### 4.0.16 - 2018-09-10 +### Added +- Added map_source event for mapping custom sources. See the Events section in the README for more. + ## 4.0.15 - 2018-08-31 ### Added - Schematic can now parse environment variables in the schema file directly, without need for an override file diff --git a/README.md b/README.md index 7e14b2dd..5976e0d4 100644 --- a/README.md +++ b/README.md @@ -133,7 +133,7 @@ Then the environment variable `KEY_VALUE` needs to be set. The value of this env Environment variables can also directly be used in the `schema.yml` file. Beware that if you do that, they will be overriden on export by their environment variable's values. -### Hooks +### Events Custom converters can be injected with the `EVENT_RESOLVE_CONVERTER` event. This can be especially useful for importing and exporting custom field types. @@ -147,6 +147,22 @@ Event::on(Schematic::class, Schematic::EVENT_RESOLVE_CONVERTER, function (Conver }); ``` +Custom source mappings can be injected with the `EVENT_MAP_SOURCE` event. +This can be especially useful for importing and exporting custom sources. + +```php +Event::on(Schematic::class, Schematic::EVENT_MAP_SOURCE, function (SourceMappingEvent $event) { + list($sourceType, $sourceFrom) = explode(':', $event->source); + + switch ($sourceType) { + case 'plugin-source': + $event->service = Craft::$app->customService; + $event->method = 'getCustomModelBy'; + break; + } +}); +``` + ### Caveats Schematic uses handles to identify content. When a handle is changed in the schema file and import is run with force a new content type will be created with that handle, and the old content type will be deleted! diff --git a/composer.json b/composer.json index 9f004676..84920d02 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "version": "4.0.15", + "version": "4.0.16", "name": "nerds-and-company/schematic", "description": "Craft setup and sync tool", "type": "craft-plugin", diff --git a/src/Behaviors/SourcesBehavior.php b/src/Behaviors/SourcesBehavior.php index 4ca0b2ea..1282e035 100644 --- a/src/Behaviors/SourcesBehavior.php +++ b/src/Behaviors/SourcesBehavior.php @@ -7,6 +7,7 @@ use yii\base\Behavior; use craft\base\Model; use NerdsAndCompany\Schematic\Schematic; +use NerdsAndCompany\Schematic\Events\SourceMappingEvent; /** * Schematic Sources Behavior. @@ -94,6 +95,7 @@ public function getSource(string $fieldType, string $source = null, string $inde /** @var Model $sourceObject */ $sourceObject = null; + // Get service and method by source list($sourceType, $sourceFrom) = explode(':', $source); switch ($sourceType) { case 'editSite': @@ -144,6 +146,18 @@ public function getSource(string $fieldType, string $source = null, string $inde return $source; } + // Send event + $plugin = Craft::$app->controller->module; + $event = new SourceMappingEvent([ + 'source' => $source, + 'service' => $service ?? null, + 'method' => $method ?? null, + ]); + $plugin->trigger($plugin::EVENT_MAP_SOURCE, $event); + $service = $event->service; + $method = $event->method; + + // Try service and method if (isset($service) && isset($method) && isset($sourceFrom)) { $method = $method.ucfirst($indexFrom); try { diff --git a/src/Controllers/Base.php b/src/Controllers/Base.php index 45101529..5ce0392a 100644 --- a/src/Controllers/Base.php +++ b/src/Controllers/Base.php @@ -53,9 +53,10 @@ protected function getDataTypes(): array $dataTypes = $this->applyExcludes($dataTypes); } - //Import fields again after all sources have been imported + //Import fields and usergroups again after all sources have been imported if (array_search('fields', $dataTypes) && count($dataTypes) > 1) { $dataTypes[] = 'fields'; + $dataTypes[] = 'userGroups'; } return $dataTypes; diff --git a/src/Events/SourceMappingEvent.php b/src/Events/SourceMappingEvent.php new file mode 100644 index 00000000..02c73db4 --- /dev/null +++ b/src/Events/SourceMappingEvent.php @@ -0,0 +1,34 @@ +