function InstallHelper::readMultilingualContent

Same name in other branches
  1. 9 core/profiles/demo_umami/modules/demo_umami_content/src/InstallHelper.php \Drupal\demo_umami_content\InstallHelper::readMultilingualContent()
  2. 8.9.x core/profiles/demo_umami/modules/demo_umami_content/src/InstallHelper.php \Drupal\demo_umami_content\InstallHelper::readMultilingualContent()
  3. 11.x core/profiles/demo_umami/modules/demo_umami_content/src/InstallHelper.php \Drupal\demo_umami_content\InstallHelper::readMultilingualContent()

Read multilingual content.

Parameters

string $filename: Filename to import.

Return value

array An array of two items: 1. All multilingual content that was read from the files. 2. List of language codes that need to be imported.

1 call to InstallHelper::readMultilingualContent()
InstallHelper::importContentFromFile in core/profiles/demo_umami/modules/demo_umami_content/src/InstallHelper.php
Imports content.

File

core/profiles/demo_umami/modules/demo_umami_content/src/InstallHelper.php, line 177

Class

InstallHelper
Defines a helper class for importing default content.

Namespace

Drupal\demo_umami_content

Code

protected function readMultilingualContent($filename) {
    $default_content_path = $this->module_path . "/default_content/languages/";
    // Get all enabled languages.
    $translated_languages = $this->enabledLanguages;
    // Load all the content from any CSV files that exist for enabled languages.
    foreach ($translated_languages as $language) {
        if (file_exists($default_content_path . "{$language}/{$filename}") && ($handle = fopen($default_content_path . "{$language}/{$filename}", 'r')) !== FALSE) {
            $header = fgetcsv($handle, escape: '');
            $line_counter = 0;
            while (($content = fgetcsv($handle, escape: '')) !== FALSE) {
                $keyed_content[$language][$line_counter] = array_combine($header, $content);
                $line_counter++;
            }
            fclose($handle);
        }
        else {
            // Language directory exists, but the file in this language was not found,
            // remove that language from list of languages to be translated.
            $key = array_search($language, $translated_languages);
            unset($translated_languages[$key]);
        }
    }
    return [
        $keyed_content,
        $translated_languages,
    ];
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.