/**
  * {@inheritdoc}
  */
 public function query(QueryPluginBase $query)
 {
     // If the data table is defined, we use the translation language as render
     // language, otherwise we fall back to the default entity language, which is
     // stored in the revision table for revisionable entity types.
     $entity_info = $this->view->rowPlugin->entityManager->getDefinition($this->entityType->id());
     foreach (array('data_table', 'revision_table', 'base_table') as $key) {
         if ($table = $entity_info->get($key)) {
             $table_alias = $query->ensureTable($table);
             $this->langcodeAlias = $query->addField($table_alias, 'langcode');
             break;
         }
     }
 }
Ejemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 public function query(QueryPluginBase $query, $relationship = NULL)
 {
     // In order to render in the translation language of the entity, we need
     // to add the language code of the entity to the query. Skip if the site
     // is not multilingual or the entity is not translatable.
     if (!$this->languageManager->isMultilingual() || !$this->entityType->hasKey('langcode')) {
         return;
     }
     $langcode_key = $this->entityType->getKey('langcode');
     $storage = \Drupal::entityManager()->getStorage($this->entityType->id());
     if ($table = $storage->getTableMapping()->getFieldTableName($langcode_key)) {
         $table_alias = $query->ensureTable($table, $relationship);
         $this->langcodeAlias = $query->addField($table_alias, $langcode_key);
     }
 }
Ejemplo n.º 3
0
 /**
  * {@inheritdoc}
  */
 public function query(QueryPluginBase $query, $relationship = NULL)
 {
     // There is no point in getting the language, in case the site is not
     // multilingual.
     if (!$this->languageManager->isMultilingual()) {
         return;
     }
     // If the data table is defined, we use the translation language as render
     // language, otherwise we fall back to the default entity language, which is
     // stored in the revision table for revisionable entity types.
     $langcode_key = $this->entityType->getKey('langcode');
     foreach (array('data_table', 'revision_table', 'base_table') as $key) {
         if ($table = $this->entityType->get($key)) {
             $table_alias = $query->ensureTable($table, $relationship);
             $this->langcodeAlias = $query->addField($table_alias, $langcode_key);
             break;
         }
     }
 }
Ejemplo n.º 4
0
 /**
  * Execute the view's query.
  *
  * @param string $display_id
  *   The machine name of the display, which should be executed.
  *
  * @return bool
  *   Return whether the executing was successful, for example an argument
  *   could stop the process.
  */
 public function execute($display_id = NULL)
 {
     if (empty($this->built)) {
         if (!$this->build($display_id)) {
             return FALSE;
         }
     }
     if (!empty($this->executed)) {
         return TRUE;
     }
     // Don't allow to use deactivated displays, but display them on the live preview.
     if (!$this->display_handler->isEnabled() && empty($this->live_preview)) {
         $this->build_info['fail'] = TRUE;
         return FALSE;
     }
     // Let modules modify the view just prior to executing it.
     $module_handler = \Drupal::moduleHandler();
     $module_handler->invokeAll('views_pre_execute', array($this));
     // Check for already-cached results.
     if (!empty($this->live_preview)) {
         $cache = $this->display_handler->getPlugin('cache', 'none');
     } else {
         $cache = $this->display_handler->getPlugin('cache');
     }
     if ($cache->cacheGet('results')) {
         if ($this->pager->usePager()) {
             $this->pager->total_items = $this->total_rows;
             $this->pager->updatePageInfo();
         }
     } else {
         $this->query->execute($this);
         // Enforce the array key rule as documented in
         // views_plugin_query::execute().
         $this->result = array_values($this->result);
         $this->_postExecute();
         $cache->cacheSet('results');
     }
     // Let modules modify the view just after executing it.
     $module_handler->invokeAll('views_post_execute', array($this));
     $this->executed = TRUE;
 }
Ejemplo n.º 5
0
  /**
   * {@inheritdoc}
   */
  public function getCacheMaxAge() {
    $max_age = parent::getCacheMaxAge();
    foreach ($this->getAllEntities() as $entity) {
      $max_age = Cache::mergeMaxAges($max_age, $entity->getCacheMaxAge());
    }

    return $max_age;
  }
Ejemplo n.º 6
0
 /**
  * Add settings for the ui.
  */
 public function buildOptionsForm(&$form, FormStateInterface $form_state)
 {
     parent::buildOptionsForm($form, $form_state);
     $form['disable_sql_rewrite'] = array('#title' => t('Disable SQL rewriting'), '#description' => t('Disabling SQL rewriting will disable node_access checks as well as other modules that implement hook_query_alter().'), '#type' => 'checkbox', '#default_value' => !empty($this->options['disable_sql_rewrite']), '#suffix' => '<div class="messages messages--warning sql-rewrite-warning js-hide">' . t('WARNING: Disabling SQL rewriting means that node access security is disabled. This may allow users to see data they should not be able to see if your view is misconfigured. Use this option only if you understand and accept this security risk.') . '</div>');
     $form['distinct'] = array('#type' => 'checkbox', '#title' => t('Distinct'), '#description' => t('This will make the view display only distinct items. If there are multiple identical items, each will be displayed only once. You can use this to try and remove duplicates from a view, though it does not always work. Note that this can slow queries down, so use it with caution.'), '#default_value' => !empty($this->options['distinct']));
     $form['replica'] = array('#type' => 'checkbox', '#title' => t('Use Secondary Server'), '#description' => t('This will make the query attempt to connect to a replica server if available.  If no replica server is defined or available, it will fall back to the default server.'), '#default_value' => !empty($this->options['replica']));
     $form['query_comment'] = array('#type' => 'textfield', '#title' => t('Query Comment'), '#description' => t('If set, this comment will be embedded in the query and passed to the SQL server. This can be helpful for logging or debugging.'), '#default_value' => $this->options['query_comment']);
     $form['query_tags'] = array('#type' => 'textfield', '#title' => t('Query Tags'), '#description' => t('If set, these tags will be appended to the query and can be used to identify the query in a module. This can be helpful for altering queries.'), '#default_value' => implode(', ', $this->options['query_tags']), '#element_validate' => array('views_element_validate_tags'));
 }
Ejemplo n.º 7
0
 /**
  * Creates cross-database SQL dates.
  *
  * @return string
  *   An appropriate SQL string for the db type and field type.
  */
 public function getDateField()
 {
     return $this->query->getDateField("{$this->tableAlias}.{$this->realField}");
 }
Ejemplo n.º 8
0
 /**
  * {@inheritdoc}
  */
 public function calculateDependencies()
 {
     $dependencies = parent::calculateDependencies();
     $dependencies[$this->index->getConfigDependencyKey()][] = $this->index->getConfigDependencyName();
     return $dependencies;
 }
Ejemplo n.º 9
0
 /**
  * Implements \Drupal\views\Plugin\views\query\QueryPluginBase::buildOptionsForm().
  */
 public function buildOptionsForm(&$form, &$form_state)
 {
     parent::buildOptionsForm($form, $form_state);
     $form['test_setting'] = array('#title' => t('Test setting'), '#type' => 'textfield', '#default_value' => $this->options['test_setting']);
 }
Ejemplo n.º 10
0
 /**
  * {@inheritdoc}
  */
 public function calculateDependencies()
 {
     return parent::calculateDependencies() + ['content' => ['QueryTest']];
 }
Ejemplo n.º 11
0
 /**
  * {@inheritdoc}
  */
 public function buildOptionsForm(&$form, FormStateInterface $form_state)
 {
     parent::buildOptionsForm($form, $form_state);
     $form['search_api_bypass_access'] = array('#type' => 'checkbox', '#title' => $this->t('Bypass access checks'), '#description' => $this->t('If the underlying search index has access checks enabled, this option allows you to disable them for this view.'), '#default_value' => $this->options['search_api_bypass_access']);
     if ($this->getEntityTypes(TRUE)) {
         $form['entity_access'] = array('#type' => 'checkbox', '#title' => $this->t('Additional access checks on result entities'), '#description' => $this->t("Execute an access check for all result entities. This prevents users from seeing inappropriate content when the index contains stale data, or doesn't provide access checks. However, result counts, paging and other things won't work correctly if results are eliminated in this way, so only use this as a last ressort (and in addition to other checks, if possible)."), '#default_value' => $this->options['entity_access']);
     }
     // @todo Move this setting to the argument and filter plugins where it makes
     //   more sense for users.
     $form['parse_mode'] = array('#type' => 'select', '#title' => $this->t('Parse mode'), '#description' => $this->t('Choose how the search keys will be parsed.'), '#options' => array(), '#default_value' => $this->options['parse_mode']);
     foreach ($this->query->parseModes() as $key => $mode) {
         $form['parse_mode']['#options'][$key] = $mode['name'];
         if (!empty($mode['description'])) {
             $states['visible'][':input[name="query[options][parse_mode]"]']['value'] = $key;
             $form["parse_mode_{$key}_description"] = array('#type' => 'item', '#title' => $mode['name'], '#description' => $mode['description'], '#states' => $states);
         }
     }
 }
Ejemplo n.º 12
0
 /**
  * Builds the SQL for the join this object represents.
  *
  * When possible, try to use table alias instead of table names.
  *
  * @param $select_query
  *   An select query object.
  * @param $table
  *   The base table to join.
  * @param \Drupal\views\Plugin\views\query\QueryPluginBase $view_query
  *   The source views query.
  */
 public function buildJoin($select_query, $table, $view_query)
 {
     if (empty($this->configuration['table formula'])) {
         $right_table = $this->table;
     } else {
         $right_table = $this->configuration['table formula'];
     }
     if ($this->leftTable) {
         $left = $view_query->getTableInfo($this->leftTable);
         $left_field = "{$left['alias']}.{$this->leftField}";
     } else {
         // This can be used if left_field is a formula or something. It should be used only *very* rarely.
         $left_field = $this->leftField;
     }
     $condition = "{$left_field} = {$table['alias']}.{$this->field}";
     $arguments = array();
     // Tack on the extra.
     if (isset($this->extra)) {
         if (is_array($this->extra)) {
             $extras = array();
             foreach ($this->extra as $info) {
                 // Figure out the table name. Remember, only use aliases provided
                 // if at all possible.
                 $join_table = '';
                 if (!array_key_exists('table', $info)) {
                     $join_table = $table['alias'] . '.';
                 } elseif (isset($info['table'])) {
                     // If we're aware of a table alias for this table, use the table
                     // alias instead of the table name.
                     if (isset($left) && $left['table'] == $info['table']) {
                         $join_table = $left['alias'] . '.';
                     } else {
                         $join_table = $info['table'] . '.';
                     }
                 }
                 // Convert a single-valued array of values to the single-value case,
                 // and transform from IN() notation to = notation
                 if (is_array($info['value']) && count($info['value']) == 1) {
                     if (empty($info['operator'])) {
                         $operator = '=';
                     } else {
                         $operator = $info['operator'] == 'NOT IN' ? '!=' : '=';
                     }
                     $info['value'] = array_shift($info['value']);
                 }
                 if (is_array($info['value'])) {
                     // With an array of values, we need multiple placeholders and the
                     // 'IN' operator is implicit.
                     foreach ($info['value'] as $value) {
                         $placeholder_i = ':views_join_condition_' . $select_query->nextPlaceholder();
                         $arguments[$placeholder_i] = $value;
                     }
                     $operator = !empty($info['operator']) ? $info['operator'] : 'IN';
                     $placeholder = '( ' . implode(', ', array_keys($arguments)) . ' )';
                 } else {
                     // With a single value, the '=' operator is implicit.
                     $operator = !empty($info['operator']) ? $info['operator'] : '=';
                     $placeholder = ':views_join_condition_' . $select_query->nextPlaceholder();
                     $arguments[$placeholder] = $info['value'];
                 }
                 $extras[] = "{$join_table}{$info['field']} {$operator} {$placeholder}";
             }
             if ($extras) {
                 if (count($extras) == 1) {
                     $condition .= ' AND ' . array_shift($extras);
                 } else {
                     $condition .= ' AND (' . implode(' ' . $this->extraOperator . ' ', $extras) . ')';
                 }
             }
         } elseif ($this->extra && is_string($this->extra)) {
             $condition .= " AND ({$this->extra})";
         }
     }
     $select_query->addJoin($this->type, $right_table, $table['alias'], $condition, $arguments);
 }