Skip to content

Commit

Permalink
Merge pull request #130 from AcclaroInc/feature/custom-twig-regex-fil…
Browse files Browse the repository at this point in the history
…ters

Custom Twig RegEx Filters
  • Loading branch information
sidedwards authored Jan 16, 2021
2 parents a0ce357 + 523b44e commit a2e5a11
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/controllers/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ public function actionConfigurationOptions()

$variables['chkDuplicateEntries'] = Translations::getInstance()->settings->chkDuplicateEntries;
$variables['uploadVolume'] = Translations::getInstance()->settings->uploadVolume;
$variables['twigSearchFilterSingleQuote'] = !empty(Translations::getInstance()->settings->twigSearchFilterSingleQuote) ? Translations::getInstance()->settings->twigSearchFilterSingleQuote : "";
$variables['twigSearchFilterDoubleQuote'] = !empty(Translations::getInstance()->settings->twigSearchFilterDoubleQuote) ? Translations::getInstance()->settings->twigSearchFilterDoubleQuote : "";

$allVolumes = Craft::$app->getVolumes()->getAllVolumes();

Expand Down Expand Up @@ -264,12 +266,14 @@ public function actionSaveConfigurationOptions()
$request = Craft::$app->getRequest();
$duplicateEntries = $request->getParam('chkDuplicateEntries');
$selectedVolume = $request->getParam('uploadVolume');
$twigSearchFilterSingleQuote = $request->getParam('twigSearchFilterSingleQuote');
$twigSearchFilterDoubleQuote = $request->getParam('twigSearchFilterDoubleQuote');

try {

$pluginService = Craft::$app->getPlugins();
$plugin = $pluginService->getPlugin('translations');
if (!$pluginService->savePluginSettings($plugin, ['chkDuplicateEntries' => $duplicateEntries, 'uploadVolume' => $selectedVolume])) {
if (!$pluginService->savePluginSettings($plugin, ['chkDuplicateEntries' => $duplicateEntries, 'uploadVolume' => $selectedVolume, 'twigSearchFilterSingleQuote' => $twigSearchFilterSingleQuote, 'twigSearchFilterDoubleQuote' => $twigSearchFilterDoubleQuote])) {
Craft::$app->getSession()->setError(Translations::$plugin->translator->translate('app', 'Unable to save setting.'));
} else {
Craft::$app->getSession()->setNotice(Translations::$plugin->translator->translate('app', 'Setting saved.'));
Expand Down
4 changes: 4 additions & 0 deletions src/models/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ class Settings extends Model
{
public $chkDuplicateEntries = true;

public $twigSearchFilterSingleQuote = "";

public $twigSearchFilterDoubleQuote = "";

/** @var int The Volume ID where uploads will be saved */
public $uploadVolume = 0;

Expand Down
9 changes: 5 additions & 4 deletions src/services/repository/StaticTranslationsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Exception;
use craft\helpers\FileHelper;
use craft\helpers\ElementHelper;
use acclaro\translations\Translations;
use craft\elements\db\ElementQueryInterface;
use acclaro\translations\elements\StaticTranslations;

Expand Down Expand Up @@ -137,7 +138,8 @@ private function getFileStrings($path, $file, ElementQueryInterface $query, $cat
* @return array
*/
public function getExpressions($ext) {

$twigSearchFilterSingleQuote = !empty(Translations::getInstance()->settings->twigSearchFilterSingleQuote) ? Translations::getInstance()->settings->twigSearchFilterSingleQuote : '/\'((?:[^\']|\\\\\')*)\'\s*\|\s*t(?:ranslate)?\b/';
$twigSearchFilterDoubleQuote = !empty(Translations::getInstance()->settings->twigSearchFilterDoubleQuote) ? Translations::getInstance()->settings->twigSearchFilterDoubleQuote : '/"((?:[^"]|\\\\")*)"\s*\|\s*t(?:ranslate)?\b/';
$exp = [];
switch ($ext) {
case 'php':
Expand All @@ -156,8 +158,7 @@ public function getExpressions($ext) {
$exp = [
'position' => '1',
'regex' => [
'/\'((?:[^\']|\\\\\')*)\'\s*\|\s*t(?:ranslate)?\b/',
'/"((?:[^"]|\\\\")*)"\s*\|\s*t(?:ranslate)?\b/',
$twigSearchFilterSingleQuote, $twigSearchFilterDoubleQuote
]
];
break;
Expand Down Expand Up @@ -205,4 +206,4 @@ public function set($lang, array $fileContent)

return true;
}
}
}
20 changes: 19 additions & 1 deletion src/templates/settings/configuration-options.twig
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,24 @@
toggle: true
}) }}

<h3>{{ "Static Translations"|t('app') }}</h3>

{{ forms.textField({
label: "Twig search filter (single quote)"|t('app'),
id: 'twigSearchFilterSingleQuote',
name: 'twigSearchFilterSingleQuote',
value: twigSearchFilterSingleQuote ?? '',
size: '40',
}) }}

{{ forms.textField({
label: "Twig search filter (double quote)"|t('app'),
id: 'twigSearchFilterDoubleQuote',
name: 'twigSearchFilterDoubleQuote',
value: twigSearchFilterDoubleQuote ?? '',
size: '40',
}) }}

<div class="buttons">
<input type="submit" id="save-configuration" class="btn" value="{{ "Save"|t('app') }}" />
</div>
Expand All @@ -50,4 +68,4 @@
$('#save-configuration').toggleClass('submit');
});
});
{% endjs %}
{% endjs %}

0 comments on commit a2e5a11

Please sign in to comment.