You are here

public function CurrentLanguage::process in Fullcalendar View 8.3

Process the view variable array.

Parameters

array $variables: Template variables.

Overrides FullcalendarViewProcessorBase::process

File

src/Plugin/FullcalendarViewProcessor/CurrentLanguage.php, line 17

Class

CurrentLanguage
Current language plugin.

Namespace

Drupal\fullcalendar_view\Plugin\FullcalendarViewProcessor

Code

public function process(array &$variables) {
  $view = $variables['view'];
  $style = $view->style_plugin;
  $options = $style->options;

  // Field machine name for event description.
  $des_field = $options['des'];

  // Update the default language, only if it is specified as current active language.
  if ($options['defaultLanguage'] === 'current_lang') {

    // Get current language.
    $language = \Drupal::languageManager()
      ->getCurrentLanguage();
    $default_lang = $this
      ->fullcalendar_view_map_langcodes($language
      ->getId());
    $entries = isset($variables['#attached']['drupalSettings']['fullCalendarView']) ? $variables['#attached']['drupalSettings']['fullCalendarView'] : [];

    // Go through all results of the view.
    foreach ($view->result as $row) {

      // Result entity of current row.
      $current_entity = $row->_entity;

      // Update the URL for the view entry refer to current row.
      foreach ($entries as &$entry) {
        if ($entry['id'] === $current_entity
          ->id()) {
          $entry['url'] = $current_entity
            ->toUrl('canonical', [
            'language' => $language,
          ])
            ->toString();

          // Translate the title and description,
          // if using the entity field rather than
          // the raw view fields.
          if ($options['use_entity_fields']) {
            if ($current_entity
              ->hasTranslation($language
              ->getId())) {
              $translated_entity = $current_entity
                ->getTranslation($language
                ->getId());
            }
            else {

              // Current entity doesn't have the translation for
              // the language specified.
              $translated_entity = $current_entity
                ->getTranslation(LanguageInterface::LANGCODE_DEFAULT);
            }
            $entry['title'] = $translated_entity
              ->getTitle();

            // Description for events. For multiple bundle types,
            // there might be more than one field specified.
            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;

                  // We just need only one description text.
                  // Once we got it, quit the loop.
                  break;
                }
              }
            }
            if (!isset($des)) {
              $des = '';
            }
            if (is_array($des)) {
              $des = reset($des);
              if (isset($des['value'])) {
                $des = $des['value'];
              }
            }
            $entry['description'] = $des;
          }
          break;
        }
      }
    }

    // Set the default language.
    $variables['#attached']['drupalSettings']['defaultLang'] = $default_lang;

    // Update the entry URL in default language.
    if ($entries) {
      $variables['#attached']['drupalSettings']['fullCalendarView'] = $entries;
    }
  }
}