Back to LanguageHelper class

Method getContentLanguages

public static array
getContentLanguages
(mixed $publishedStates = array(1), mixed $checkInstalled = true, mixed $pivot = 'lang_code', mixed $orderField = null, mixed $orderDirection = null)
Get a list of content languages.
Parameters
  • array $publishedStates Array with the content language published states. Empty array for all.
  • bool $checkInstalled Check if the content language is installed.
  • string $pivot The pivot of the returning array.
  • string $orderField Field to order the results.
  • string $orderDirection Direction to order the results.
Returns
  • array Array of the content languages.
Since
  • 3.7.0

Method getContentLanguages - Source code

/**
 * Get a list of content languages.
 *
 * @param   array    $publishedStates  Array with the content language published states. Empty array for all.
 * @param   boolean  $checkInstalled   Check if the content language is installed.
 * @param   string   $pivot            The pivot of the returning array.
 * @param   string   $orderField       Field to order the results.
 * @param   string   $orderDirection   Direction to order the results.
 *
 * @return  array  Array of the content languages.
 *
 * @since   3.7.0
 */
public static function getContentLanguages($publishedStates = array(1), $checkInstalled = true, $pivot = 'lang_code', $orderField = null, $orderDirection = null)
{
    static $contentLanguages = null;
    if ($contentLanguages === null) {
        /** @var OutputController $cache */
        $cache = Factory::getContainer()->get(CacheControllerFactoryInterface::class)->createCacheController('output', ['defaultgroup' => 'com_languages']);
        if ($cache->contains('contentlanguages')) {
            $contentLanguages = $cache->get('contentlanguages');
        } else {
            $db = Factory::getDbo();
            $query = $db->getQuery(true)->select('*')->from($db->quoteName('#__languages'));
            $contentLanguages = $db->setQuery($query)->loadObjectList();
            $cache->store($contentLanguages, 'contentlanguages');
        }
    }
    $languages = $contentLanguages;
    // B/C layer. Before 3.8.3.
    if ($publishedStates === true) {
        $publishedStates = array(1);
    } elseif ($publishedStates === false) {
        $publishedStates = array();
    }
    // Check the language published state, if needed.
    if (\count($publishedStates) > 0) {
        foreach ($languages as $key => $language) {
            if (!\in_array((int) $language->published, $publishedStates, true)) {
                unset($languages[$key]);
            }
        }
    }
    // Check if the language is installed, if needed.
    if ($checkInstalled) {
        $languages = array_values(array_intersect_key(ArrayHelper::pivot($languages, 'lang_code'), static::getInstalledLanguages(0)));
    }
    // Order the list, if needed.
    if ($orderField !== null && $orderDirection !== null) {
        $languages = ArrayHelper::sortObjects($languages, $orderField, strtolower($orderDirection) === 'desc' ? -1 : 1, true, true);
    }
    // Add the pivot, if needed.
    if ($pivot !== null) {
        $languages = ArrayHelper::pivot($languages, $pivot);
    }
    return $languages;
}