Example #1
0
 /**
  * Called to add the field to a query.
  *
  * By default, all needed data is taken from entities loaded by the query
  * plugin. Columns are added only if they are used in groupings.
  */
 public function query($use_groupby = FALSE)
 {
     $this->get_base_table();
     $entity_type = $this->definition['entity_tables'][$this->base_table];
     $fields = $this->additional_fields;
     // No need to add the entity type.
     $entity_type_key = array_search('entity_type', $fields);
     if ($entity_type_key !== FALSE) {
         unset($fields[$entity_type_key]);
     }
     $field_definition = $this->getFieldDefinition();
     if ($use_groupby) {
         // Add the fields that we're actually grouping on.
         $options = array();
         if ($this->options['group_column'] != 'entity_id') {
             $options = array($this->options['group_column'] => $this->options['group_column']);
         }
         $options += is_array($this->options['group_columns']) ? $this->options['group_columns'] : array();
         $fields = array();
         $rkey = $this->definition['is revision'] ? EntityStorageInterface::FIELD_LOAD_REVISION : EntityStorageInterface::FIELD_LOAD_CURRENT;
         // Go through the list and determine the actual column name from field api.
         foreach ($options as $column) {
             $name = $column;
             if (isset($field_definition['storage_details']['sql'][$rkey][$this->table][$column])) {
                 $name = $field_definition['storage_details']['sql'][$rkey][$this->table][$column];
             }
             $fields[$column] = $name;
         }
         $this->group_fields = $fields;
     }
     // Add additional fields (and the table join itself) if needed.
     if ($this->add_field_table($use_groupby)) {
         $this->ensureMyTable();
         $this->addAdditionalFields($fields);
         // Filter by langcode, if field translation is enabled.
         $field = $field_definition;
         if ($field->isTranslatable() && !empty($this->view->display_handler->options['field_langcode_add_to_query'])) {
             $column = $this->tableAlias . '.langcode';
             // By the same reason as field_language the field might be
             // LanguageInterface::LANGCODE_NOT_SPECIFIED in reality so allow it as
             // well.
             // @see this::field_langcode()
             $default_langcode = language_default()->id;
             $langcode = str_replace(array('***CURRENT_LANGUAGE***', '***DEFAULT_LANGUAGE***'), array($this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_CONTENT), $default_langcode), $this->view->display_handler->options['field_langcode']);
             $placeholder = $this->placeholder();
             $langcode_fallback_candidates = $this->languageManager->getFallbackCandidates($langcode, array('operation' => 'views_query', 'data' => $this));
             $this->query->addWhereExpression(0, "{$column} IN({$placeholder}) OR {$column} IS NULL", array($placeholder => $langcode_fallback_candidates));
         }
     }
 }
 /**
  * {@inheritdoc}
  */
 public function getFallbackCandidates(array $context = array())
 {
     if ($this->isMultilingual()) {
         $candidates = array();
         if (empty($context['operation']) || $context['operation'] != 'locale_lookup') {
             // If the fallback context is not locale_lookup, initialize the
             // candidates with languages ordered by weight and add
             // LanguageInterface::LANGCODE_NOT_SPECIFIED at the end. Interface
             // translation fallback should only be based on explicit configuration
             // gathered via the alter hooks below.
             $candidates = array_keys($this->getLanguages());
             $candidates[] = LanguageInterface::LANGCODE_NOT_SPECIFIED;
             $candidates = array_combine($candidates, $candidates);
             // The first candidate should always be the desired language if
             // specified.
             if (!empty($context['langcode'])) {
                 $candidates = array($context['langcode'] => $context['langcode']) + $candidates;
             }
         }
         // Let other modules hook in and add/change candidates.
         $type = 'language_fallback_candidates';
         $types = array();
         if (!empty($context['operation'])) {
             $types[] = $type . '_' . $context['operation'];
         }
         $types[] = $type;
         $this->moduleHandler->alter($types, $candidates, $context);
     } else {
         $candidates = parent::getFallbackCandidates($context);
     }
     return $candidates;
 }
 /**
  * {@inheritdoc}
  */
 public function getFallbackCandidates($langcode = NULL, array $context = array())
 {
     if ($this->isMultilingual()) {
         // Get languages ordered by weight, add BaseLanguageInterface::LANGCODE_NOT_SPECIFIED
         // at the end.
         $candidates = array_keys($this->getLanguages());
         $candidates[] = BaseLanguageInterface::LANGCODE_NOT_SPECIFIED;
         $candidates = array_combine($candidates, $candidates);
         // The first candidate should always be the desired language if specified.
         if (!empty($langcode)) {
             $candidates = array($langcode => $langcode) + $candidates;
         }
         // Let other modules hook in and add/change candidates.
         $type = 'language_fallback_candidates';
         $types = array();
         if (!empty($context['operation'])) {
             $types[] = $type . '_' . $context['operation'];
         }
         $types[] = $type;
         $this->moduleHandler->alter($types, $candidates, $context);
     } else {
         $candidates = parent::getFallbackCandidates($langcode, $context);
     }
     return $candidates;
 }