initQuery example

if (!isset($field)) {
      if (!empty($this->formula)) {
        $field = $this->getFormula();
      }
      else {
        $field = $this->tableAlias . '.' . $this->realField;
      }
    }

    // If grouping, check to see if the aggregation method needs to modify the field.     if ($this->view->display_handler->useGroupBy()) {
      $this->view->initQuery();
      if ($this->query) {
        $info = $this->query->getAggregationInfo();
        if (!empty($info[$this->options['group_type']]['method'])) {
          $method = $info[$this->options['group_type']]['method'];
          if (method_exists($this->query, $method)) {
            return $this->query->$method($this->options['group_type']$field);
          }
        }
      }
    }

    
$this->assertEquals($field->relationship, $view->relationship['valid_relationship']->alias, 'Make sure that a valid relationship does create the right relationship query alias.');
  }

  /** * Tests the placeholder function. * * @see \Drupal\views\Plugin\views\HandlerBase::placeholder() */
  public function testPlaceholder() {
    $view = Views::getView('test_view');
    $view->initHandlers();
    $view->initQuery();

    $handler = $view->field['name'];
    $table = $handler->table;
    $field = $handler->field;
    $string = ':' . $table . '_' . $field;

    // Make sure the placeholder variables are like expected.     $this->assertEquals($string$handler->getPlaceholder());
    $this->assertEquals($string . 1, $handler->getPlaceholder());
    $this->assertEquals($string . 2, $handler->getPlaceholder());

    
return $status;
  }

  /** * Gets the current query plugin. * * @return \Drupal\views\Plugin\views\query\QueryPluginBase * The current query plugin. */
  public function getQuery() {
    if (!isset($this->query)) {
      $this->initQuery();
    }

    return $this->query;
  }

  /** * Initializes the query object for the view. * * @return true * Always returns TRUE. */
  

  protected function assertGrid(ViewExecutable $view, string $alignment, int $columns): void {
    $view->setDisplay('default');
    $view->initStyle();
    $view->initHandlers();
    $view->initQuery();
    $view->style_plugin->options['alignment'] = $alignment;
    $view->style_plugin->options['columns'] = $columns;
    $this->executeView($view);
    $output = $view->preview();
    $output = \Drupal::service('renderer')->renderRoot($output);
    $this->setRawContent($output);
    if (!in_array($alignment$this->alignmentsTested)) {
      $result = $this->xpath('//div[contains(@class, "views-view-grid") and contains(@class, :alignment) and contains(@class, :columns)]', [':alignment' => $alignment, ':columns' => 'cols-' . $columns]);
      $this->assertGreaterThan(0, count($result)ucfirst($alignment) . " grid markup detected.");
      $this->alignmentsTested[] = $alignment;
    }
    
/** * Tests base join functionality. * * This duplicates parts of * \Drupal\Tests\views\Kernel\Plugin\JoinTest::testBasePlugin() to ensure that * no functionality provided by the base join plugin is broken. */
  public function testBase() {
    // Setup a simple join and test the result sql.     $view = Views::getView('test_view');
    $view->initDisplay();
    $view->initQuery();

    // First define a simple join without an extra condition.     // Set the various options on the join object.     $configuration = [
      'left_table' => 'views_test_data',
      'left_field' => 'uid',
      'table' => 'users_field_data',
      'field' => 'uid',
      'adjusted' => TRUE,
    ];
    $join = $this->manager->createInstance($this->pluginId, $configuration);
    
'#tree' => TRUE,
        '#theme_wrappers' => ['container'],
        '#attributes' => ['class' => ['scroll'], 'data-drupal-views-scroll' => TRUE],
      ],
    ];
    $executable = $view->getExecutable();
    if (!$executable->setDisplay($display_id)) {
      $form['markup'] = ['#markup' => $this->t('Invalid display id @display', ['@display' => $display_id])];
      return $form;
    }

    $executable->initQuery();

    $item = $executable->getHandler($display_id$type$id);

    if ($item) {
      $handler = $executable->display_handler->getHandler($type$id);
      if (empty($handler)) {
        $form['markup'] = ['#markup' => $this->t("Error: handler for @table > @field doesn't exist!", ['@table' => $item['table'], '@field' => $item['field']])];
      }
      else {
        $handler->init($executable$executable->display_handler, $item);
        $types = ViewExecutable::getHandlerTypes();

        
$display_hash = spl_object_hash($view->display_handler);

    // Test the initStyle() method.     $view->initStyle();
    $this->assertInstanceOf(DefaultStyle::class$view->style_plugin);
    // Test the plugin has been invited and view have references to the view and     // display handler.     $this->assertEquals($view_hashspl_object_hash($view->style_plugin->view));
    $this->assertEquals($display_hashspl_object_hash($view->style_plugin->displayHandler));

    // Test the initQuery method().     $view->initQuery();
    $this->assertInstanceOf(Sql::class$view->query);
    $this->assertEquals($view_hashspl_object_hash($view->query->view));
    $this->assertEquals($display_hashspl_object_hash($view->query->displayHandler));

    $view->destroy();

    // Test the plugin get methods.     $display_plugin = $view->getDisplay();
    $this->assertInstanceOf(DefaultDisplay::class$display_plugin);
    $this->assertInstanceOf(DefaultDisplay::class$view->display_handler);
    $this->assertSame($display_plugin$view->getDisplay(), 'The same display plugin instance was returned.');

    
public static $testViews = ['test_table'];

  /** * Tests the table style. */
  public function testTable() {
    $view = Views::getView('test_table');

    $view->setDisplay('default');
    $view->initStyle();
    $view->initHandlers();
    $view->initQuery();
    $style_plugin = $view->style_plugin;

    // Test the buildSort() method.     $request = new Request();
    $view->setRequest($request);

    $style_plugin->options['override'] = FALSE;

    $style_plugin->options['default'] = '';
    $this->assertTrue($style_plugin->buildSort(), 'If no order and no default order is specified, the normal sort should be used.');

    
return $data;
  }

  /** * Tests adding some metadata/tags to the views query. */
  public function testExecuteMetadata() {
    $view = Views::getView('test_view');
    $view->setDisplay();

    $view->initQuery();
    $view->execute();
    /** @var \Drupal\Core\Database\Query\Select $query */
    $main_query = $view->build_info['query'];
    /** @var \Drupal\Core\Database\Query\Select $count_query */
    $count_query = $view->build_info['count_query'];

    foreach ([$main_query$count_query] as $query) {
      // Check query access tags.       $this->assertTrue($query->hasTag('test_tag'));

      // Check metadata.
// For footer/header $handler_type is area but $type is footer/header.     // For all other handle types it's the same.     $handler_type = $type = $form_state->get('type');
    if (!empty($types[$type]['type'])) {
      $handler_type = $types[$type]['type'];
    }

    $override = NULL;
    $executable = $view->getExecutable();
    if ($executable->display_handler->useGroupBy() && !empty($item['group_type'])) {
      if (empty($executable->query)) {
        $executable->initQuery();
      }
      $aggregate = $executable->query->getAggregationInfo();
      if (!empty($aggregate[$item['group_type']]['handler'][$type])) {
        $override = $aggregate[$item['group_type']]['handler'][$type];
      }
    }

    // Create a new handler and unpack the options from the form onto it. We     // can use that for storage.     $handler = Views::handlerManager($handler_type)->getHandler($item$override);
    $handler->init($executable$executable->display_handler, $item);

    

  public function testResponsiveGrid(array $options, array $expected): void {
    // Create and preview a View with the provided options.     $view = Views::getView('test_grid_responsive');
    $view->setDisplay('default');
    $view->initStyle();
    $view->initHandlers();
    $view->initQuery();
    $view->style_plugin->options = $options + $view->style_plugin->options;
    $this->executeView($view);
    $output = $view->preview();
    $output = \Drupal::service('renderer')->renderRoot($output);
    $this->setRawContent($output);

    // Confirm that the alignment class is added.     $result = $this->xpath('//div[contains(@class, "views-view-responsive-grid") and contains(@class, :alignment)]', [':alignment' => 'views-view-responsive-grid--' . $expected['alignment']]);
    $this->assertGreaterThan(0, count($result), "Alignment CSS variable value is detected and correct.");

    // Check for CSS variables in style attribute.
$this->_testQueryExecute();
    $this->queryMethodsTests();
  }

  /** * Tests the ViewExecutable::initQuery method. */
  public function _testInitQuery() {
    $view = Views::getView('test_view');
    $view->setDisplay();

    $view->initQuery();
    $this->assertInstanceOf(QueryTestPlugin::class$view->query);
  }

  public function _testQueryExecute() {
    $view = Views::getView('test_view');
    $view->setDisplay();

    $view->initQuery();
    $view->query->setAllItems($this->dataSet());

    $this->executeView($view);
    
if ($info['id'] != $id) {
          $info['id'] = $id;
        }

        // If aggregation is on, the group type might override the actual         // handler that is in use. This piece of code checks that and,         // if necessary, sets the override handler.         $override = NULL;
        if ($this->useGroupBy() && !empty($info['group_type'])) {
          if (empty($this->view->query)) {
            $this->view->initQuery();
          }
          $aggregate = $this->view->query->getAggregationInfo();
          if (!empty($aggregate[$info['group_type']]['handler'][$type])) {
            $override = $aggregate[$info['group_type']]['handler'][$type];
          }
        }

        if (!empty($types[$type]['type'])) {
          $handler_type = $types[$type]['type'];
        }
        else {
          
    $this->manager = $this->container->get('plugin.manager.views.join');
  }

  /** * Tests an example join plugin. */
  public function testExamplePlugin() {

    // Setup a simple join and test the result sql.     $view = Views::getView('test_view');
    $view->initDisplay();
    $view->initQuery();

    $configuration = [
      'left_table' => 'views_test_data',
      'left_field' => 'uid',
      'table' => 'users_field_data',
      'field' => 'uid',
    ];
    $join = $this->manager->createInstance('join_test', $configuration);
    $this->assertInstanceOf(JoinTestPlugin::class$join);

    $rand_int = rand(0, 1000);
    
$this->assertEquals($rand_number$view->getOffset(), 'Make sure getOffset uses the settings of setOffset.');

    $this->assertNull($view->getCurrentPage(), 'If the pager is not initialized and no manual override there is no current page.');
    $rand_number = rand(1, 5);
    $view->setCurrentPage($rand_number);
    $this->assertEquals($rand_number$view->getCurrentPage(), 'Make sure getCurrentPage uses the settings of set_current_page.');

    $view->destroy();

    // On this round enable the pager.     $view->initDisplay();
    $view->initQuery();
    $view->initPager();

    $this->assertEquals(5, $view->getItemsPerPage(), 'Per default the view has 5 items per page.');
    $rand_number = rand(1, 5);
    $view->setItemsPerPage($rand_number);
    $rand_number = rand(6, 11);
    $view->pager->setItemsPerPage($rand_number);
    $this->assertEquals($rand_number$view->getItemsPerPage(), 'Make sure getItemsPerPage uses the settings of setItemsPerPage.');

    $this->assertEquals(0, $view->getOffset(), 'Per default a view has a 0 offset.');
    $rand_number = rand(1, 5);
    
Home | Imprint | This part of the site doesn't use cookies.