Skip to content

Commit

Permalink
make language filter pluign work with JRoute from admin
Browse files Browse the repository at this point in the history
  • Loading branch information
andrepereiradasilva committed Jul 4, 2016
1 parent 173c231 commit 0faa289
Showing 1 changed file with 37 additions and 24 deletions.
61 changes: 37 additions & 24 deletions plugins/system/languagefilter/languagefilter.php
Expand Up @@ -54,14 +54,15 @@ public function __construct(&$subject, $config)

$this->app = JFactory::getApplication();

// Setup language data.
$this->mode_sef = $this->app->get('sef', 0);
$this->sefs = JLanguageHelper::getLanguages('sef');
$this->lang_codes = JLanguageHelper::getLanguages('lang_code');
$this->default_lang = JComponentHelper::getParams('com_languages')->get('site', 'en-GB');

// If language filter plugin is executed in a site page.
if ($this->app->isSite())
{
// Setup language data.
$this->mode_sef = $this->app->get('sef', 0);
$this->sefs = JLanguageHelper::getLanguages('sef');
$this->lang_codes = JLanguageHelper::getLanguages('lang_code');
$this->default_lang = JComponentHelper::getParams('com_languages')->get('site', 'en-GB');

$levels = JFactory::getUser()->getAuthorisedViewLevels();

foreach ($this->sefs as $sef => $language)
Expand All @@ -81,6 +82,20 @@ public function __construct(&$subject, $config)
// Detect browser feature.
$this->app->setDetectBrowser($this->params->get('detect_browser', '1') == '1');
}
// If language filter plugin is executed in a admin page (ex: JRoute site).
else
{
$this->current_lang = $this->default_lang;

foreach ($this->sefs as $sef => $language)
{
if (!array_key_exists($language->lang_code, JLanguageMultilang::getSiteLangs()))
{
unset($this->lang_codes[$language->lang_code]);
unset($this->sefs[$language->sef]);
}
}
}
}

/**
Expand All @@ -94,26 +109,24 @@ public function onAfterInitialise()
{
$this->app->item_associations = $this->params->get('item_associations', 0);

if ($this->app->isSite())
{
$router = $this->app->getRouter();
// We need to make sure we are always using the site router, even if the language plugin is executed in admin app.
$router = $this->app->isSite() ? $this->app->getRouter() : JApplicationCms::getInstance('site')->getRouter('site');

// Attach build rules for language SEF.
$router->attachBuildRule(array($this, 'preprocessBuildRule'), JRouter::PROCESS_BEFORE);
$router->attachBuildRule(array($this, 'buildRule'), JRouter::PROCESS_DURING);
// Attach build rules for language SEF.
$router->attachBuildRule(array($this, 'preprocessBuildRule'), JRouter::PROCESS_BEFORE);
$router->attachBuildRule(array($this, 'buildRule'), JRouter::PROCESS_DURING);

if ($this->mode_sef)
{
$router->attachBuildRule(array($this, 'postprocessSEFBuildRule'), JRouter::PROCESS_AFTER);
}
else
{
$router->attachBuildRule(array($this, 'postprocessNonSEFBuildRule'), JRouter::PROCESS_AFTER);
}

// Attach parse rules for language SEF.
$router->attachParseRule(array($this, 'parseRule'), JRouter::PROCESS_DURING);
if ($this->mode_sef)
{
$router->attachBuildRule(array($this, 'postprocessSEFBuildRule'), JRouter::PROCESS_AFTER);
}
else
{
$router->attachBuildRule(array($this, 'postprocessNonSEFBuildRule'), JRouter::PROCESS_AFTER);
}

// Attach parse rules for language SEF.
$router->attachParseRule(array($this, 'parseRule'), JRouter::PROCESS_DURING);
}

/**
Expand All @@ -126,7 +139,7 @@ public function onAfterInitialise()
public function onAfterRoute()
{
// Add custom site name.
if (isset($this->lang_codes[$this->current_lang]) && $this->lang_codes[$this->current_lang]->sitename)
if ($this->app->isSite() && isset($this->lang_codes[$this->current_lang]) && $this->lang_codes[$this->current_lang]->sitename)
{
$this->app->set('sitename', $this->lang_codes[$this->current_lang]->sitename);
}
Expand Down

0 comments on commit 0faa289

Please sign in to comment.