Skip to content

Commit

Permalink
allow context failing (cherry picked #39)
Browse files Browse the repository at this point in the history
* allow context failing
* improve template locations
* add upgrade note
  • Loading branch information
solverat authored Aug 22, 2019
1 parent 675116e commit 9842944
Show file tree
Hide file tree
Showing 10 changed files with 177 additions and 43 deletions.
1 change: 1 addition & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Just click the "update" button or execute the migration command to finish the bu
#### Update from Version 3.0.0 to Version 3.1.0
- **[NEW FEATURE]**: Allow [pimcore redirect modification](https://github.com/dachcom-digital/pimcore-i18n/issues/33).
- **[BUGFIX]**: Disable context switch event if [pimcore full page cache](https://github.com/dachcom-digital/pimcore-i18n/issues/18) is enabled
- **[BUGFIX]**: Fix context on [xliff export](https://github.com/dachcom-digital/pimcore-i18n/issues/28)

#### Update from Version 2.x to Version 3.0.0
- **[NEW FEATURE]**: Pimcore 6.0.0 ready
Expand Down
85 changes: 49 additions & 36 deletions docs/60_CodeExamples.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,53 +271,66 @@ Available Options for the `getCurrentCountryInfo` or `getCurrentLanguageInfo` co

### Language Drop-Down
```twig
<nav id="navigation">
<select>
{% for language in i18n_context('getActiveLanguages') %}
<option {{ language.active ? 'selected' : '' }} value="{{ language.linkedHref }}">{{ language.iso|upper }}</option>
{% endfor %}
</select>
</nav>
{% set languages = i18n_context('getActiveLanguages') %}
{% if languages is iterable %}
<nav id="navigation">
<select>
{% for language in languages %}
<option {{ language.active ? 'selected' : '' }} value="{{ language.linkedHref }}">{{ language.iso|upper }}</option>
{% endfor %}
</select>
</nav>
{% endif %}
```

### Country Selection
```twig
<nav id="navigation">
{% for country in i18n_context('getActiveCountries') %}
<ul>
<li class="country">{{ country.countryTitle }}
<ul class="languages">
{% for language in country.languages %}
<li{{ language.active ? ' class="active"' : '' }}><a href="{{ language.linkedHref }}">{{ language.iso|upper }}</a></li>
{% endfor %}
</ul>
</li>
</ul>
{% endfor %}
</nav>
{% set countries = i18n_context('getActiveCountries') %}
{% if countries is iterable %}
<nav id="navigation">
{% for country in countries %}
<ul>
<li class="country">{{ country.countryTitle }}
<ul class="languages">
{% for language in country.languages %}
<li{{ language.active ? ' class="active"' : '' }}><a href="{{ language.linkedHref }}">{{ language.iso|upper }}</a></li>
{% endfor %}
</ul>
</li>
</ul>
{% endfor %}
</nav>
{% endif %}
```

### Complex Country / Language Selection based on Current Zone
```twig
<nav id="navigation">
{% if i18n_zone_info('mode') == 'country' %}
{% for country in i18n_context('getActiveCountries') %}
<ul>
<li class="country">{{ country.countryTitle }}
<ul class="languages">
{% for language in country.languages %}
<li{{ language.active ? ' class="active"' : '' }}><a href="{{ language.linkedHref }}">{{ language.iso|upper }}</a></li>
{% endfor %}
</ul>
</li>
</ul>
{% endfor %}
{% elseif i18n_zone_info('mode') == 'language' %}
<select>
{% for language in i18n_context('getActiveLanguages') %}
<option {{ language.active ? 'selected' : '' }} value="{{ language.linkedHref }}">{{ language.iso|upper }}</option>
{% set countries = i18n_context('getActiveCountries') %}
{% if countries is iterable %}
{% for country in countries %}
<ul>
<li class="country">{{ country.countryTitle }}
<ul class="languages">
{% for language in country.languages %}
<li{{ language.active ? ' class="active"' : '' }}><a href="{{ language.linkedHref }}">{{ language.iso|upper }}</a></li>
{% endfor %}
</ul>
</li>
</ul>
{% endfor %}
</select>
{% endif %}
{% elseif i18n_zone_info('mode') == 'language' %}
{% set languages = i18n_context('getActiveLanguages') %}
{% if languages is iterable %}
<select>
{% for language in languages %}
<option {{ language.active ? 'selected' : '' }} value="{{ language.linkedHref }}">{{ language.iso|upper }}</option>
{% endfor %}
</select>
{% endif %}
{% endif %}
</nav>
```
11 changes: 11 additions & 0 deletions src/I18nBundle/Exception/ContextNotDefinedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace I18nBundle\Exception;

final class ContextNotDefinedException extends \Exception
{
public function __construct()
{
parent::__construct('context is not defined');
}
}
5 changes: 3 additions & 2 deletions src/I18nBundle/Manager/ContextManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use I18nBundle\Adapter\Context\ContextInterface;
use I18nBundle\Adapter\Context\Country;
use I18nBundle\Adapter\Context\Language;
use I18nBundle\Exception\ContextNotDefinedException;
use I18nBundle\Registry\ContextRegistry;
use Pimcore\Model\Document;

Expand Down Expand Up @@ -61,12 +62,12 @@ public function initContext($contextIdentifier, $document = null)
/**
* @return ContextInterface
*
* @throws \Exception
* @throws ContextNotDefinedException
*/
public function getContext()
{
if (empty($this->currentContext)) {
throw new \Exception('context is not defined');
throw new ContextNotDefinedException();
}

return $this->currentContext;
Expand Down
9 changes: 8 additions & 1 deletion src/I18nBundle/Twig/Extension/I18nExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace I18nBundle\Twig\Extension;

use I18nBundle\Exception\ContextNotDefinedException;
use I18nBundle\Manager\ZoneManager;
use I18nBundle\Manager\ContextManager;
use Twig\Extension\AbstractExtension;
Expand Down Expand Up @@ -50,7 +51,13 @@ public function getFunctions()
*/
public function getI18Context($method = '', $options = [])
{
return call_user_func_array([$this->contextManager->getContext(), $method], $options);
try {
$context = $this->contextManager->getContext();
} catch (ContextNotDefinedException $e) {
return null;
}

return call_user_func_array([$context, $method], $options);
}

/**
Expand Down
1 change: 1 addition & 0 deletions tests/_etc/bundle_configuration
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ declare -A DACHCOM_INSTALL_CONFIG_FILES=(
# template files
["$DACHCOM_BUNDLE_HOME/_etc/config/bundle/template/controller/DefaultController"]="src/AppBundle/Controller/DefaultController.php"
["$DACHCOM_BUNDLE_HOME/_etc/config/bundle/template/views/default"]="app/Resources/views/Default/default.html.twig"
["$DACHCOM_BUNDLE_HOME/_etc/config/bundle/template/views/languageSelector"]="app/Resources/views/Default/languageSelector.html.twig"
)
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@ class DefaultController extends FrontendController
public function defaultAction(Request $request)
{
}

public function languageSelectorAction(Request $request)
{

}

}
40 changes: 40 additions & 0 deletions tests/_etc/config/bundle/template/views/languageSelector
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!DOCTYPE html>
<html lang="{{ app.request.locale }}">
<head>
<meta charset="UTF-8">
<title>Test Page for I18n</title>
{{ pimcore_head_meta() }}
{{ pimcore_head_link() }}
</head>
<body>
<div id="site">

{% set languages = i18n_context('getActiveLanguages') %}
{% if languages is iterable %}
<nav id="i18n-language-selector">
<select>
{% for language in languages %}
<option {{ language.active ? 'selected' : '' }} value="{{ language.linkedHref }}">{{ language.iso|upper }}</option>
{% endfor %}
</select>
</nav>
{% endif %}

<div class="navigation">
{% set menu = pimcore_build_nav(document, document, null, app.request.locale) %}
{{ pimcore_render_nav(menu, 'menu', 'renderMenu', {
maxDepth: 10,
ulClass: 'nav navbar-nav'
}) }}
</div>
<div class="content">
{% if document is defined %}
<span id="page-id">{{ document.getId() }}</span>
{% endif %}
</div>
{% block content %}
{{ pimcore_areablock('dachcomBundleTest') }}
{% endblock %}
</div>
</body>
</html>
40 changes: 36 additions & 4 deletions tests/_support/Helper/PimcoreBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Codeception\Module;
use Codeception\TestInterface;
use DachcomBundle\Test\Helper\Browser\PhpBrowser;
use DachcomBundle\Test\Util\FileGeneratorHelper;
use DachcomBundle\Test\Util\I18nHelper;
use Pimcore\Model\Document;
Expand Down Expand Up @@ -43,14 +44,16 @@ public function _after(TestInterface $test)
*
* @param string $documentKey
* @param string $locale
* @param string $action
*
* @return Page
*/
public function haveAPageDocument(
$documentKey = 'test-document',
$locale = null
$locale = null,
$action = 'default'
) {
$document = $this->generatePageDocument($documentKey, $locale);
$document = $this->generatePageDocument($documentKey, $locale, $action);

try {
$document->save();
Expand Down Expand Up @@ -395,19 +398,48 @@ public function haveAPimcoreRedirect(array $data)
return $redirect;
}

/**
* @param Document $document
*
* @throws \Codeception\Exception\ModuleException
*/
public function submitDocumentToXliffExporter(Document $document)
{
/** @var PimcoreCore $pimcoreCore */
$pimcoreCore = $this->getModule('\\' . PimcoreCore::class);

$pimcoreCore->_loadPage('POST', '/admin/translation/xliff-export', [
'csrfToken' => PhpBrowser::PIMCORE_ADMIN_CSRF_TOKEN_NAME,
'source' => 'en',
'target' => 'de',
'data' => json_encode([
[
'id' => $document->getId(),
'path' => $document->getFullPath(),
'type' => 'document',
'children' => true
]
]),
'type' => 'xliff'
]);

$this->assertContains(['successs' => true], json_decode($pimcoreCore->_getResponseContent(), true));
}

/**
* API Function to create a page document
*
* @param string $key
* @param string $locale
* @param string $action
*
* @return Page
*/
protected function generatePageDocument($key = 'document-test', $locale = null)
protected function generatePageDocument($key = 'document-test', $locale = null, $action = 'default')
{
$document = TestHelper::createEmptyDocumentPage('', false);
$document->setController('@AppBundle\Controller\DefaultController');
$document->setAction('default');
$document->setAction($action);
$document->setKey($key);
$document->setProperty('navigation_title', 'text', $key);
$document->setProperty('navigation_name', 'text', $key);
Expand Down
22 changes: 22 additions & 0 deletions tests/functional.default/XliffExportCest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace DachcomBundle\Test\FunctionalDefault;

use DachcomBundle\Test\FunctionalTester;

class XliffExportCest
{
/**
* @param FunctionalTester $I
*/
public function testXliffExport(FunctionalTester $I)
{
$document = $I->haveAPageDocument('en', 'en', 'languageSelector');

$I->haveAUser('dachcom_test');
$I->amLoggedInAs('dachcom_test');

$I->submitDocumentToXliffExporter($document);
}

}

0 comments on commit 9842944

Please sign in to comment.