Skip to content

Commit

Permalink
Api: Add support for multi-language urls
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Schendel committed Apr 29, 2022
1 parent 260e547 commit af929e4
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 13 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ We now have created a basic Twack-component and you now the general concepts how

## Changelog

### Changes in 2.2.4 (2022-04-29)

- Api: Added support for Multi-Language URLS

### Changes in 2.2.3 (2022-03-08)

* getAjaxOf: Use AppApi-function if installed
Expand Down
2 changes: 1 addition & 1 deletion Twack.module.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static function getModuleInfo() {
return [
'title' => 'Twack',
'author' => 'Sebastian Schendel',
'version' => '2.2.3',
'version' => '2.2.4',
'summary' => 'Reusable components for your ProcessWire-templates.',
'singular' => true,
'autoload' => true,
Expand Down
51 changes: 39 additions & 12 deletions TwackApiAccess.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,62 @@ class TwackApiAccess {
public static function pageIDRequest($data) {
$data = AppApiHelper::checkAndSanitizeRequiredParameters($data, ['id|int']);
$page = wire('pages')->get('id=' . $data->id);
return self::pageRequest($page);
return self::pageRequest($page, '');
}

public static function dashboardRequest() {
$page = wire('pages')->get('/');
return self::pageRequest($page);
return self::pageRequest($page, '');
}

public static function pagePathRequest($data) {
$data = AppApiHelper::checkAndSanitizeRequiredParameters($data, ['path|pagePathName']);
$page = wire('pages')->get('/' . $data->path);
return self::pageRequest($page);
$path = '/' . trim($data->path, '/') . '/';
$page = wire('pages')->get('path="' . $path . '"');

if (!$page->id && wire('modules')->isInstalled('LanguageSupport')) {
// Check if its a root path
$rootPage = wire('pages')->get('/');
foreach ($rootPage->urls as $key => $value) {
if ($value !== $path) {
continue;
}
return self::pageRequest($rootPage, $key);
}
}

$info = wire('pages')->pathFinder()->get($path);
if (!empty($info['language']['name'])) {
return self::pageRequest($page, $info['language']['name']);
}

return self::pageRequest($page, '');
}

protected static function pageRequest(Page $page) {
protected static function pageRequest(Page $page, $languageFromPath) {
if (!wire('modules')->isInstalled('Twack')) {
throw new InternalServererrorException('Twack module not found.');
}
wire('twack')->enableAjaxResponse();

if (wire('modules')->isInstalled('LanguageSupport')) {
$lang = '' . strtolower(wire('input')->get->pageName('lang'));
$langAlt = SELF::getLanguageCode($lang);
if (!empty($lang) && wire('languages')->get($lang) instanceof Page && wire('languages')->get($lang)->id) {
wire('user')->language = wire('languages')->get($lang);
} elseif (!empty($langAlt) && wire('languages')->get($langAlt) instanceof Page && wire('languages')->get($langAlt)->id) {
wire('user')->language = wire('languages')->get($langAlt);
if (!empty($languageFromPath) && wire('languages')->get($languageFromPath) instanceof Page && wire('languages')->get($languageFromPath)->id) {
wire('user')->language = wire('languages')->get($languageFromPath);
} else {
wire('user')->language = wire('languages')->getDefault();
$lang = '' . strtolower(wire('input')->get->pageName('lang'));
$langAlt = SELF::getLanguageCode($lang);

if (!empty($lang) && wire('languages')->get($lang) instanceof Page && wire('languages')->get($lang)->id) {
wire('user')->language = wire('languages')->get($lang);
} elseif (!empty($langAlt) && wire('languages')->get($langAlt) instanceof Page && wire('languages')->get($langAlt)->id) {
wire('user')->language = wire('languages')->get($langAlt);
} else {
wire('user')->language = wire('languages')->getDefault();
}
}

if (!$page->viewable(wire('user')->language)) {
throw new ForbiddenException();
}
}

Expand Down

0 comments on commit af929e4

Please sign in to comment.