ClientCdfLanguage.php in Acquia Content Hub 8.2
File
src/EventSubscriber/ClientCdf/ClientCdfLanguage.php
View source
<?php
namespace Drupal\acquia_contenthub\EventSubscriber\ClientCdf;
use Drupal\acquia_contenthub\AcquiaContentHubEvents;
use Drupal\acquia_contenthub\Event\BuildClientCdfEvent;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandler;
use Drupal\Core\Language\LanguageDefault;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ClientCdfLanguage implements EventSubscriberInterface {
protected $entityTypeManager;
protected $moduleHandler;
protected $languageDefault;
public function __construct(EntityTypeManagerInterface $entityTypeManager, ModuleHandler $moduleHandler, LanguageDefault $language_default) {
$this->entityTypeManager = $entityTypeManager;
$this->moduleHandler = $moduleHandler;
$this->languageDefault = $language_default;
}
public static function getSubscribedEvents() {
$events[AcquiaContentHubEvents::BUILD_CLIENT_CDF][] = [
'onBuildClientCdf',
100,
];
return $events;
}
public function onBuildClientCdf(BuildClientCdfEvent $event) {
$cdf = $event
->getCdf();
$metadata = $cdf
->getMetadata();
if (!$this->moduleHandler
->moduleExists('language')) {
$default_language = [
'direction' => $this->languageDefault
->get()
->getDirection(),
'id' => $this->languageDefault
->get()
->getId(),
'label' => $this->languageDefault
->get()
->getName(),
'langcode' => $this->languageDefault
->get()
->getId(),
'locked' => $this->languageDefault
->get()
->isLocked(),
'status' => 1,
'weight' => $this->languageDefault
->get()
->getWeight(),
];
$metadata['languages'][$default_language['langcode']] = $default_language;
}
else {
$metadata['languages'] = $this
->getLanguages();
}
$cdf
->setMetadata($metadata);
}
protected function getLanguages() {
$languages = [];
$lang_entities = $this->entityTypeManager
->getStorage('configurable_language')
->loadMultiple();
foreach ($lang_entities as $langcode => $language) {
$languages[$langcode] = $language
->toArray();
unset($languages[$langcode]['_core'], $languages[$langcode]['dependencies']);
}
return $languages;
}
}