本文整理汇总了PHP中drupal_render_root函数的典型用法代码示例。如果您正苦于以下问题:PHP drupal_render_root函数的具体用法?PHP drupal_render_root怎么用?PHP drupal_render_root使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了drupal_render_root函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: preview
/**
* Overrides \Drupal\views\Plugin\views\display\PathPluginBase::preview().
*/
public function preview()
{
$output = $this->view->render();
if (!empty($this->view->live_preview)) {
$output = array('#prefix' => '<pre>', '#markup' => SafeMarkup::checkPlain(drupal_render_root($output)), '#suffix' => '</pre>');
}
return $output;
}
示例2: preview
/**
* {@inheritdoc}
*/
public function preview()
{
$output = $this->view->render();
if (!empty($this->view->live_preview)) {
$output = array('#prefix' => '<pre>', '#plain_text' => drupal_render_root($output), '#suffix' => '</pre>');
}
return $output;
}
示例3: assertRenderedElement
/**
* Tests that elements are rendered properly.
*/
protected function assertRenderedElement(array $element, $xpath, array $xpath_args = array())
{
$original_element = $element;
$this->setRawContent(drupal_render_root($element));
$this->verbose('<hr />' . $this->getRawContent());
// @see \Drupal\simpletest\WebTestBase::xpath()
$xpath = $this->buildXPathQuery($xpath, $xpath_args);
$element += array('#value' => NULL);
$this->assertFieldByXPath($xpath, $element['#value'], format_string('#type @type was properly rendered.', array('@type' => var_export($element['#type'], TRUE))));
}
示例4: render
/**
* {@inheritdoc}
*/
public function render($row)
{
$entity = $row->_entity;
$item = new \stdClass();
foreach ($entity as $name => $field) {
// views_view_row_rss takes care about the escaping.
$item->{$name} = $field->value;
}
$item->elements = array(array('key' => 'pubDate', 'value' => gmdate('r', $entity->timestamp->value)), array('key' => 'dc:creator', 'value' => $entity->author->value), array('key' => 'guid', 'value' => $entity->guid->value, 'attributes' => array('isPermaLink' => 'false')));
$build = array('#theme' => $this->themeFunctions(), '#view' => $this->view, '#options' => $this->options, '#row' => $item);
return drupal_render_root($build);
}
示例5: testSerializerResponses
/**
* Checks the behavior of the Serializer callback paths and row plugins.
*/
public function testSerializerResponses()
{
// Test the serialize callback.
$view = Views::getView('test_serializer_display_field');
$view->initDisplay();
$this->executeView($view);
$actual_json = $this->drupalGet('test/serialize/field', array(), array('Accept: application/json'));
$this->assertResponse(200);
// Test the http Content-type.
$headers = $this->drupalGetHeaders();
$this->assertEqual($headers['content-type'], 'application/json', 'The header Content-type is correct.');
$expected = array();
foreach ($view->result as $row) {
$expected_row = array();
foreach ($view->field as $id => $field) {
$expected_row[$id] = $field->render($row);
}
$expected[] = $expected_row;
}
$this->assertIdentical($actual_json, json_encode($expected), 'The expected JSON output was found.');
// Test that the rendered output and the preview output are the same.
$view->destroy();
$view->setDisplay('rest_export_1');
// Mock the request content type by setting it on the display handler.
$view->display_handler->setContentType('json');
$output = $view->preview();
$this->assertIdentical($actual_json, drupal_render_root($output), 'The expected JSON preview output was found.');
// Test a 403 callback.
$this->drupalGet('test/serialize/denied');
$this->assertResponse(403);
// Test the entity rows.
$view = Views::getView('test_serializer_display_entity');
$view->initDisplay();
$this->executeView($view);
// Get the serializer service.
$serializer = $this->container->get('serializer');
$entities = array();
foreach ($view->result as $row) {
$entities[] = $row->_entity;
}
$expected = $serializer->serialize($entities, 'json');
$actual_json = $this->drupalGet('test/serialize/entity', array(), array('Accept: application/json'));
$this->assertResponse(200);
$this->assertIdentical($actual_json, $expected, 'The expected JSON output was found.');
$expected = $serializer->serialize($entities, 'hal_json');
$actual_json = $this->drupalGet('test/serialize/entity', array(), array('Accept: application/hal+json'));
$this->assertIdentical($actual_json, $expected, 'The expected HAL output was found.');
}
示例6: renderResponse
/**
* {@inheritdoc}
*/
public function renderResponse(array $main_content, Request $request, RouteMatchInterface $route_match)
{
$response = new AjaxResponse();
// First render the main content, because it might provide a title.
$content = drupal_render_root($main_content);
// Attach the library necessary for using the OpenModalDialogCommand and set
// the attachments for this Ajax response.
$main_content['#attached']['library'][] = 'core/drupal.dialog.ajax';
$response->setAttachments($main_content['#attached']);
// If the main content doesn't provide a title, use the title resolver.
$title = isset($main_content['#title']) ? $main_content['#title'] : $this->titleResolver->getTitle($request, $route_match->getRouteObject());
// Determine the title: use the title provided by the main content if any,
// otherwise get it from the routing information.
$options = $request->request->get('dialogOptions', array());
$response->addCommand(new OpenModalDialogCommand($title, $content, $options));
return $response;
}
示例7: render
/**
* {@inheritdoc}
*/
public function render(HtmlFragmentInterface $fragment, $status_code = 200)
{
// Converts the given HTML fragment which represents the main content region
// of the page into a render array.
$page_content['main'] = array('#markup' => $fragment->getContent());
$page_content['#title'] = $fragment->getTitle();
if ($fragment instanceof CacheableInterface) {
$page_content['main']['#cache']['tags'] = $fragment->getCacheTags();
}
// Build the full page array by calling drupal_prepare_page(), which invokes
// hook_page_build(). This adds the other regions to the page.
$page_array = drupal_prepare_page($page_content);
// Build the HtmlPage object.
$page = new HtmlPage('', array(), $fragment->getTitle());
$page = $this->preparePage($page, $page_array);
$page->setBodyTop(drupal_render_root($page_array['page_top']));
$page->setBodyBottom(drupal_render_root($page_array['page_bottom']));
$page->setContent(drupal_render_root($page_array));
$page->setStatusCode($status_code);
drupal_process_attached($page_array);
if (isset($page_array['page_top'])) {
drupal_process_attached($page_array['page_top']);
}
if (isset($page_array['page_bottom'])) {
drupal_process_attached($page_array['page_bottom']);
}
if ($fragment instanceof CacheableInterface) {
// Persist cache tags associated with this page. Also associate the
// "rendered" cache tag. This allows us to invalidate the entire render
// cache, regardless of the cache bin.
$cache_tags = Cache::mergeTags(isset($page_array['page_top']) ? $page_array['page_top']['#cache']['tags'] : [], $page_array['#cache']['tags'], isset($page_array['page_bottom']) ? $page_array['page_bottom']['#cache']['tags'] : [], ['rendered']);
// Only keep unique cache tags. We need to prevent duplicates here already
// rather than only in the cache layer, because they are also used by
// reverse proxies (like Varnish), not only by Drupal's page cache.
$page->setCacheTags(array_unique($cache_tags));
}
return $page;
}
示例8: testSnippets
/**
* Tests various code snippets.
*/
function testSnippets()
{
$container = \Drupal::getContainer();
// Cache snippets from section "The Drupal Cache" in Chapter 1.
// Make up some test data.
$bin = 'default';
$cid = 'my_cid';
$data = 'my_data';
$nid = 5;
$tags = array('node:' . $nid);
// Cache the data. Get the class in two different ways.
$cache_class = \Drupal::cache($bin);
$this->assertTrue($cache_class, 'Cache class is not null');
$cache_class = $container->get('cache.' . $bin);
$cache_class->set($cid, $data, CacheBackendInterface::CACHE_PERMANENT, $tags);
// Check that we can retrieve data from the cache.
$out = $cache_class->get($cid);
$this->outputVariable($out, 'Cache get method output');
$this->assertEqual($data, $out->data, 'Cached data could be retrieved');
// Invalidate the data and check that it cannot be retrieved.
Cache::invalidateTags($tags);
$out = $cache_class->get($cid);
$this->assertFalse($out, 'After invalidating tags, cached data cannot be retrieved');
// Theme snippets from section "Making Your Output Themeable" in Chapter 2.
$build['hello'] = array('#input1' => t('Hello World!'), '#theme' => 'mymodule_hookname');
$output = drupal_render_root($build);
$expected = '<div>Hello World!</div>';
$this->outputHTML($output, 'Theme template output');
$this->assertEqual(trim($output), $expected, 'Theme template worked in render array');
// Config API snippets from "Configuration API in Drupal 8" section in
// chapter 2.
// Test reading the settings several different ways.
$config = \Drupal::config('mymodule.settings');
$this->assertTrue($config, 'Config class is not null');
$config = $container->get('config.factory')->getEditable('mymodule.settings');
$all = $config->get();
$this->outputVariable($all, 'Full configuration output');
$button_label = $all['submit_button_label'];
$this->assertEqual($button_label, 'Submit', 'Read correct button label from overall get');
$button_label = $config->get('submit_button_label');
$this->assertEqual($button_label, 'Submit', 'Read correct button label with specific get');
$name_field_info = $all['name_field_settings'];
$name_label = $name_field_info['field_label'];
$this->assertEqual($name_label, 'Your name', 'Read correct name field label from overall get');
$name_field_info = $config->get('name_field_settings');
$this->outputVariable($name_field_info, 'Name field configuration output');
$name_label = $name_field_info['field_label'];
$this->assertEqual($name_label, 'Your name', 'Read correct name field label from field settings get');
$name_label = $config->get('name_field_settings.field_label');
$this->assertEqual($name_label, 'Your name', 'Read correct name field label from specific get');
// Change the submit label.
$new_label = "Save";
$config->set('submit_button_label', $new_label);
$config->save();
// Get a new config object, to make sure it was really saved.
$new_config = \Drupal::config('mymodule.settings');
$button_label = $new_config->get('submit_button_label');
$this->assertEqual($button_label, 'Save', 'Read correct button label after save');
// State API snippets from "State API in Drupal 8" section in chapter 2.
// Get $state in two ways.
$state = \Drupal::state();
$this->assertTrue($state, 'State is not null');
$state = $container->get('state');
$value = 'Some test data';
$state->set('mymodule.my_state_variable_name', $value);
$new_value = $state->get('mymodule.my_state_variable_name');
$this->assertEqual($value, $new_value, 'State get worked correctly');
$state->delete('mymodule.my_state_variable_name');
$new_value = $state->get('mymodule.my_state_variable_name');
$this->assertNull($new_value, 'After delete, could not retrieve state value');
// Snippets from "Internationalizing User Interface Text" section in
// chapter 2. Good code only; bad code examples are omitted.
$button_text = t('Save');
$this->assertEqual($button_text, 'Save', 't() worked OK on simple string');
$user_name = 'foo';
$message_string = t('Hello @user_name', array('@user_name' => $user_name));
$this->outputHTML($message_string, 't() with variables output');
$this->assertEqual($message_string, 'Hello foo', 't() worked OK on string with variable');
$test = (object) array();
$foo = $test instanceof MyClass;
// Database snippets from "Querying the Database with the Database API"
// section in Chapter 2.
// Make a blocked user for querying purposes.
$account = $this->drupalCreateUser(array());
$account->status = 0;
$account->save();
// Query by status 0 (blocked).
$desired_status = 0;
$found = FALSE;
$result = db_query('SELECT * FROM {users_field_data} u WHERE u.status = :status', array(':status' => $desired_status));
foreach ($result as $record) {
$this->outputVariable($record, 'User database record');
if ($record->uid == $account->id()) {
$found = TRUE;
}
}
$this->assertTrue($found, 'Created user was found by status query');
//.........这里部分代码省略.........
示例9: onView
/**
* Processes a successful controller into an HTTP 200 response.
*
* Some controllers may not return a response object but simply the body of
* one. The VIEW event is called in that case, to allow us to mutate that
* body into a Response object. In particular we assume that the return
* from an JSON-type response is a JSON string, so just wrap it into a
* Response object.
*
* @param Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent $event
* The Event to process.
*/
public function onView(GetResponseForControllerResultEvent $event)
{
$request = $event->getRequest();
// For a master request, we process the result and wrap it as needed.
// For a subrequest, all we want is the string value. We assume that
// is just an HTML string from a controller, so wrap that into a response
// object. The subrequest's response will get dissected and placed into
// the larger page as needed.
if ($event->getRequestType() == HttpKernelInterface::MASTER_REQUEST) {
$method = 'on' . $this->negotiation->getContentType($request);
if (method_exists($this, $method)) {
$event->setResponse($this->{$method}($event));
} else {
$event->setResponse(new Response('Not Acceptable', 406));
}
} else {
// This is a new-style Symfony-esque subrequest, which means we assume
// the body is not supposed to be a complete page but just a page
// fragment.
$page_result = $event->getControllerResult();
if ($page_result instanceof HtmlPage || $page_result instanceof Response) {
return $page_result;
}
if (!is_array($page_result)) {
$page_result = array('#markup' => $page_result);
}
// If no title was returned fall back to one defined in the route.
if (!isset($page_result['#title'])) {
$page_result['#title'] = $this->titleResolver->getTitle($request, $request->attributes->get(RouteObjectInterface::ROUTE_OBJECT));
}
$event->setResponse(new Response(drupal_render_root($page_result)));
}
}
示例10: render
public function render($row)
{
global $base_url;
$nid = $row->{$this->field_alias};
if (!is_numeric($nid)) {
return;
}
$display_mode = $this->options['view_mode'];
if ($display_mode == 'default') {
$display_mode = \Drupal::config('system.rss')->get('items.view_mode');
}
// Load the specified node:
/** @var \Drupal\node\NodeInterface $node */
$node = $this->nodes[$nid];
if (empty($node)) {
return;
}
$item_text = '';
$node->link = $node->url('canonical', array('absolute' => TRUE));
$node->rss_namespaces = array();
$node->rss_elements = array(array('key' => 'pubDate', 'value' => gmdate('r', $node->getCreatedTime())), array('key' => 'dc:creator', 'value' => $node->getOwner()->getUsername()), array('key' => 'guid', 'value' => $node->id() . ' at ' . $base_url, 'attributes' => array('isPermaLink' => 'false')));
// The node gets built and modules add to or modify $node->rss_elements
// and $node->rss_namespaces.
$build_mode = $display_mode;
$build = node_view($node, $build_mode);
unset($build['#theme']);
if (!empty($node->rss_namespaces)) {
$this->view->style_plugin->namespaces = array_merge($this->view->style_plugin->namespaces, $node->rss_namespaces);
} elseif (function_exists('rdf_get_namespaces')) {
// Merge RDF namespaces in the XML namespaces in case they are used
// further in the RSS content.
$xml_rdf_namespaces = array();
foreach (rdf_get_namespaces() as $prefix => $uri) {
$xml_rdf_namespaces['xmlns:' . $prefix] = $uri;
}
$this->view->style_plugin->namespaces += $xml_rdf_namespaces;
}
if ($display_mode != 'title') {
// We render node contents.
$item_text .= drupal_render_root($build);
}
$item = new \stdClass();
$item->description = SafeMarkup::set($item_text);
$item->title = $node->label();
$item->link = $node->link;
$item->elements = $node->rss_elements;
$item->nid = $node->id();
$theme_function = array('#theme' => $this->themeFunctions(), '#view' => $this->view, '#options' => $this->options, '#row' => $item);
return drupal_render_root($theme_function);
}
示例11: dialog
/**
* Displays content in a dialog.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The request object.
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The route match.
* @param mixed $_content
* A controller definition string, or a callable object/closure.
* @param bool $modal
* (optional) TRUE to render a modal dialog. Defaults to FALSE.
*
* @return \Drupal\Core\Ajax\AjaxResponse
* AjaxResponse to return the content wrapper in a dialog.
*/
public function dialog(Request $request, RouteMatchInterface $route_match, $_content, $modal = FALSE)
{
$page_content = $this->getContentResult($request, $_content);
// Allow controllers to return a HtmlPage or a Response object directly.
if ($page_content instanceof HtmlPage) {
$page_content = $page_content->getContent();
}
if ($page_content instanceof Response) {
$page_content = $page_content->getContent();
}
// Most controllers return a render array, but some return a string.
if (!is_array($page_content)) {
$page_content = array('#markup' => $page_content);
}
$content = drupal_render_root($page_content);
drupal_process_attached($page_content);
$title = isset($page_content['#title']) ? $page_content['#title'] : $this->titleResolver->getTitle($request, $route_match->getRouteObject());
$response = new AjaxResponse();
// Fetch any modal options passed in from data-dialog-options.
$options = $request->request->get('dialogOptions', array());
// Set modal flag and re-use the modal ID.
if ($modal) {
$options['modal'] = TRUE;
$target = '#drupal-modal';
} else {
// Generate the target wrapper for the dialog.
if (isset($options['target'])) {
// If the target was nominated in the incoming options, use that.
$target = $options['target'];
// Ensure the target includes the #.
if (substr($target, 0, 1) != '#') {
$target = '#' . $target;
}
// This shouldn't be passed on to jQuery.ui.dialog.
unset($options['target']);
} else {
// Generate a target based on the route id.
$route_name = $route_match->getRouteName();
$target = '#' . drupal_html_id("drupal-dialog-{$route_name}");
}
}
$response->addCommand(new OpenDialogCommand($target, $title, $content, $options));
return $response;
}
示例12: drupalRenderRoot
/**
* Wraps drupal_render_root().
*
* @todo: Remove as part of https://drupal.org/node/2182149
*/
protected function drupalRenderRoot(&$elements)
{
$output = drupal_render_root($elements);
drupal_process_attached($elements);
return $output;
}
示例13: render
public function render($row)
{
static $row_index;
if (!isset($row_index)) {
$row_index = 0;
}
if (function_exists('rdf_get_namespaces')) {
// Merge RDF namespaces in the XML namespaces in case they are used
// further in the RSS content.
$xml_rdf_namespaces = array();
foreach (rdf_get_namespaces() as $prefix => $uri) {
$xml_rdf_namespaces['xmlns:' . $prefix] = $uri;
}
$this->view->style_plugin->namespaces += $xml_rdf_namespaces;
}
// Create the RSS item object.
$item = new \stdClass();
$item->title = $this->getField($row_index, $this->options['title_field']);
$item->link = _url($this->getField($row_index, $this->options['link_field']), array('absolute' => TRUE));
$item->description = $this->getField($row_index, $this->options['description_field']);
$item->elements = array(array('key' => 'pubDate', 'value' => $this->getField($row_index, $this->options['date_field'])), array('key' => 'dc:creator', 'value' => $this->getField($row_index, $this->options['creator_field']), 'namespace' => array('xmlns:dc' => 'http://purl.org/dc/elements/1.1/')));
$guid_is_permalink_string = 'false';
$item_guid = $this->getField($row_index, $this->options['guid_field_options']['guid_field']);
if ($this->options['guid_field_options']['guid_field_is_permalink']) {
$guid_is_permalink_string = 'true';
$item_guid = _url($item_guid, array('absolute' => TRUE));
}
$item->elements[] = array('key' => 'guid', 'value' => $item_guid, 'attributes' => array('isPermaLink' => $guid_is_permalink_string));
$row_index++;
foreach ($item->elements as $element) {
if (isset($element['namespace'])) {
$this->view->style_plugin->namespaces = array_merge($this->view->style_plugin->namespaces, $element['namespace']);
}
}
$build = array('#theme' => $this->themeFunctions(), '#view' => $this->view, '#options' => $this->options, '#row' => $item, '#field_alias' => isset($this->field_alias) ? $this->field_alias : '');
return drupal_render_root($build);
}
示例14: render
public function render($row)
{
global $base_url;
$cid = $row->{$this->field_alias};
if (!is_numeric($cid)) {
return;
}
$view_mode = $this->options['view_mode'];
if ($view_mode == 'default') {
$view_mode = \Drupal::config('system.rss')->get('items.view_mode');
}
// Load the specified comment and its associated node:
/** @var $comment \Drupal\comment\CommentInterface */
$comment = $this->comments[$cid];
if (empty($comment)) {
return;
}
$item_text = '';
$comment->link = $comment->url('canonical', array('absolute' => TRUE));
$comment->rss_namespaces = array();
$comment->rss_elements = array(array('key' => 'pubDate', 'value' => gmdate('r', $comment->getCreatedTime())), array('key' => 'dc:creator', 'value' => $comment->getAuthorName()), array('key' => 'guid', 'value' => 'comment ' . $comment->id() . ' at ' . $base_url, 'attributes' => array('isPermaLink' => 'false')));
// The comment gets built and modules add to or modify
// $comment->rss_elements and $comment->rss_namespaces.
$build = comment_view($comment, 'rss');
unset($build['#theme']);
if (!empty($comment->rss_namespaces)) {
$this->view->style_plugin->namespaces = array_merge($this->view->style_plugin->namespaces, $comment->rss_namespaces);
}
if ($view_mode != 'title') {
// We render comment contents.
$item_text .= drupal_render_root($build);
}
$item = new \stdClass();
$item->description = $item_text;
$item->title = $comment->label();
$item->link = $comment->link;
$item->elements = $comment->rss_elements;
$item->cid = $comment->id();
$build = array('#theme' => $this->themeFunctions(), '#view' => $this->view, '#options' => $this->options, '#row' => $item);
return drupal_render_root($build);
}
示例15: execute
/**
* {@inheritdoc}
*/
public function execute()
{
parent::execute();
$output = $this->view->render();
return new Response(drupal_render_root($output), 200, array('Content-type' => $this->getMimeType()));
}