本文整理汇总了PHP中Drupal\Component\Utility\UrlHelper类的典型用法代码示例。如果您正苦于以下问题:PHP UrlHelper类的具体用法?PHP UrlHelper怎么用?PHP UrlHelper使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了UrlHelper类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: validateForm
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state)
{
// Validate video URL.
if (!UrlHelper::isValid($form_state->getValue('video'), TRUE)) {
$form_state->setErrorByName('video', $this->t("The video url '%url' is invalid.", array('%url' => $form_state->getValue('video'))));
}
}
示例2: validate
/**
* {@inheritdoc}
*/
public function validate($value, Constraint $constraint)
{
if (isset($value)) {
$url_is_valid = TRUE;
/** @var $link_item \Drupal\link\LinkItemInterface */
$link_item = $value;
$link_type = $link_item->getFieldDefinition()->getSetting('link_type');
$url_string = $link_item->url;
// Validate the url property.
if ($url_string !== '') {
try {
// @todo This shouldn't be needed, but massageFormValues() may not
// run.
$parsed_url = UrlHelper::parse($url_string);
$url = Url::createFromPath($parsed_url['path']);
if ($url->isExternal() && !UrlHelper::isValid($url_string, TRUE)) {
$url_is_valid = FALSE;
} elseif ($url->isExternal() && !($link_type & LinkItemInterface::LINK_EXTERNAL)) {
$url_is_valid = FALSE;
}
} catch (NotFoundHttpException $e) {
$url_is_valid = FALSE;
} catch (MatchingRouteNotFoundException $e) {
$url_is_valid = FALSE;
} catch (ParamNotConvertedException $e) {
$url_is_valid = FALSE;
}
}
if (!$url_is_valid) {
$this->context->addViolation($this->message, array('%url' => $url_string));
}
}
}
示例3: blockValidate
/**
* {@inheritdoc}
*/
public function blockValidate($form, FormStateInterface $form_state)
{
// Instantiate UrlHelper object to validate the URL's.
$url_helper = new UrlHelper();
// Build an array of the links that need validating. If more fields are
// added later, add another entry to the links array.
$links = [];
$links['pantheon_url'] = $form_state->getValue('pantheon_url');
// Create an error variable to prevent setting the error message repeatedly.
$error_set = FALSE;
// Validate and set errors where appropriate.
foreach ($links as $key => $link) {
if ($link == '') {
break;
}
$validity = $url_helper->isValid($link, TRUE);
if ($validity != TRUE) {
$form_state->setErrorByName($key, "The value must be a full URL similar to http://www.example.com.");
// Using drupal_set_message because of a bug with setError that is
// causing the form to not submit correctly, but isn't displaying
// messages.
if ($error_set == FALSE) {
drupal_set_message('All values must be full URLs of format: http://www.example.com.', 'error');
// Prevent the error from outputting multiple times.
$error_set = TRUE;
}
}
}
}
示例4: execute
/**
* {@inheritdoc}
*/
public function execute($object = NULL)
{
$url = $this->configuration['url'];
// Leave external URLs unchanged, and assemble others as absolute URLs
// relative to the site's base URL.
if (!UrlHelper::isExternal($url)) {
$parts = UrlHelper::parse($url);
// @todo '<front>' is valid input for BC reasons, may be removed by
// https://www.drupal.org/node/2421941
if ($parts['path'] === '<front>') {
$parts['path'] = '';
}
$uri = 'base:' . $parts['path'];
$options = ['query' => $parts['query'], 'fragment' => $parts['fragment'], 'absolute' => TRUE];
// Treat this as if it's user input of a path relative to the site's
// base URL.
$url = $this->unroutedUrlAssembler->assemble($uri, $options);
}
$response = new RedirectResponse($url);
$listener = function ($event) use($response) {
$event->setResponse($response);
};
// Add the listener to the event dispatcher.
$this->dispatcher->addListener(KernelEvents::RESPONSE, $listener);
}
示例5: prepareValue
/**
* {@inheritdoc}
*/
protected function prepareValue($delta, array &$values)
{
$values['uri'] = trim($values['uri']);
if (!UrlHelper::isValid($values['uri'], TRUE)) {
$values['uri'] = '';
}
}
示例6: isValid
/**
* {@inheritdoc}
*/
public function isValid($path)
{
// External URLs and the front page are always valid.
if ($path == '<front>' || UrlHelper::isExternal($path)) {
return TRUE;
}
// Check the routing system.
$collection = $this->routeProvider->getRoutesByPattern('/' . $path);
if ($collection->count() == 0) {
return FALSE;
}
$request = RequestHelper::duplicate($this->requestStack->getCurrentRequest(), '/' . $path);
$request->attributes->set('_system_path', $path);
// We indicate that a menu administrator is running the menu access check.
$request->attributes->set('_menu_admin', TRUE);
// Attempt to match this path to provide a fully built request to the
// access checker.
try {
$request->attributes->add($this->requestMatcher->matchRequest($request));
} catch (ParamNotConvertedException $e) {
return FALSE;
}
// Consult the access manager.
$routes = $collection->all();
$route = reset($routes);
return $this->accessManager->check($route, $request, $this->account);
}
示例7: validateValue
/**
* {@inheritdoc}
*/
public static function validateValue(array &$element, FormStateInterface $form_state, array $form)
{
if (!empty($element['#value'])) {
if (!UrlHelper::isValid($element['#value'], TRUE)) {
$form_state->setError($element, t('The entered Tumblr URI is not valid.'));
}
}
}
示例8: createUrlFromString
/**
* @param $url
*
* @see FillPdfLinkManipulatorInterface::parseUrlString()
*
* @return \Drupal\Core\Url
*/
protected function createUrlFromString($url) {
$url_parts = UrlHelper::parse($url);
$path = $url_parts['path'];
$query = $url_parts['query'];
$link = Url::fromUri($path, ['query' => $query]);
return $link;
}
示例9: validateUrl
/**
* Form element validation handler for #type 'url'.
*
* Note that #maxlength and #required is validated by _form_validate() already.
*/
public static function validateUrl(&$element, FormStateInterface $form_state, &$complete_form)
{
$value = trim($element['#value']);
$form_state->setValueForElement($element, $value);
if ($value !== '' && !UrlHelper::isValid($value, TRUE)) {
$form_state->setError($element, t('The URL %url is not valid.', array('%url' => $value)));
}
}
示例10: validateForm
public function validateForm(array &$form, FormStateInterface $form_state)
{
if (strlen($form_state->getValue('title')) < 3) {
$form_state->setErrorByName('title', $this->t('Your name is too short.'));
}
if (!UrlHelper::isValid($form_state->getValue('video'), TRUE)) {
$form_state->setErrorByName('video', $this->t("The video url '%url' is invalid.", array('%url' => $form_state->getValue('video'))));
}
}
示例11: __construct
/**
* Constructs a new generator object.
*
* @param \Drupal\Core\Routing\RouteProviderInterface $provider
* The route provider to be searched for routes.
* @param \Drupal\Core\PathProcessor\OutboundPathProcessorInterface $path_processor
* The path processor to convert the system path to one suitable for urls.
* @param \Drupal\Core\RouteProcessor\OutboundRouteProcessorInterface $route_processor
* The route processor.
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* A request stack object.
* @param string[] $filter_protocols
* (optional) An array of protocols allowed for URL generation.
*/
public function __construct(RouteProviderInterface $provider, OutboundPathProcessorInterface $path_processor, OutboundRouteProcessorInterface $route_processor, RequestStack $request_stack, array $filter_protocols = ['http', 'https'])
{
$this->provider = $provider;
$this->context = new RequestContext();
$this->pathProcessor = $path_processor;
$this->routeProcessor = $route_processor;
UrlHelper::setAllowedProtocols($filter_protocols);
$this->requestStack = $request_stack;
}
示例12: setEmbedProvider
/**
* {@inheritdoc}
*/
public function setEmbedProvider($provider)
{
$provider_parsed = UrlHelper::parse($provider);
$provider_parsed['query'] = array_filter($provider_parsed['query'], function ($value) {
return $value !== '{callback}';
});
$provider_parsed['absolute'] = TRUE;
$this->embed_provider = $this->urlAssembler->assemble($provider_parsed['path'], $provider_parsed);
}
示例13: testHandleWithGetRequest
/**
* Tests onHandleException with a GET request.
*/
public function testHandleWithGetRequest()
{
$request = Request::create('/test', 'GET', array('name' => 'druplicon', 'pass' => '12345'));
$this->kernel->expects($this->once())->method('handle')->will($this->returnCallback(function (Request $request) {
return new Response($request->getMethod() . ' ' . UrlHelper::buildQuery($request->query->all()));
}));
$event = new GetResponseForExceptionEvent($this->kernel, $request, 'foo', new \Exception('foo'));
$this->exceptionListener->onKernelException($event);
$response = $event->getResponse();
$this->assertEquals('GET name=druplicon&pass=12345 ', $response->getContent() . " " . UrlHelper::buildQuery($request->request->all()));
}
示例14: testBadProtocolStripping
/**
* Checks that harmful protocols are stripped.
*/
function testBadProtocolStripping()
{
// Ensure that check_url() strips out harmful protocols, and encodes for
// HTML.
// Ensure \Drupal\Component\Utility\UrlHelper::stripDangerousProtocols() can
// be used to return a plain-text string stripped of harmful protocols.
$url = 'javascript:http://www.example.com/?x=1&y=2';
$expected_plain = 'http://www.example.com/?x=1&y=2';
$expected_html = 'http://www.example.com/?x=1&y=2';
$this->assertIdentical(check_url($url), $expected_html, 'check_url() filters a URL and encodes it for HTML.');
$this->assertIdentical(UrlHelper::stripDangerousProtocols($url), $expected_plain, '\\Drupal\\Component\\Utility\\Url::stripDangerousProtocols() filters a URL and returns plain text.');
}
示例15: render
/**
* Response for the xmlsitemap_engines_test.ping route.
*
* @throws NotFoundHttpException
* Throw a NotFoundHttpException if query url is not valid.
*
* @return \Symfony\Component\HttpFoundation\Response
* A response with 200 code if the url query is valid.
*/
public function render()
{
$query = \Drupal::request()->query->get('sitemap');
if (empty($query) || !UrlHelper::isValid($query)) {
watchdog('xmlsitemap', 'No valid sitemap parameter provided.', array(), WATCHDOG_WARNING);
// @todo Remove this? Causes an extra watchdog error to be handled.
throw new NotFoundHttpException();
} else {
watchdog('xmlsitemap', 'Recieved ping for @sitemap.', array('@sitemap' => $query));
}
return new Response('', 200);
}