Skip to content

Commit

Permalink
pkp#10616 Add ThemePlugin function to get localized theme option value (
Browse files Browse the repository at this point in the history
  • Loading branch information
NateWr authored and Hafsa-Naeem committed Dec 11, 2024
1 parent d243f8f commit 138a720
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions classes/plugins/ThemePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use PKP\core\Core;
use PKP\core\PKPApplication;
use PKP\db\DAORegistry;
use PKP\facades\Locale;
use PKP\session\SessionManager;

define('LESS_FILENAME_SUFFIX', '.less');
Expand Down Expand Up @@ -492,6 +493,56 @@ public function getOption($name)
return $option->default ?? null;
}

/**
* Get the localized value of an option
*
* Modelled on DataObject::getLocalizedData()
*/
public function getLocalizedOption(string $name, string $preferredLocale = null, string &$selectedLocale = null): mixed
{
$value = $this->getOption($name);

if (!is_array($value)) {
return null;
}

foreach ($this->getLocalePrecedence($preferredLocale) as $locale) {
if (!empty($value[$locale])) {
$selectedLocale = $locale;
return $value[$locale];
}
}

// Fallback: Get the first available piece of data]
foreach ($value as $locale => $dataValue) {
if (!empty($dataValue)) {
$selectedLocale = $locale;
return $dataValue;
}
}

return null;
}

/**
* Get the locale precedence order
*
* @see DataObject::getLocalePrecedence()
* @deprecated 3.4
*/
protected function getLocalePrecedence(string $preferredLocale = null): array
{
$request = Application::get()->getRequest();

return array_unique(
array_filter([
$preferredLocale ?? Locale::getLocale(),
$request->getContext()?->getPrimaryLocale(),
$request->getSite()->getPrimaryLocale(),
])
);
}

/**
* Get an option's configuration settings
*
Expand Down

0 comments on commit 138a720

Please sign in to comment.