advancedRender example

$name_field = $view->field['name'];

    // Tests stripping of html elements.     $this->executeView($view);
    $random_text = $this->randomMachineName();
    $name_field->options['alter']['alter_text'] = TRUE;
    $name_field->options['alter']['text'] = $html_text = '<div class="views-test">' . $random_text . '</div>';
    $row = $view->result[0];

    $name_field->options['alter']['strip_tags'] = TRUE;
    $output = $renderer->executeInRenderContext(new RenderContext()function D) use ($name_field$row) {
      return $name_field->advancedRender($row);
    });
    $this->assertSubString($output$random_text, 'Find text without html if stripping of views field output is enabled.');
    $this->assertNotSubString($output$html_text, 'Find no text with the html if stripping of views field output is enabled.');

    // Tests preserving of html tags.     $name_field->options['alter']['preserve_tags'] = '<div>';
    $output = $renderer->executeInRenderContext(new RenderContext()function D) use ($name_field$row) {
      return $name_field->advancedRender($row);
    });
    $this->assertSubString($output$random_text, 'Find text without html if stripping of views field output is enabled but a div is allowed.');
    $this->assertSubString($output$html_text, 'Find text with the html if stripping of views field output is enabled but a div is allowed.');

    
$view = Views::getView('test_view');
    $view->setDisplay();
    $view->displayHandlers->get('default')->overrideOption('fields', [
      'langcode' => [
        'id' => 'langcode',
        'table' => 'views_test_data',
        'field' => 'langcode',
      ],
    ]);
    $this->executeView($view);

    $this->assertEquals('English', $view->field['langcode']->advancedRender($view->result[0]));
    $this->assertEquals('Lolspeak', $view->field['langcode']->advancedRender($view->result[1]));
  }

}
'field_name' => $comment2->getFieldName(),
      ],
    ];
    $column_map = [
      'cid' => 'cid',
      'comment_field_data_field_name' => 'field_name',
    ];
    $this->assertIdenticalResultset($view$expected_result$column_map);

    // Test that data rendered correctly.     $expected_output = $renderer->executeInRenderContext(new RenderContext()function D) use ($view) {
      return $view->field['field_name']->advancedRender($view->result[0]);
    });
    $this->assertEquals($expected_output$comment->getFieldName());
    $expected_output = $renderer->executeInRenderContext(new RenderContext()function D) use ($view) {
      return $view->field['field_name']->advancedRender($view->result[1]);
    });
    $this->assertEquals($expected_output$comment2->getFieldName());
  }

}
public function testRenderAsLinkWithoutPath() {
    $alter = [
      'make_link' => TRUE,
    ];

    $this->setUpUrlIntegrationServices();
    $field = $this->setupTestField(['alter' => $alter]);
    $field->field_alias = 'key';
    $row = new ResultRow(['key' => 'value']);

    $expected_result = 'value';
    $result = $field->advancedRender($row);
    $this->assertEquals($expected_result$result);
  }

  /** * Tests rendering with a more link. * * @param string $path * An internal or external path. * @param string $url * The final URL used by the more link. * * @dataProvider providerTestRenderTrimmedWithMoreLinkAndPath * @covers ::renderText */
/** * Test TimeInterval handler. */
  public function testFieldTimeInterval() {
    $view = Views::getView('test_view');
    $view->setDisplay();
    $this->executeView($view);
    foreach ($view->result as $delta => $row) {
      [$value$formatted_value$granularity] = $this->ages[$delta];
      $view->field['age']->options['granularity'] = $granularity;
      $this->assertEquals($formatted_value$view->field['age']->advancedRender($row));
    }
  }

  /** * Overrides \Drupal\views\Tests\ViewUnitTestBase::schemaDefinition(). */
  protected function schemaDefinition() {
    $schema_definition = parent::schemaDefinition();
    $schema_definition['views_test_data']['fields']['age']['not null'] = FALSE;
    return $schema_definition;
  }

  

  protected function assertRenderedDatesEqual(ViewExecutable $view, array $map, ?string $timezone = NULL): void {
    foreach ($map as $date_format => $expected_result) {
      $view->field['created']->options['date_format'] = $date_format;
      if (isset($timezone)) {
        $message = "$date_format format for timezone $timezone matches.";
        $view->field['created']->options['timezone'] = $timezone;
      }
      else {
        $message = "$date_format format matches.";
      }
      $actual_result = $view->field['created']->advancedRender($view->result[0]);
      $this->assertEquals($expected_resultstrip_tags($actual_result)$message);
    }
  }

  /** * Asserts properly formatted display against 'destroyed' field in view. * * @param \Drupal\views\ViewExecutable $view * View to be tested. * @param array $map * Data map. * * @internal */
$view->preExecute([]);
    $view->execute();

    $renderer = $this->container->get('renderer');
    $dropbutton_output = [];

    // Render each row and field in turn - the dropbutton plugin relies on     // output being set in previous versions.     foreach ($view->result as $index => $row) {
      foreach (array_keys($view->field) as $field) {
        $output = $renderer->executeInRenderContext(new RenderContext()function D) use ($view$row$field) {
          return $view->field[$field]->advancedRender($row);
        });
        if ($field === 'dropbutton') {
          $dropbutton_output[] = $output;
        }
      }
    }

    // The first row should contain edit links to node 3, as the user has     // access.     $this->assertStringContainsString($this->node3->toUrl('edit-form')->toString()(string) $dropbutton_output[0]);
    $this->assertStringContainsString($this->node3->toUrl('delete-form')->toString()(string) $dropbutton_output[0]);

    
$view->displayHandlers->get('default')->overrideOption('fields', [
      'age' => [
        'id' => 'age',
        'table' => 'views_test_data',
        'field' => 'age',
      ],
    ]);

    $this->executeView($view);

    // Test with the formatted option.     $this->assertEquals('', $view->field['age']->advancedRender($view->result[0]));
    $this->assertEquals('10 bytes', $view->field['age']->advancedRender($view->result[1]));
    $this->assertEquals('1000 bytes', $view->field['age']->advancedRender($view->result[2]));
    $this->assertEquals('9.77 KB', $view->field['age']->advancedRender($view->result[3]));
    // Test with the bytes option.     $view->field['age']->options['file_size_display'] = 'bytes';
    $this->assertEquals('', $view->field['age']->advancedRender($view->result[0]));
    $this->assertEquals('10', $view->field['age']->advancedRender($view->result[1]));
    $this->assertEquals('1000', $view->field['age']->advancedRender($view->result[2]));
    $this->assertEquals('10000', $view->field['age']->advancedRender($view->result[3]));
  }

}
'name' => [
        'id' => 'name',
        'table' => 'views_test_data',
        'field' => 'name',
        'relationship' => 'none',
        'display_as_link' => FALSE,
      ],
    ]);

    $this->executeView($view);

    $this->assertEquals('John', $view->field['name']->advancedRender($view->result[0]));

    // Make the URL a link.     $view->destroy();
    $view->setDisplay();

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

    if (!empty($field_settings)) {
      $view->displayHandlers->get('default')->overrideOption('fields', ['age' => $field_settings]);
    }
    $this->executeView($view);

    foreach ($values as $key => $value) {
      $view->result[0]->views_test_data_age = $value;
      $this->assertSame($expected_values[$key]$view->field['age']->advancedRender($view->result[0]));
    }
  }

  /** * Data provider for testFieldNumeric. * * @return array * The data set containing field settings, values to set and expected * values. */
  public function providerTestFieldNumeric() {
    
$view->initHandlers();
    $view->field['name']->options['link_to_user'] = TRUE;
    $view->field['name']->options['type'] = 'user_name';
    $view->field['name']->init($view$view->getDisplay('default'));
    $view->field['name']->options['id'] = 'name';
    $this->executeView($view);

    $anon_name = $this->config('user.settings')->get('anonymous');
    $view->result[0]->_entity->setUsername('');
    $view->result[0]->_entity->uid->value = 0;
    $render = $renderer->executeInRenderContext(new RenderContext()function D) use ($view) {
      return $view->field['name']->advancedRender($view->result[0]);
    });
    $this->assertStringContainsString($anon_name$render, 'For user 0 it should use the default anonymous name by default.');

    $render = $renderer->executeInRenderContext(new RenderContext()function D) use ($view$new_user) {
      return $view->field['name']->advancedRender($view->result[$new_user->id()]);
    });
    $this->assertStringContainsString($new_user->getDisplayName()$render, 'If link to user is checked the username should be part of the output.');
    $this->assertStringContainsString('user/' . $new_user->id()$render, 'If link to user is checked the link to the user should appear as well.');

    $view->field['name']->options['link_to_user'] = FALSE;
    $view->field['name']->options['type'] = 'string';
    
$view = Views::getView('test_field_argument_tokens');
    $this->executeView($view['{{ { "#pre_render": ["\Drupal\views_test_data\Controller\ViewsTestDataController::preRender"]} }}']);

    $name_field_0 = $view->field['name'];

    // Test the old style tokens.     $name_field_0->options['alter']['alter_text'] = TRUE;
    $name_field_0->options['alter']['text'] = '%1 !1';

    $row = $view->result[0];
    $output = $renderer->executeInRenderContext(new RenderContext()function D) use ($name_field_0$row) {
      return $name_field_0->advancedRender($row);
    });

    $this->assertStringNotContainsString('\Drupal\views_test_data\Controller\ViewsTestDataController::preRender executed', (string) $output, 'Ensure that the pre_render function was not executed');
    $this->assertEquals('%1 !1', (string) $output, "Ensure that old style placeholders aren't replaced");

    // This time use new style tokens but ensure that we still don't allow     // arbitrary code execution.     $name_field_0->options['alter']['alter_text'] = TRUE;
    $name_field_0->options['alter']['text'] = '{{ arguments.null }} {{ raw_arguments.null }}';

    $row = $view->result[0];
    
/** * Tests the taxonomy field handler. */
  public function testViewsHandlerTidField() {
    /** @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = \Drupal::service('renderer');

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

    $actual = $renderer->executeInRenderContext(new RenderContext()function D) use ($view) {
      return $view->field['name']->advancedRender($view->result[0]);
    });
    $expected = Link::fromTextAndUrl($this->term1->label()$this->term1->toUrl());

    $this->assertEquals($expected->toString()$actual);
  }

}
/** * Tests the field handling for the Vocabulary ID. */
  public function testViewsHandlerVidField() {
    /** @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = \Drupal::service('renderer');

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

    $actual = $renderer->executeInRenderContext(new RenderContext()function D) use ($view) {
      return $view->field['vid']->advancedRender($view->result[0]);
    });
    $tid = $view->result[0]->_entity->id();
    $vocabulary = Vocabulary::load($this->terms[$tid]->bundle());
    $expected = $vocabulary->get('name');

    $this->assertEquals($expected$actual, 'Displayed vocabulary name should match that loaded from the term.');
    $this->assertEquals('aaa', $vocabulary->id(), 'First result should be vocabulary "aaa", due to ASC sorting.');

    // Reverse sorting.
    $view = Views::getView('test_taxonomy_vid_field');
    
'type' => 'page',
      'title' => $this->randomMachineName(),
      'field_group_rows' => ['a', 'b', 'c'],
    ])->save();

    $renderer = $this->container->get('renderer');
    $view = Views::getView('test_group_rows');

    // Test grouped rows.     $this->executeView($view);
    $output = $renderer->executeInRenderContext(new RenderContext()function D) use ($view) {
      return $view->field['field_group_rows']->advancedRender($view->result[0]);
    });
    $this->assertEquals('a, b, c', $output);

    // Change the group_rows checkbox to false.     $view = Views::getView('test_group_rows');
    $view->setHandlerOption('default', 'field', 'field_group_rows', 'group_rows', FALSE);

    // Test ungrouped rows.     $this->executeView($view);
    $view->render();

    
Home | Imprint | This part of the site doesn't use cookies.