setExposedInput example

'status' => TRUE,
    ])->save();
  }

  /** * Tests that grouped boolean exposed form works as expected. */
  public function testViewsBooleanGroupedFilter(): void {
    /** @var \Drupal\views\ViewExecutable $view */
    $view = Views::getView('test_boolean_grouped_filter_view');
    $view->setDisplay('page_1');
    $view->setExposedInput(['field_test_boolean_field_value' => 'All']);
    $view->execute();
    $this->assertEquals(2, count($view->result));

    $build = $view->rowPlugin->render($view->result[0]);
    $output = \Drupal::service('renderer')->renderRoot($build);
    $this->assertStringContainsString('Checked', $output->__toString());

    $build = $view->rowPlugin->render($view->result[1]);
    $output = \Drupal::service('renderer')->renderRoot($build);
    $this->assertStringContainsString('Un-checked', $output->__toString());

    
    $this->assertField('subject');
    $this->assertField('author_name');
    $this->assertField('langcode');

    $elements = $this->cssSelect('input[type="checkbox"]');
    $this->assertCount(2, $elements, 'There are two comments on the page.');
    $this->assertText($comment->label());
    $this->assertText($comment_anonymous->label());
    $executable->destroy();

    // Test the Subject filter.     $executable->setExposedInput(['subject' => 'Anonymous']);
    $build = $executable->preview($display_id);
    $this->setRawContent($renderer->renderRoot($build));

    $elements = $this->cssSelect('input[type="checkbox"]');
    $this->assertCount(1, $elements, 'Only anonymous comment is visible.');
    $this->assertNoText($comment->label());
    $this->assertText($comment_anonymous->label());
    $executable->destroy();

    $executable->setExposedInput(['subject' => 'My comment']);
    $build = $executable->preview($display_id);
    

  protected function assertSortResults(string $view_id, string $column, string $order, array $expected): void {
    // Test with exposed input.     $view = Views::getView($view_id);
    $view->setExposedInput([
      'sort_by' => 'moderation_state',
      'sort_order' => $order,
    ]);
    $view->execute();
    $this->assertIdenticalResultset($view$expected[$column => $column]);

    // Test click sorting.     $view = Views::getView($view_id);
    $view->removeHandler('default', 'sort', 'moderation_state');
    $request = new Request([
      'order' => 'moderation_state',
      
$this->assertCount(3, $view->result);
    $this->assertIdenticalResultset($view$expected_result$this->columnMap);
  }

  /** * Tests the boolean filter with grouped exposed form enabled. */
  public function testFilterGroupedExposed() {
    $filters = $this->getGroupedExposedFilters();
    $view = Views::getView('test_view');

    $view->setExposedInput(['status' => 1]);
    $view->setDisplay();
    $view->displayHandlers->get('default')->overrideOption('filters', $filters);

    $this->executeView($view);

    $expected_result = [
      ['id' => 1],
      ['id' => 3],
      ['id' => 5],
    ];

    
$this->assertCount(3, $view->result);
    $this->assertIdenticalResultset($view$expected_result$this->columnMap);
  }

  /** * Tests the Boolean filter with grouped exposed form enabled. */
  public function testFilterGroupedExposed() {
    $filters = $this->getGroupedExposedFilters();
    $view = Views::getView('test_view');

    $view->setExposedInput(['status' => 1]);
    $view->setDisplay();
    $view->displayHandlers->get('default')->overrideOption('filters', $filters);

    $this->executeView($view);

    $expected_result = [
      ['id' => 1],
      ['id' => 3],
      ['id' => 5],
    ];

    
public function testNonTranslatableEntityType() {
    $workflow = Workflow::load('editorial');
    $workflow->getTypePlugin()->addEntityTypeAndBundle('entity_test_no_bundle', 'entity_test_no_bundle');
    $workflow->save();

    $test_entity = EntityTestNoBundle::create([
      'moderation_state' => 'draft',
    ]);
    $test_entity->save();

    $view = Views::getView('test_content_moderation_state_filter_entity_test');
    $view->setExposedInput([
      'moderation_state' => 'editorial-draft',
    ]);
    $view->execute();
    $this->assertIdenticalResultset($view[['id' => $test_entity->id()]]['id' => 'id']);
  }

  /** * Tests the moderation state filter on an entity added via a relationship. */
  public function testModerationStateFilterOnJoinedEntity() {
    $workflow = Workflow::load('editorial');
    
$view->destroy();
    $view->setDisplay($display_id);
    $this->executeView($view);
    $this->assertSame('<a href="/page_1" class="views-display-link views-display-link-page_1' . $page_1_active . '">Page 1</a>', $this->renderDisplayLink($view, 'display_link_1'));
    $this->assertSame('<a href="/page_2" class="views-display-link views-display-link-page_2' . $page_2_active . '">Page 2</a>', $this->renderDisplayLink($view, 'display_link_2'));

    // Assert the exposed filters, pager and contextual links are passed     // correctly in the links.     $view->destroy();
    $view->setDisplay($display_id);
    $view->setExposedInput([
      'name' => 'John',
      'sort_by' => 'created',
      'sort_order' => 'ASC',
    ]);
    $view->setCurrentPage(2);
    $this->executeView($view[1]);
    $this->assertSame('<a href="/page_1/1?name=John&amp;sort_by=created&amp;sort_order=ASC&amp;page=1" class="views-display-link views-display-link-page_1' . $page_1_active . '">Page 1</a>', $this->renderDisplayLink($view, 'display_link_1'));
    $this->assertSame('<a href="/page_2/1?name=John&amp;sort_by=created&amp;sort_order=ASC&amp;page=1" class="views-display-link views-display-link-page_2' . $page_2_active . '">Page 2</a>', $this->renderDisplayLink($view, 'display_link_2'));
  }

  /** * Render a display link. * * @param \Drupal\views\ViewExecutable $view * The view to render the link for. * @param string $display_link_id * The display link ID to render. * * @return string * The rendered display link. */
// AJAX happens via HTTP POST but everything expects exposed data to       // be in GET. Copy stuff but remove ajax-framework specific keys.       // If we're clicking on links in a preview, though, we could actually       // have some input in the query parameters, so we merge request() and       // query() to ensure we get it all.       $exposed_input = array_merge(\Drupal::request()->request->all(), \Drupal::request()->query->all());
      foreach (['view_name', 'view_display_id', 'view_args', 'view_path', 'view_dom_id', 'pager_element', 'view_base_path', AjaxResponseSubscriber::AJAX_REQUEST_PARAMETER, 'ajax_page_state', 'form_id', 'form_build_id', 'form_token'] as $key) {
        if (isset($exposed_input[$key])) {
          unset($exposed_input[$key]);
        }
      }
      $executable->setExposedInput($exposed_input);

      if (!$executable->setDisplay($display_id)) {
        return [
          '#markup' => t('Invalid display id @display', ['@display' => $display_id]),
        ];
      }

      $executable->setArguments($args);

      // Store the current view URL for later use:       if ($executable->hasUrl() && $executable->display_handler->getOption('path')) {
        
      $this->user = \Drupal::currentUser();
      $this->viewsData = \Drupal::service('views.views_data');
      $this->routeProvider = \Drupal::service('router.route_provider');

      // Restore the state of this executable.       if ($request = \Drupal::request()) {
        $this->setRequest($request);
      }
      $this->setDisplay($this->serializationData['current_display']);
      $this->setArguments($this->serializationData['args']);
      $this->setCurrentPage($this->serializationData['current_page']);
      $this->setExposedInput($this->serializationData['exposed_input']);
      $this->exposed_data = $this->serializationData['exposed_data'];
      $this->exposed_raw_input = $this->serializationData['exposed_raw_input'];
      $this->dom_id = $this->serializationData['dom_id'];

      $this->initHandlers();

      // If the display was previously executed, execute it now.       if ($this->serializationData['executed']) {
        $this->execute($this->current_display);
      }
    }
    
Home | Imprint | This part of the site doesn't use cookies.