You are here

class CurrentLanguage in Fullcalendar View 8.3

Current language plugin.

Plugin annotation


@FullcalendarViewProcessor(
  id = "fullcalendar_view_language",
  label = @Translation("Current language")
)

Hierarchy

Expanded class hierarchy of CurrentLanguage

File

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

Namespace

Drupal\fullcalendar_view\Plugin\FullcalendarViewProcessor
View source
class CurrentLanguage extends FullcalendarViewProcessorBase {
  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;
      }
    }
  }

  /**
   * Map Drupal language codes to those used by FullCalendar.
   *
   * @param string $langcode
   *   Drupal language code.
   *
   * @return string
   *   Returns the mapped langcode.
   */
  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;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CurrentLanguage::fullcalendar_view_map_langcodes private function Map Drupal language codes to those used by FullCalendar.
CurrentLanguage::process public function Process the view variable array. Overrides FullcalendarViewProcessorBase::process
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 92