You are here

private function SiteSettingsLoader::buildSettings in Site Settings and Labels 8

Build the settings array.

1 call to SiteSettingsLoader::buildSettings()
SiteSettingsLoader::rebuildCache in src/SiteSettingsLoader.php
Rebuild the cache.

File

src/SiteSettingsLoader.php, line 114

Class

SiteSettingsLoader
Class SiteSettingsLoader.

Namespace

Drupal\site_settings

Code

private function buildSettings($langcode) {

  // Clear the existing settings to avoid empty keys.
  $this->settings = [];

  // Get all site settings.
  $setting_entities = SiteSettingEntity::loadMultiple();

  // Get entity type configurations at once for performance.
  $entities = [];
  $entity_type = $this->entityTypeManager
    ->getStorage('site_setting_entity_type');
  if ($entity_type) {
    $entities = $entity_type
      ->loadMultiple();
  }
  foreach ($setting_entities as $entity) {
    if (method_exists($entity, 'hasTranslation') && $entity
      ->hasTranslation($langcode)) {
      $entity = $entity
        ->getTranslation($langcode);
    }

    // Get data.
    $fieldset = $entity->fieldset
      ->getValue()[0]['value'];
    $fieldset = $this
      ->fieldsetKey($fieldset);
    $type = $entity->type
      ->getValue()[0]['target_id'];
    $multiple = isset($entities[$type]) ? $entities[$type]->multiple : FALSE;

    // If we have multiple, set as array of entities.
    if ($multiple) {
      if (!isset($this->settings[$fieldset][$type])) {
        $this->settings[$fieldset][$type] = [];
      }
      $this->settings[$fieldset][$type][] = $this
        ->getValues($entity);
    }
    else {
      $this->settings[$fieldset][$type] = $this
        ->getValues($entity);
    }
  }

  // Get all possibilities and fill with empty values.
  $bundles = $this->entityTypeManager
    ->getStorage('site_setting_entity_type')
    ->loadMultiple();
  foreach ($bundles as $bundle) {
    $fieldset = $this
      ->fieldsetKey($bundle->fieldset);
    $id = $bundle
      ->id();

    // Only fill if not yet set.
    if (!isset($this->settings[$fieldset][$id])) {
      $this->settings[$fieldset][$id] = '';
    }
  }
}