Skip to content

Commit b40b6d0

Browse files
committed
Add support for template name change tracking.
1 parent b26c53f commit b40b6d0

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

ProcessGraphQL.module

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?php namespace ProcessWire;
22

33
use Youshido\GraphQL\Execution\Processor;
4+
use ProcessWire\HookEvent;
45
use ProcessWire\GraphQL\Schema;
6+
use Processwire\ProcessGraphQLConfig;
57

68
class ProcessGraphQL extends Process implements Module {
79

@@ -15,7 +17,7 @@ class ProcessGraphQL extends Process implements Module {
1517
'summary' => 'GraphQL for ProcessWire.',
1618
'href' => 'https://github.com/dadish/ProcessGraphql',
1719
'singular' => true,
18-
'autoload' => false,
20+
'autoload' => 'process=ProcessTemplate',
1921
'icon' => 'object-group',
2022
);
2123
}
@@ -25,9 +27,39 @@ class ProcessGraphQL extends Process implements Module {
2527
*/
2628
public function init()
2729
{
30+
$this->addHookAfter('Template::changed', $this, 'hookTemplateNameChange');
2831
require_once $this->config->paths->site . 'modules/ProcessGraphQL/vendor/autoload.php';
2932
}
3033

34+
public function hookTemplateNameChange(HookEvent $event)
35+
{
36+
// require_once $this->config->paths->site . 'modules/ProcessGraphQL/ProcessGraphQLConfig.php';
37+
$whatChanged = $event->arguments[0];
38+
$oldName = $event->arguments[1];
39+
$newName = $event->arguments[2];
40+
41+
// do nothing if name has not changed
42+
if ($whatChanged !== 'name') return;
43+
44+
// do nothing if template is not in legalTemplates
45+
if (!in_array($oldName, $this->legalTemplates)) return;
46+
47+
// remove the oldName from the legalTemplates
48+
$index = array_search($oldName, $this->legalTemplates);
49+
$legalTemplates = array_slice($this->legalTemplates, 0);
50+
array_splice($legalTemplates, $index, 1);
51+
$this->message("Removed `$oldName` from the legalTemplates.");
52+
53+
// if newName is compatible with the module then add it into legalTemplates
54+
if (ProcessGraphQLConfig::isLegalTemplateName($newName)) {
55+
$legalTemplates[] = $newName;
56+
$this->message("Added `$newName` into legalTemplaes.");
57+
}
58+
59+
// make sure to remember changes!
60+
$this->modules->saveConfig($this, 'legalTemplates', $legalTemplates);
61+
}
62+
3163
public function ___execute()
3264
{
3365
if ($this->config->ajax) return $this->executeGraphQL();

ProcessGraphQLConfig.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function getDefaults()
2828
'httpUrl',
2929
'description',
3030
],
31-
'fullWidthGraphiql' => false,
31+
'fullWidthGraphiQL' => false,
3232
);
3333
}
3434

@@ -37,6 +37,7 @@ public static function isLegalTemplateName($name)
3737
if (!$name) return false;
3838
if (preg_match('/^[_A-Za-z][-_0-9A-Za-z]*$/', $name) !== 1) return false; // the GraphQL naming requirement
3939
if (strpos($name, '__') === 0) return false; // the names with `__` prefix are reserved by GraphQL
40+
if (strpos($name, '--') === 0) return false; // as we change the `-` symbols to `_` for field names the `--` becomes `__` and it also reserved by GraphQL
4041
if (in_array($name, Settings::getReservedWords())) return false; // some words that used now and might be for future
4142
return true;
4243
}

0 commit comments

Comments
 (0)