getView example

$data['views_test_data']['id']['field']['click sortable'] = TRUE;
    return $data;
  }

  /** * Tests that the render function is called. */
  public function testRender() {
    /** @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = \Drupal::service('renderer');

    $view = Views::getView('test_field_tokens');
    $this->executeView($view);

    $random_text = $this->randomMachineName();
    $view->field['job']->setTestValue($random_text);
    $output = $renderer->executeInRenderContext(new RenderContext()function D) use ($view) {
      return $view->field['job']->theme($view->result[0]);
    });
    $this->assertEquals($random_text$output, 'Make sure the render method rendered the manual set value.');
  }

  /** * Tests all things related to the query. */
$node2->addTranslation('fr', [
      'title' => $node2->title->value,
      'field_name_1' => 'field name 1: fr',
    ])->save();

    $map = [
      'nid' => 'nid',
      'langcode' => 'langcode',
    ];

    $view = Views::getView('test_field_config_translation_filter');

    // Filter by 'field name 1: es'.     $view->setDisplay('embed_1');
    $this->executeView($view);
    $expected = [
      [
        'nid' => $node1->id(),
        'langcode' => 'es',
      ],
      [
        'nid' => $node2->id(),
        
/** * {@inheritdoc} */
  public static $testViews = ['test_argument_datetime', 'test_filter_datetime', 'test_sort_datetime'];

  /** * Tests argument plugin schema. */
  public function testDateTimeSchema() {
    // Test argument schema.     $view = Views::getView('test_argument_datetime');
    $view->initHandlers();
    $view->setDisplay('default');
    $arguments = $view->displayHandlers->get('default')->getOption('arguments');
    $arguments['field_date_value_year']['date'] = 'Date';
    $view->displayHandlers->get('default')->overrideOption('arguments', $arguments);
    $view->save();
    $this->assertConfigSchemaByName('views.view.test_argument_datetime');

    // Test filter schema.     $view = Views::getView('test_filter_datetime');
    $view->initHandlers();
    

      );
    }
  }

  /** * Tests the node path plugin functionality when converted to entity link. */
  public function testPathPlugin() {
    /** @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = $this->container->get('renderer');
    $view = Views::getView('test_node_path_plugin');

    // The configured deprecated node path plugin should be converted to the     // entity link plugin.     $field = $view->getHandler('page_1', 'field', 'path');
    $this->assertEquals('entity_link', $field['plugin_id']);

    $view->initDisplay();
    $view->setDisplay('page_1');
    $view->initStyle();
    $view->rowPlugin->options['view_mode'] = 'full';

    

    ]);
    $view->save();

    /** @var \Drupal\Core\Session\AccountSwitcherInterface $account_switcher */
    $account_switcher = \Drupal::service('account_switcher');

    /** @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = \Drupal::service('renderer');

    $account_switcher->switchTo($this->adminUser);
    $executable = Views::getView($view_id);
    $build = $executable->preview();
    $this->setRawContent($renderer->renderRoot($build));

    $this->assertLink('My comment title');
    $this->assertLink('Anonymous comment title');
    // Display plugin of the view is showing the name field. When comment     // belongs to an authenticated user the name field has no value.     $comment_author = $this->xpath('//div[contains(@class, :class)]/span[normalize-space(text())=""]', [
      ':class' => 'views-field-subject',
    ]);
    $this->assertNotEmpty($comment_author);
    


  /** * Tests the computed field handler. */
  public function testComputedFieldHandler() {
    \Drupal::state()->set('entity_test_computed_field_item_list_value', ['computed string']);

    $entity = EntityTestComputedField::create([]);
    $entity->save();

    $view = Views::getView('computed_field_view');

    $rendered_view = $view->preview();
    $output = $this->container->get('renderer')->renderRoot($rendered_view);
    $this->assertStringContainsString('computed string', (string) $output);
  }

}
Role::create(['id' => 123, 'label' => 'Numeric'])
      ->save();

    $user = User::create([
      'uid' => 2,
      'name' => 'foo',
      'roles' => 123,
    ]);
    $user->save();

    $view = Views::getView('user_admin_people');
    $this->executeView($view);
    $view->render('user_admin_people');
    $output = $view->field['roles_target_id']->render($view->result[0]);
    $this->assertEquals(2, $output);
  }

}
    $view = [];
    $view['label'] = $this->randomMachineName(16);
    $view['id'] = strtolower($this->randomMachineName(16));
    $view['show[wizard_key]'] = 'node';
    $view['page[create]'] = TRUE;
    $view['page[style][style_plugin]'] = 'table';
    $view['page[title]'] = $this->randomMachineName(16);
    $view['page[path]'] = $view['id'];
    $this->drupalGet('admin/structure/views/add');
    $this->submitForm($view, 'Save and edit');

    $view = Views::getView($view['id']);
    $view->initHandlers();
    $this->assertEquals('Title', $view->field['title']->options['label'], 'The field label for table styles is not empty.');
  }

}
$node2->save();

    // Create one node on which the author has neither authorship of revisions     // or the main node.     $node3 = Node::create([
      'type' => 'default',
      'title' => $this->randomMachineName(),
    ]);
    $node3->setOwner($no_author);
    $node3->save();

    $view = Views::getView('test_argument_node_uid_revision');
    $view->initHandlers();
    $view->setArguments(['uid_revision' => $author->id()]);

    $this->executeView($view);
    $this->assertIdenticalResultset($view$expected_result['nid' => 'nid']);
  }

}

  protected static $modules = ['user'];

  /** * Views used by this test. * * @var array */
  public static $testViews = ['test_view'];

  public function testSimple() {
    $view = Views::getView('test_view');
    $view->setDisplay();
    $view->displayHandlers->get('default')->overrideOption('fields', [
      'counter' => [
        'id' => 'counter',
        'table' => 'views',
        'field' => 'counter',
        'relationship' => 'none',
      ],
      'name' => [
        'id' => 'name',
        'table' => 'views_test_data',
        

  public function viewsData() {
    $data = parent::viewsData();
    $data['views_test_data']['name']['field']['id'] = 'custom';
    return $data;
  }

  /** * Ensure that custom fields work and doesn't escape unnecessary markup. */
  public function testFieldCustom() {
    $view = Views::getView('test_view');
    $view->setDisplay();

    // Alter the text of the field to a random string.     $random = '<div>' . $this->randomMachineName() . '</div>';
    $view->displayHandlers->get('default')->overrideOption('fields', [
      'name' => [
        'id' => 'name',
        'table' => 'views_test_data',
        'field' => 'name',
        'relationship' => 'none',
        'alter' => [
          
$filter_end_date['id'] = $field_end;
    $filter_end_date['field'] = $field_end;

    $view->getDisplay('default')['display_options']['filters'][$field_end] = $filter_end_date;
    $view->save();
  }

  /** * Tests offsets with date-only fields. */
  public function testDateOffsets() {
    $view = Views::getView('test_filter_datetime');
    $field_start = static::$fieldName . '_value';
    $field_end = static::$fieldName . '_end_value';

    // Test simple operations.     $view->initHandlers();

    // Search nodes with:     // - start date greater than or equal to 'yesterday'.     // - end date lower than or equal to 'today'.     // Expected results: nodes 0 and 1.     $view->filter[$field_start]->operator = '>=';
    
$node->save();
      $this->nodes[] = $node;
    }
  }

  /** * Tests year argument. * * @see \Drupal\datetime\Plugin\views\argument\YearDate */
  public function testDatetimeArgumentYear() {
    $view = Views::getView('test_argument_datetime');

    // The 'default' display has the 'year' argument.     $view->setDisplay('default');
    $this->executeView($view['2000']);
    $expected = [];
    $expected[] = ['nid' => $this->nodes[0]->id()];
    $this->assertIdenticalResultset($view$expected$this->map);
    $view->destroy();

    $view->setDisplay('default');
    $this->executeView($view['2002']);
    
$expected_result[] = ['nid' => $node->id()];
    $revision = clone $node;
    // Force to add a new revision.     $revision->set('vid', NULL);
    $revision->set('revision_uid', $author->id());
    $revision->save();

    // Create one node on which the author has neither authorship of revisions     // or the main node.     $this->createNode(['uid' => $no_author->id()]);

    $view = Views::getView('test_filter_node_uid_revision');
    $view->initHandlers();
    $view->filter['uid_revision']->value = [$author->id()];

    $view->preview();
    $this->assertIdenticalResultset($view$expected_result['nid' => 'nid'], 'Make sure that the view only returns nodes which match either the node or the revision author.');
  }

}
// Ensure entities have different ids.     if ($entity_test->id() == $user->id()) {
      $entity_test->delete();
      $entity_test = EntityTest::create([
        'user_id' => $user->id(),
        'name' => 'test entity test',
      ]);
      $entity_test->save();
    }

    $view = Views::getView('test_entity_row');
    $build = $view->preview();
    $this->render($build);

    $this->assertText('test entity test');
    $this->assertNoText('Member for');

    // Change the view to use a relationship to render the row.     $view = Views::getView('test_entity_row');
    $display = &$view->storage->getDisplay('default');
    $display['display_options']['row']['type'] = 'entity:user';
    $display['display_options']['row']['options']['relationship'] = 'user_id';
    
Home | Imprint | This part of the site doesn't use cookies.