SiteSettingsLoader.php in Site Settings and Labels 8
File
src/SiteSettingsLoader.php
View source
<?php
namespace Drupal\site_settings;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Url;
use Drupal\site_settings\Entity\SiteSettingEntity;
class SiteSettingsLoader {
protected const SITE_SETTINGS_CACHE_BIN = 'site_settings';
protected const SITE_SETTINGS_CACHE_CID = 'settings';
protected $entityTypeManager;
protected $languageManager;
protected $settings;
public function __construct(EntityTypeManagerInterface $entity_type_manager, LanguageManagerInterface $language_manager) {
$this->entityTypeManager = $entity_type_manager;
$this->languageManager = $language_manager;
}
public function loadByFieldset($fieldset) {
$this
->loadAll();
$fieldset = $this
->fieldsetKey($fieldset);
return isset($this->settings[$fieldset]) ? $this->settings[$fieldset] : [];
}
public function loadAll($rebuild_cache = FALSE) {
$langcode = $this->languageManager
->getCurrentLanguage()
->getId();
if (!$rebuild_cache && ($cache = \Drupal::cache(self::SITE_SETTINGS_CACHE_BIN)
->get(self::SITE_SETTINGS_CACHE_CID . ':' . $langcode))) {
$this->settings = $cache->data;
}
else {
$this
->rebuildCache($langcode);
}
return $this->settings;
}
public function rebuildCache($langcode) {
$this
->buildSettings($langcode);
\Drupal::cache(self::SITE_SETTINGS_CACHE_BIN)
->set(self::SITE_SETTINGS_CACHE_CID . ':' . $langcode, $this->settings);
}
public function clearCache() {
\Drupal::cache(self::SITE_SETTINGS_CACHE_BIN)
->deleteAll();
}
private function buildSettings($langcode) {
$this->settings = [];
$setting_entities = SiteSettingEntity::loadMultiple();
$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);
}
$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 ($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);
}
}
$bundles = $this->entityTypeManager
->getStorage('site_setting_entity_type')
->loadMultiple();
foreach ($bundles as $bundle) {
$fieldset = $this
->fieldsetKey($bundle->fieldset);
$id = $bundle
->id();
if (!isset($this->settings[$fieldset][$id])) {
$this->settings[$fieldset][$id] = '';
}
}
}
private function getValues($entity) {
$build = [];
$fields = $entity
->getFields();
foreach ($fields as $key => $field) {
$field_definition = $field
->getFieldDefinition();
if (!method_exists(get_class($field_definition), 'isBaseField') || !$field_definition
->isBaseField()) {
if (($value = $this
->getValue($field)) || $field_definition
->getType() == 'boolean') {
$build[$key] = $value;
switch ($field_definition
->getType()) {
case 'link':
$build[$key] = $this
->addSupplementaryLinkData($build[$key], $field);
break;
case 'image':
case 'file':
case 'svg_image_field':
$build[$key] = $this
->addSupplementaryImageData($build[$key], $field);
break;
}
}
}
}
if (count($build) == 1) {
return reset($build);
}
elseif (count($build) == 2 && isset($build['user_id'])) {
unset($build['user_id']);
return reset($build);
}
else {
return $build;
}
}
private function getValue(FieldItemListInterface $field) {
if ($value = $field
->getValue()) {
if (count($value) <= 1) {
$item = reset($value);
if (count($item) <= 1) {
return reset($item);
}
else {
return $item;
}
}
else {
return $value;
}
}
return FALSE;
}
private function addSupplementaryLinkData(array $data, $field) {
if (isset($field->uri) && ($url = Url::fromUri($field->uri))) {
$data = array_merge($data, [
'url' => $url,
]);
}
return $data;
}
private function addSupplementaryImageData(array $data, $field) {
if ($entities = $field
->referencedEntities()) {
if (count($entities) > 1) {
foreach ($data as $key => $sub_image_data) {
$entity = $entities[$key];
$data[$key]['filename'] = $entity
->getFilename();
$data[$key]['uri'] = $entity
->getFileUri();
$data[$key]['mime_type'] = $entity
->getMimeType();
$data[$key]['size'] = $entity
->getSize();
$data[$key]['is_permanent'] = $entity
->isPermanent();
}
}
else {
$entity = reset($entities);
$data['filename'] = $entity
->getFilename();
$data['uri'] = $entity
->getFileUri();
$data['mime_type'] = $entity
->getMimeType();
$data['size'] = $entity
->getSize();
$data['is_permanent'] = $entity
->isPermanent();
}
}
return $data;
}
private function fieldsetKey($fieldset) {
return strtolower(str_replace(' ', '_', $fieldset));
}
}