当前位置: 首页>>代码示例>>PHP>>正文


PHP Url::setUrlGenerator方法代码示例

本文整理汇总了PHP中Drupal\Core\Url::setUrlGenerator方法的典型用法代码示例。如果您正苦于以下问题:PHP Url::setUrlGenerator方法的具体用法?PHP Url::setUrlGenerator怎么用?PHP Url::setUrlGenerator使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Drupal\Core\Url的用法示例。


在下文中一共展示了Url::setUrlGenerator方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: generate

 /**
  * {@inheritdoc}
  *
  * For anonymous users, the "active" class will be calculated on the server,
  * because most sites serve each anonymous user the same cached page anyway.
  * For authenticated users, the "active" class will be calculated on the
  * client (through JavaScript), only data- attributes are added to links to
  * prevent breaking the render cache. The JavaScript is added in
  * system_page_attachments().
  *
  * @see system_page_attachments()
  */
 public function generate($text, Url $url, $collect_cacheability_metadata = FALSE)
 {
     // Performance: avoid Url::toString() needing to retrieve the URL generator
     // service from the container.
     $url->setUrlGenerator($this->urlGenerator);
     // Start building a structured representation of our link to be altered later.
     $variables = array('text' => is_array($text) ? drupal_render($text) : $text, 'url' => $url, 'options' => $url->getOptions());
     // Merge in default options.
     $variables['options'] += array('attributes' => array(), 'query' => array(), 'language' => NULL, 'set_active_class' => FALSE, 'absolute' => FALSE);
     // Add a hreflang attribute if we know the language of this link's url and
     // hreflang has not already been set.
     if (!empty($variables['options']['language']) && !isset($variables['options']['attributes']['hreflang'])) {
         $variables['options']['attributes']['hreflang'] = $variables['options']['language']->getId();
     }
     // Set the "active" class if the 'set_active_class' option is not empty.
     if (!empty($variables['options']['set_active_class']) && !$url->isExternal()) {
         // Add a "data-drupal-link-query" attribute to let the
         // drupal.active-link library know the query in a standardized manner.
         if (!empty($variables['options']['query'])) {
             $query = $variables['options']['query'];
             ksort($query);
             $variables['options']['attributes']['data-drupal-link-query'] = Json::encode($query);
         }
         // Add a "data-drupal-link-system-path" attribute to let the
         // drupal.active-link library know the path in a standardized manner.
         if ($url->isRouted() && !isset($variables['options']['attributes']['data-drupal-link-system-path'])) {
             // @todo System path is deprecated - use the route name and parameters.
             $system_path = $url->getInternalPath();
             // Special case for the front page.
             $variables['options']['attributes']['data-drupal-link-system-path'] = $system_path == '' ? '<front>' : $system_path;
         }
     }
     // Remove all HTML and PHP tags from a tooltip, calling expensive strip_tags()
     // only when a quick strpos() gives suspicion tags are present.
     if (isset($variables['options']['attributes']['title']) && strpos($variables['options']['attributes']['title'], '<') !== FALSE) {
         $variables['options']['attributes']['title'] = strip_tags($variables['options']['attributes']['title']);
     }
     // Allow other modules to modify the structure of the link.
     $this->moduleHandler->alter('link', $variables);
     // Move attributes out of options since generateFromRoute() doesn't need
     // them. Include a placeholder for the href.
     $attributes = array('href' => '') + $variables['options']['attributes'];
     unset($variables['options']['attributes']);
     $url->setOptions($variables['options']);
     if (!$collect_cacheability_metadata) {
         $url_string = $url->toString($collect_cacheability_metadata);
     } else {
         $generated_url = $url->toString($collect_cacheability_metadata);
         $url_string = $generated_url->getGeneratedUrl();
         $generated_link = GeneratedLink::createFromObject($generated_url);
     }
     // The result of the URL generator is a plain-text URL to use as the href
     // attribute, and it is escaped by \Drupal\Core\Template\Attribute.
     $attributes['href'] = $url_string;
     $result = SafeMarkup::format('<a@attributes>@text</a>', array('@attributes' => new Attribute($attributes), '@text' => $variables['text']));
     return $collect_cacheability_metadata ? $generated_link->setGeneratedLink($result) : $result;
 }
开发者ID:jeyram,项目名称:camp-gdl,代码行数:69,代码来源:LinkGenerator.php

示例2: testUrlBubbleableMetadataBubbling

 /**
  * Tests bubbling of cacheable metadata for URLs.
  *
  * @param bool $collect_bubbleable_metadata
  *   Whether bubbleable metadata should be collected.
  * @param int $invocations
  *   The expected amount of invocations for the ::bubble() method.
  * @param array $options
  *   The URL options.
  *
  * @covers ::bubble
  *
  * @dataProvider providerUrlBubbleableMetadataBubbling
  */
 public function testUrlBubbleableMetadataBubbling($collect_bubbleable_metadata, $invocations, array $options)
 {
     $self = $this;
     $this->renderer->expects($this->exactly($invocations))->method('render')->willReturnCallback(function ($build) use($self) {
         $self->assertTrue(!empty($build['#cache']));
     });
     $url = new Url('test_1', [], $options);
     $url->setUrlGenerator($this->generator);
     $url->toString($collect_bubbleable_metadata);
 }
开发者ID:aWEBoLabs,项目名称:taxi,代码行数:24,代码来源:MetadataBubblingUrlGeneratorTest.php

示例3: generate

 /**
  * {@inheritdoc}
  *
  * For anonymous users, the "active" class will be calculated on the server,
  * because most sites serve each anonymous user the same cached page anyway.
  * For authenticated users, the "active" class will be calculated on the
  * client (through JavaScript), only data- attributes are added to links to
  * prevent breaking the render cache. The JavaScript is added in
  * system_page_attachments().
  *
  * @see system_page_attachments()
  */
 public function generate($text, Url $url)
 {
     // Performance: avoid Url::toString() needing to retrieve the URL generator
     // service from the container.
     $url->setUrlGenerator($this->urlGenerator);
     // Start building a structured representation of our link to be altered later.
     $variables = array('text' => is_array($text) ? drupal_render($text) : $text, 'url' => $url, 'options' => $url->getOptions());
     // Merge in default options.
     $variables['options'] += array('attributes' => array(), 'query' => array(), 'language' => NULL, 'set_active_class' => FALSE, 'absolute' => FALSE);
     // Add a hreflang attribute if we know the language of this link's url and
     // hreflang has not already been set.
     if (!empty($variables['options']['language']) && !isset($variables['options']['attributes']['hreflang'])) {
         $variables['options']['attributes']['hreflang'] = $variables['options']['language']->getId();
     }
     // Set the "active" class if the 'set_active_class' option is not empty.
     if (!empty($variables['options']['set_active_class']) && !$url->isExternal()) {
         // Add a "data-drupal-link-query" attribute to let the
         // drupal.active-link library know the query in a standardized manner.
         if (!empty($variables['options']['query'])) {
             $query = $variables['options']['query'];
             ksort($query);
             $variables['options']['attributes']['data-drupal-link-query'] = Json::encode($query);
         }
         // Add a "data-drupal-link-system-path" attribute to let the
         // drupal.active-link library know the path in a standardized manner.
         if ($url->isRouted() && !isset($variables['options']['attributes']['data-drupal-link-system-path'])) {
             // @todo System path is deprecated - use the route name and parameters.
             $system_path = $url->getInternalPath();
             // Special case for the front page.
             $variables['options']['attributes']['data-drupal-link-system-path'] = $system_path == '' ? '<front>' : $system_path;
         }
     }
     // Remove all HTML and PHP tags from a tooltip, calling expensive strip_tags()
     // only when a quick strpos() gives suspicion tags are present.
     if (isset($variables['options']['attributes']['title']) && strpos($variables['options']['attributes']['title'], '<') !== FALSE) {
         $variables['options']['attributes']['title'] = strip_tags($variables['options']['attributes']['title']);
     }
     // Allow other modules to modify the structure of the link.
     $this->moduleHandler->alter('link', $variables);
     // Move attributes out of options. generateFromRoute(() doesn't need them.
     $attributes = new Attribute($variables['options']['attributes']);
     unset($variables['options']['attributes']);
     $url->setOptions($variables['options']);
     // The result of the url generator is a plain-text URL. Because we are using
     // it here in an HTML argument context, we need to encode it properly.
     $url = String::checkPlain($url->toString());
     // Make sure the link text is sanitized.
     $safe_text = SafeMarkup::escape($variables['text']);
     return SafeMarkup::set('<a href="' . $url . '"' . $attributes . '>' . $safe_text . '</a>');
 }
开发者ID:Nikola-xiii,项目名称:d8intranet,代码行数:62,代码来源:LinkGenerator.php

示例4: testGenerateWithAlterHook

 /**
  * Tests altering the URL object using hook_link_alter().
  *
  * @covers ::generate
  */
 public function testGenerateWithAlterHook()
 {
     $options = ['query' => [], 'language' => NULL, 'set_active_class' => FALSE, 'absolute' => FALSE];
     $this->urlGenerator->expects($this->any())->method('generateFromRoute')->will($this->returnValueMap([['test_route_1', [], $options, TRUE, (new GeneratedUrl())->setGeneratedUrl('/test-route-1')], ['test_route_2', [], $options, TRUE, (new GeneratedUrl())->setGeneratedUrl('/test-route-2')]]));
     $url = new Url('test_route_2');
     $url->setUrlGenerator($this->urlGenerator);
     $this->moduleHandler->expects($this->atLeastOnce())->method('alter')->willReturnCallback(function ($hook, &$options) {
         $options['url'] = (new Url('test_route_1'))->setUrlGenerator($this->urlGenerator);
     });
     $expected_link_markup = '<a href="/test-route-1">Test</a>';
     $this->assertEquals($expected_link_markup, (string) $this->linkGenerator->generate('Test', $url)->getGeneratedLink());
 }
开发者ID:eigentor,项目名称:tommiblog,代码行数:17,代码来源:LinkGeneratorTest.php

示例5: testRenderAsLinkWithUrlAndOptions

 /**
  * Tests link rendering with a URL and options.
  *
  * @dataProvider providerTestRenderAsLinkWithUrlAndOptions
  * @covers ::renderAsLink
  */
 public function testRenderAsLinkWithUrlAndOptions(Url $url, $alter, Url $expected_url, $url_path, Url $expected_link_url, $link_html, $final_html = NULL)
 {
     $alter += ['make_link' => TRUE, 'url' => $url];
     $final_html = isset($final_html) ? $final_html : $link_html;
     $this->setUpUrlIntegrationServices();
     $this->setupDisplayWithEmptyArgumentsAndFields();
     $field = $this->setupTestField(['alter' => $alter]);
     $field->field_alias = 'key';
     $row = new ResultRow(['key' => 'value']);
     $expected_url->setOptions($expected_url->getOptions() + $this->defaultUrlOptions);
     $expected_link_url->setUrlGenerator($this->urlGenerator);
     $expected_url_options = $expected_url->getOptions();
     unset($expected_url_options['attributes']);
     $this->urlGenerator->expects($this->once())->method('generateFromRoute')->with($expected_url->getRouteName(), $expected_url->getRouteParameters(), $expected_url_options)->willReturn($url_path);
     $result = $field->advancedRender($row);
     $this->assertEquals($final_html, $result);
 }
开发者ID:nstielau,项目名称:drops-8,代码行数:23,代码来源:FieldPluginBaseTest.php

示例6: testGenerateActive

 /**
  * Tests the active class on the link method.
  *
  * @see \Drupal\Core\Utility\LinkGenerator::generate()
  */
 public function testGenerateActive()
 {
     $this->urlGenerator->expects($this->exactly(5))->method('generateFromRoute')->will($this->returnValueMap(array(array('test_route_1', array(), FALSE, '/test-route-1'), array('test_route_3', array(), FALSE, '/test-route-3'), array('test_route_4', array('object' => '1'), FALSE, '/test-route-4/1'))));
     $this->urlGenerator->expects($this->exactly(4))->method('getPathFromRoute')->will($this->returnValueMap(array(array('test_route_1', array(), 'test-route-1'), array('test_route_3', array(), 'test-route-3'), array('test_route_4', array('object' => '1'), 'test-route-4/1'))));
     $this->moduleHandler->expects($this->exactly(5))->method('alter');
     // Render a link.
     $url = new Url('test_route_1', array(), array('set_active_class' => TRUE));
     $url->setUrlGenerator($this->urlGenerator);
     $result = $this->linkGenerator->generate('Test', $url);
     $this->assertLink(array('attributes' => array('data-drupal-link-system-path' => 'test-route-1')), $result);
     // Render a link with the set_active_class option disabled.
     $url = new Url('test_route_1', array(), array('set_active_class' => FALSE));
     $url->setUrlGenerator($this->urlGenerator);
     $result = $this->linkGenerator->generate('Test', $url);
     $this->assertNoXPathResults('//a[@data-drupal-link-system-path="test-route-1"]', $result);
     // Render a link with an associated language.
     $url = new Url('test_route_1', array(), array('language' => new Language(array('id' => 'de')), 'set_active_class' => TRUE));
     $url->setUrlGenerator($this->urlGenerator);
     $result = $this->linkGenerator->generate('Test', $url);
     $this->assertLink(array('attributes' => array('data-drupal-link-system-path' => 'test-route-1', 'hreflang' => 'de')), $result);
     // Render a link with a query parameter.
     $url = new Url('test_route_3', array(), array('query' => array('value' => 'example_1'), 'set_active_class' => TRUE));
     $url->setUrlGenerator($this->urlGenerator);
     $result = $this->linkGenerator->generate('Test', $url);
     $this->assertLink(array('attributes' => array('data-drupal-link-system-path' => 'test-route-3', 'data-drupal-link-query' => '{"value":"example_1"}')), $result);
     // Render a link with route parameters and a query parameter.
     $url = new Url('test_route_4', array('object' => '1'), array('query' => array('value' => 'example_1'), 'set_active_class' => TRUE));
     $url->setUrlGenerator($this->urlGenerator);
     $result = $this->linkGenerator->generate('Test', $url);
     $this->assertLink(array('attributes' => array('data-drupal-link-system-path' => 'test-route-4/1', 'data-drupal-link-query' => '{"value":"example_1"}')), $result);
 }
开发者ID:Nikola-xiii,项目名称:d8intranet,代码行数:36,代码来源:LinkGeneratorTest.php

示例7: testGenerateCacheability

 /**
  * Tests the LinkGenerator's support for collecting cacheability metadata.
  *
  * @see \Drupal\Core\Utility\LinkGenerator::generate()
  * @see \Drupal\Core\Utility\LinkGenerator::generateFromLink()
  */
 public function testGenerateCacheability()
 {
     $options = ['query' => [], 'language' => NULL, 'set_active_class' => FALSE, 'absolute' => FALSE];
     $this->urlGenerator->expects($this->any())->method('generateFromRoute')->will($this->returnValueMap([['test_route_1', [], $options, FALSE, '/test-route-1'], ['test_route_1', [], $options, TRUE, (new GeneratedUrl())->setGeneratedUrl('/test-route-1')]]));
     $url = new Url('test_route_1');
     $url->setUrlGenerator($this->urlGenerator);
     $expected_link_markup = '<a href="/test-route-1">Test</a>';
     // Test ::generate().
     $this->assertSame($expected_link_markup, $this->linkGenerator->generate('Test', $url));
     $generated_link = $this->linkGenerator->generate('Test', $url, TRUE);
     $this->assertSame($expected_link_markup, $generated_link->getGeneratedLink());
     $this->assertInstanceOf('\\Drupal\\Core\\Cache\\CacheableMetadata', $generated_link);
     // Test ::generateFromLink().
     $link = new Link('Test', $url);
     $this->assertSame($expected_link_markup, $this->linkGenerator->generateFromLink($link));
     $generated_link = $this->linkGenerator->generateFromLink($link, TRUE);
     $this->assertSame($expected_link_markup, $generated_link->getGeneratedLink());
     $this->assertInstanceOf('\\Drupal\\Core\\Cache\\CacheableMetadata', $generated_link);
 }
开发者ID:RealLukeMartin,项目名称:drupal8tester,代码行数:25,代码来源:LinkGeneratorTest.php

示例8: generate

 /**
  * {@inheritdoc}
  */
 public function generate($text, $route_name, array $parameters = array(), array $options = array())
 {
     $url = new Url($route_name, $parameters, $options);
     $url->setUrlGenerator($this->urlGenerator);
     return $this->generateFromUrl($text, $url);
 }
开发者ID:alnutile,项目名称:drunatra,代码行数:9,代码来源:LinkGenerator.php

示例9: testGenerateFromUrl

 /**
  * Tests the generateFromUrl() method with a route.
  *
  * @covers ::generateFromUrl()
  */
 public function testGenerateFromUrl()
 {
     $this->urlGenerator->expects($this->once())->method('generateFromRoute')->with('test_route_1', array(), array('fragment' => 'the-fragment') + $this->defaultOptions)->will($this->returnValue('/test-route-1#the-fragment'));
     $this->moduleHandler->expects($this->once())->method('alter')->with('link', $this->isType('array'));
     $url = new Url('test_route_1', array(), array('fragment' => 'the-fragment'));
     $url->setUrlGenerator($this->urlGenerator);
     $result = $this->linkGenerator->generateFromUrl('Test', $url);
     $this->assertTag(array('tag' => 'a', 'attributes' => array('href' => '/test-route-1#the-fragment'), 'content' => 'Test'), $result);
 }
开发者ID:anatalsceo,项目名称:en-classe,代码行数:14,代码来源:LinkGeneratorTest.php

示例10: generate

 /**
  * {@inheritdoc}
  *
  * For anonymous users, the "active" class will be calculated on the server,
  * because most sites serve each anonymous user the same cached page anyway.
  * For authenticated users, the "active" class will be calculated on the
  * client (through JavaScript), only data- attributes are added to links to
  * prevent breaking the render cache. The JavaScript is added in
  * system_page_attachments().
  *
  * @see system_page_attachments()
  */
 public function generate($text, Url $url)
 {
     // Performance: avoid Url::toString() needing to retrieve the URL generator
     // service from the container.
     $url->setUrlGenerator($this->urlGenerator);
     if (is_array($text)) {
         $text = $this->renderer->render($text);
     }
     // Start building a structured representation of our link to be altered later.
     $variables = array('text' => $text, 'url' => $url, 'options' => $url->getOptions());
     // Merge in default options.
     $variables['options'] += array('attributes' => array(), 'query' => array(), 'language' => NULL, 'set_active_class' => FALSE, 'absolute' => FALSE);
     // Add a hreflang attribute if we know the language of this link's url and
     // hreflang has not already been set.
     if (!empty($variables['options']['language']) && !isset($variables['options']['attributes']['hreflang'])) {
         $variables['options']['attributes']['hreflang'] = $variables['options']['language']->getId();
     }
     // Ensure that query values are strings.
     array_walk($variables['options']['query'], function (&$value) {
         if ($value instanceof MarkupInterface) {
             $value = (string) $value;
         }
     });
     // Set the "active" class if the 'set_active_class' option is not empty.
     if (!empty($variables['options']['set_active_class']) && !$url->isExternal()) {
         // Add a "data-drupal-link-query" attribute to let the
         // drupal.active-link library know the query in a standardized manner.
         if (!empty($variables['options']['query'])) {
             $query = $variables['options']['query'];
             ksort($query);
             $variables['options']['attributes']['data-drupal-link-query'] = Json::encode($query);
         }
         // Add a "data-drupal-link-system-path" attribute to let the
         // drupal.active-link library know the path in a standardized manner.
         if ($url->isRouted() && !isset($variables['options']['attributes']['data-drupal-link-system-path'])) {
             // @todo System path is deprecated - use the route name and parameters.
             $system_path = $url->getInternalPath();
             // Special case for the front page.
             $variables['options']['attributes']['data-drupal-link-system-path'] = $system_path == '' ? '<front>' : $system_path;
         }
     }
     // Remove all HTML and PHP tags from a tooltip, calling expensive strip_tags()
     // only when a quick strpos() gives suspicion tags are present.
     if (isset($variables['options']['attributes']['title']) && strpos($variables['options']['attributes']['title'], '<') !== FALSE) {
         $variables['options']['attributes']['title'] = strip_tags($variables['options']['attributes']['title']);
     }
     // Allow other modules to modify the structure of the link.
     $this->moduleHandler->alter('link', $variables);
     $url = $variables['url'];
     // Move attributes out of options since generateFromRoute() doesn't need
     // them. Make sure the "href" comes first for testing purposes.
     $attributes = array('href' => '') + $variables['options']['attributes'];
     unset($variables['options']['attributes']);
     $url->setOptions($variables['options']);
     // External URLs can not have cacheable metadata.
     if ($url->isExternal()) {
         $generated_link = new GeneratedLink();
         $attributes['href'] = $url->toString(FALSE);
     } elseif ($url->isRouted() && $url->getRouteName() === '<nolink>') {
         $generated_link = new GeneratedNoLink();
         unset($attributes['href']);
     } else {
         $generated_url = $url->toString(TRUE);
         $generated_link = GeneratedLink::createFromObject($generated_url);
         // The result of the URL generator is a plain-text URL to use as the href
         // attribute, and it is escaped by \Drupal\Core\Template\Attribute.
         $attributes['href'] = $generated_url->getGeneratedUrl();
     }
     if (!$variables['text'] instanceof MarkupInterface) {
         $variables['text'] = Html::escape($variables['text']);
     }
     $attributes = new Attribute($attributes);
     // This is safe because Attribute does escaping and $variables['text'] is
     // either rendered or escaped.
     return $generated_link->setGeneratedLink('<' . $generated_link::TAG . $attributes . '>' . $variables['text'] . '</' . $generated_link::TAG . '>');
 }
开发者ID:eigentor,项目名称:tommiblog,代码行数:88,代码来源:LinkGenerator.php


注:本文中的Drupal\Core\Url::setUrlGenerator方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。