You are here

public static function GeshiFilter::getAvailableLanguages in GeSHi Filter for syntax highlighting 8

Same name and namespace in other branches
  1. 8.2 src/GeshiFilter.php \Drupal\geshifilter\GeshiFilter::getAvailableLanguages()

List of available languages.

Return value

array An array mapping language code to array with the language path and full language name.

5 calls to GeshiFilter::getAvailableLanguages()
GeshiFilter::getEnabledLanguages in src/GeshiFilter.php
List of enabled languages(with caching).
GeshiFilterFilter::perLanguageSettings in src/Plugin/Filter/GeshiFilterFilter.php
Function for generating a form table for per language settings.
GeshiFilterLanguagesForm::perLanguageSettings in src/Form/GeshiFilterLanguagesForm.php
Function for generating a form table for per language settings.
GeshiFilterLanguagesForm::validateForm in src/Form/GeshiFilterLanguagesForm.php
Form validation handler.
GeshiFilterProcess::geshiFactory in src/GeshiFilterProcess.php
Helper function for generating a GeSHi object.

File

src/GeshiFilter.php, line 159

Class

GeshiFilter
Contains constantas and some helper functions.

Namespace

Drupal\geshifilter

Code

public static function getAvailableLanguages() {

  // Try to get it from cache (database actually).
  $cache = \Drupal::cache();
  $available_languages = $cache
    ->get('geshifilter_available_languages_cache');
  if (!$available_languages) {

    // Not in cache: build the array of available_languages.
    $geshi_library = GeshiFilter::loadGeshi();
    $available_languages = [];
    if ($geshi_library['loaded']) {
      $dirs = [
        $geshi_library['library path'] . '/geshi',
        drupal_get_path('module', 'geshifilter') . '/geshi-extra',
      ];
      foreach ($dirs as $dir) {
        foreach (file_scan_directory($dir, '/.[pP][hH][pP]$/i') as $filename => $fileinfo) {

          // Short name.
          $name = $fileinfo->name;

          // Get full name.
          $geshi = new \GeSHi('', $name);
          $geshi
            ->set_language_path($dir);
          $fullname = $geshi
            ->get_language_name();
          unset($geshi);

          // Store.
          $available_languages[$name] = [
            'language_path' => $dir,
            'fullname' => $fullname,
          ];
        }
      }
      ksort($available_languages);

      // Save array to database.
      $cache
        ->set('geshifilter_available_languages_cache', $available_languages);
    }
  }
  else {
    $available_languages = $available_languages->data;
  }
  return $available_languages;
}