CurrentLanguage.php in Fullcalendar View 8.3
File
src/Plugin/FullcalendarViewProcessor/CurrentLanguage.php
View source
<?php
namespace Drupal\fullcalendar_view\Plugin\FullcalendarViewProcessor;
use Drupal\Core\Language\LanguageInterface;
use Drupal\fullcalendar_view\Plugin\FullcalendarViewProcessorBase;
class CurrentLanguage extends FullcalendarViewProcessorBase {
public function process(array &$variables) {
$view = $variables['view'];
$style = $view->style_plugin;
$options = $style->options;
$des_field = $options['des'];
if ($options['defaultLanguage'] === 'current_lang') {
$language = \Drupal::languageManager()
->getCurrentLanguage();
$default_lang = $this
->fullcalendar_view_map_langcodes($language
->getId());
$entries = isset($variables['#attached']['drupalSettings']['fullCalendarView']) ? $variables['#attached']['drupalSettings']['fullCalendarView'] : [];
foreach ($view->result as $row) {
$current_entity = $row->_entity;
foreach ($entries as &$entry) {
if ($entry['id'] === $current_entity
->id()) {
$entry['url'] = $current_entity
->toUrl('canonical', [
'language' => $language,
])
->toString();
if ($options['use_entity_fields']) {
if ($current_entity
->hasTranslation($language
->getId())) {
$translated_entity = $current_entity
->getTranslation($language
->getId());
}
else {
$translated_entity = $current_entity
->getTranslation(LanguageInterface::LANGCODE_DEFAULT);
}
$entry['title'] = $translated_entity
->getTitle();
if (!empty($des_field) && is_array($des_field)) {
foreach ($des_field as $des_field_name) {
if ($current_entity
->hasField($des_field_name)) {
$des = $translated_entity
->get($des_field_name)->value;
break;
}
}
}
if (!isset($des)) {
$des = '';
}
if (is_array($des)) {
$des = reset($des);
if (isset($des['value'])) {
$des = $des['value'];
}
}
$entry['description'] = $des;
}
break;
}
}
}
$variables['#attached']['drupalSettings']['defaultLang'] = $default_lang;
if ($entries) {
$variables['#attached']['drupalSettings']['fullCalendarView'] = $entries;
}
}
}
private function fullcalendar_view_map_langcodes($langcode) {
switch ($langcode) {
case "en-x-simple":
return "en";
case "pt-pt":
return "pt";
case "zh-hans":
return "zh-cn";
case "zh-hant":
return "zh-tw";
default:
return $langcode;
}
}
}