Skip to content

Commit

Permalink
Added new option Composer::$hideDefaultPrefixOnly (#1834)
Browse files Browse the repository at this point in the history
* Add `hideDefaultPrefixOnly` option

When enabled, composition prefixes will be hidden only for default language. Takes effect only when `hidden` option is disabled.

* Add Changelog record for `Composer::$hideDefaultPrefixOnly`

* Update app-translation.md

* Update app-translation.md

* Add `hideDefaultPrefixOnly` option

When enabled, composition prefixes will be hidden only for default language. Takes effect only when `hidden` option is disabled.

* Add Changelog record for `Composer::$hideDefaultPrefixOnly`

* add unit test

* Update CHANGELOG.md
  • Loading branch information
WEBKADABRA authored and nadar committed Jul 18, 2018
1 parent 6960cee commit d3e1cc8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
1 change: 1 addition & 0 deletions core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ All notable changes to this project will be documented in this file. This projec
### Added

+ [#174](https://github.com/luyadev/luya-module-admin/issues/174) Added new option $apiRules in order to provide custom url rules for APIS.
+ [#1834](https://github.com/luyadev/luya/pull/1834) Added new option `Composer::$hideDefaultPrefixOnly`. When enabled, composition prefixes will be hidden only for default language. Takes effect only when `hidden` option is disabled.

### Fixed

Expand Down
10 changes: 8 additions & 2 deletions core/web/Composition.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ class Composition extends Component implements \ArrayAccess
*/
public $hidden = true;

/**
* @var boolean Disable composition prefixes in URLs only for default language. Takes effect only when `hidden` option is disabled.
* @since 1.0.10
*/
public $hideDefaultPrefixOnly = false;

/**
* @var string Url matching prefix, which is used for all the modules (e.g. an e-store requireds a language
* as the cms needs this informations too). After proccessing this informations, they will be removed
Expand Down Expand Up @@ -240,9 +246,9 @@ public function getPrefixPath()
*/
public function createRouteEnsure(array $overrideKeys = [])
{
return $this->hidden ? '' : $this->createRoute($overrideKeys);
return $this->hidden || (!$this->hidden && $this->langShortCode == $this->defaultLangShortCode && $this->hideDefaultPrefixOnly) ? '' : $this->createRoute($overrideKeys);
}

/**
* Create compositon route based on the provided keys (to override), if no keys provided
* all the default values will be used.
Expand Down
24 changes: 24 additions & 0 deletions tests/core/web/CompositionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,4 +294,28 @@ public function testComplexLangShortCode()
$this->assertSame(['langShortCode' => 'en-GB'], $resolver->resolvedValues);
$this->assertSame(['langShortCode'], $resolver->resolvedKeys);
}

public function testHideDefaultPrefixOnly()
{
// none hidden
$request = new Request(['hostInfo' => 'http://localhost', 'pathInfo' => 'de/hello/world']);
$composition = new Composition($request, ['hidden' => false, 'default' => ['langShortCode' => 'en']]);
$this->assertSame('de', $composition->createRouteEnsure());
$composition = new Composition($request, ['hidden' => false, 'default' => ['langShortCode' => 'de']]);
$this->assertSame('de', $composition->createRouteEnsure());

// hidden
$request = new Request(['hostInfo' => 'http://localhost', 'pathInfo' => 'de/hello/world']);
$composition = new Composition($request, ['hidden' => true, 'default' => ['langShortCode' => 'en']]);
$this->assertSame('', $composition->createRouteEnsure());
$composition = new Composition($request, ['hidden' => true, 'default' => ['langShortCode' => 'de']]);
$this->assertSame('', $composition->createRouteEnsure());

// not hidden but hide default prefix only
$request = new Request(['hostInfo' => 'http://localhost', 'pathInfo' => 'de/hello/world']);
$composition = new Composition($request, ['hidden' => false, 'hideDefaultPrefixOnly' => true, 'default' => ['langShortCode' => 'en']]);
$this->assertSame('de', $composition->createRouteEnsure()); // is not hidden cause default is `en` and provided in the url is `de/hello/world`.
$composition = new Composition($request, ['hidden' => false, 'hideDefaultPrefixOnly' => true, 'default' => ['langShortCode' => 'de']]);
$this->assertSame('', $composition->createRouteEnsure()); // is default `de` and also provided in the url `de/hello/world`
}
}

0 comments on commit d3e1cc8

Please sign in to comment.