本文整理汇总了PHP中Drupal\Component\Utility\UrlHelper::parse方法的典型用法代码示例。如果您正苦于以下问题:PHP UrlHelper::parse方法的具体用法?PHP UrlHelper::parse怎么用?PHP UrlHelper::parse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Drupal\Component\Utility\UrlHelper
的用法示例。
在下文中一共展示了UrlHelper::parse方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: 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));
}
}
}
示例2: getUrlIfValid
/**
* {@inheritdoc}
*/
public function getUrlIfValid($path)
{
$parsed_url = UrlHelper::parse($path);
$options = [];
if (!empty($parsed_url['query'])) {
$options['query'] = $parsed_url['query'];
}
if (!empty($parsed_url['fragment'])) {
$options['fragment'] = $parsed_url['fragment'];
}
if ($parsed_url['path'] == '<front>') {
return new Url('<front>', [], $options);
} elseif (UrlHelper::isExternal($path) && UrlHelper::isValid($path)) {
if (empty($parsed_url['path'])) {
return FALSE;
}
return Url::fromUri($path);
}
$path = ltrim($path, '/');
$request = Request::create('/' . $path);
$attributes = $this->getPathAttributes($path, $request);
if (!$attributes) {
return FALSE;
}
$route_name = $attributes[RouteObjectInterface::ROUTE_NAME];
$route_parameters = $attributes['_raw_variables']->all();
return new Url($route_name, $route_parameters, $options + ['query' => $request->query->all()]);
}
示例3: 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);
}
示例4: 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;
}
示例5: 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);
}
示例6: getNextDestination
/**
* Returns the next redirect path in a multipage sequence.
*
* @param array $destinations
* An array of destinations to redirect to.
*
* @return array
* The next destination to redirect to.
*/
public static function getNextDestination(array $destinations)
{
$next_destination = array_shift($destinations);
if (is_array($next_destination)) {
$next_destination['options']['query']['destinations'] = $destinations;
} else {
$options = UrlHelper::parse($next_destination);
if ($destinations) {
$options['query']['destinations'] = $destinations;
}
$next_destination = array($options['path'], $options);
}
return $next_destination;
}
示例7: buildCancelLink
/**
* Builds the cancel link for a confirmation form.
*
* @param \Drupal\Core\Form\ConfirmFormInterface $form
* The confirmation form.
* @param \Symfony\Component\HttpFoundation\Request $request
* The current request.
*
* @return array
* The link render array for the cancel form.
*/
public static function buildCancelLink(ConfirmFormInterface $form, Request $request)
{
// Prepare cancel link.
$query = $request->query;
// If a destination is specified, that serves as the cancel link.
if ($query->has('destination')) {
$options = UrlHelper::parse($query->get('destination'));
// @todo Revisit this in https://www.drupal.org/node/2418219.
$url = Url::fromUserInput('/' . $options['path'], $options);
} else {
$url = $form->getCancelUrl();
}
return ['#type' => 'link', '#title' => $form->getCancelText(), '#attributes' => ['class' => ['button']], '#url' => $url];
}
示例8: buildCancelLink
/**
* Builds the cancel link for a confirmation form.
*
* @param \Drupal\Core\Form\ConfirmFormInterface $form
* The confirmation form.
* @param \Symfony\Component\HttpFoundation\Request $request
* The current request.
*
* @return array
* The link render array for the cancel form.
*/
public static function buildCancelLink(ConfirmFormInterface $form, Request $request)
{
// Prepare cancel link.
$query = $request->query;
// If a destination is specified, that serves as the cancel link.
if ($query->has('destination')) {
$options = UrlHelper::parse($query->get('destination'));
// @todo Use Url::fromPath() once https://www.drupal.org/node/2351379 is
// resolved.
$url = Url::fromUri('base://' . $options['path'], $options);
} else {
$url = $form->getCancelUrl();
}
return ['#type' => 'link', '#title' => $form->getCancelText(), '#url' => $url];
}
示例9: buildCancelLink
/**
* Builds the cancel link for a confirmation form.
*
* @param \Drupal\Core\Form\ConfirmFormInterface $form
* The confirmation form.
* @param \Symfony\Component\HttpFoundation\Request $request
* The current request.
*
* @return array
* The link render array for the cancel form.
*/
public static function buildCancelLink(ConfirmFormInterface $form, Request $request)
{
// Prepare cancel link.
$query = $request->query;
// If a destination is specified, that serves as the cancel link.
if ($query->has('destination')) {
$options = UrlHelper::parse($query->get('destination'));
$link = array('#href' => $options['path'], '#options' => $options);
} elseif ($route = $form->getCancelRoute()) {
$link = $route->toRenderArray();
}
$link['#type'] = 'link';
$link['#title'] = $form->getCancelText();
return $link;
}
示例10: getNextDestination
/**
* Returns the next redirect path in a multipage sequence.
*
* @param array $destinations
* An array of destinations to redirect to.
*
* @return \Drupal\Core\Url
* The next destination to redirect to.
*/
public static function getNextDestination(array $destinations)
{
$next_destination = array_shift($destinations);
if (is_array($next_destination)) {
$next_destination['options']['query']['destinations'] = $destinations;
$next_destination += array('route_parameters' => array());
$next_destination = Url::fromRoute($next_destination['route_name'], $next_destination['route_parameters'], $next_destination['options']);
} else {
$options = UrlHelper::parse($next_destination);
if ($destinations) {
$options['query']['destinations'] = $destinations;
}
// Redirect to any given path within the same domain.
// @todo Revisit this in https://www.drupal.org/node/2418219.
$next_destination = Url::fromUserInput('/' . $options['path']);
}
return $next_destination;
}
示例11: getNextDestination
/**
* Returns the next redirect path in a multipage sequence.
*
* @param array $destinations
* An array of destinations to redirect to.
*
* @return \Drupal\Core\Url
* The next destination to redirect to.
*/
public static function getNextDestination(array $destinations)
{
$next_destination = array_shift($destinations);
if (is_array($next_destination)) {
$next_destination['options']['query']['destinations'] = $destinations;
$next_destination += array('route_parameters' => array());
$next_destination = Url::fromRoute($next_destination['route_name'], $next_destination['route_parameters'], $next_destination['options']);
} else {
$options = UrlHelper::parse($next_destination);
if ($destinations) {
$options['query']['destinations'] = $destinations;
}
// Redirect to any given path within the same domain.
// @todo Use Url::fromPath() once https://www.drupal.org/node/2351379 is
// resolved.
$next_destination = Url::fromUri('base://' . $options['path']);
}
return $next_destination;
}
示例12: checkRedirectUrl
/**
* Allows manipulation of the response object when performing a redirect.
*
* @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
* The Event to process.
*/
public function checkRedirectUrl(FilterResponseEvent $event)
{
$response = $event->getResponse();
if ($response instanceof RedirectResponse) {
$options = array();
$request = $event->getRequest();
$destination = $request->query->get('destination');
// A destination from \Drupal::request()->query always overrides the
// current RedirectResponse. We do not allow absolute URLs to be passed
// via \Drupal::request()->query, as this can be an attack vector, with
// the following exception:
// - Absolute URLs that point to this site (i.e. same base URL and
// base path) are allowed.
if ($destination) {
if (!UrlHelper::isExternal($destination)) {
// The destination query parameter can be a relative URL in the sense
// of not including the scheme and host, but its path is expected to
// be absolute (start with a '/'). For such a case, prepend the
// scheme and host, because the 'Location' header must be absolute.
if (strpos($destination, '/') === 0) {
$destination = $request->getSchemeAndHttpHost() . $destination;
} else {
// Legacy destination query parameters can be relative paths that
// have not yet been converted to URLs (outbound path processors
// and other URL handling still needs to be performed).
// @todo As generateFromPath() is deprecated, remove this in
// https://www.drupal.org/node/2418219.
$destination = UrlHelper::parse($destination);
$path = $destination['path'];
$options['query'] = $destination['query'];
$options['fragment'] = $destination['fragment'];
// The 'Location' HTTP header must always be absolute.
$options['absolute'] = TRUE;
$destination = $this->urlGenerator->generateFromPath($path, $options);
}
$response->setTargetUrl($destination);
} elseif (UrlHelper::externalIsLocal($destination, $this->requestContext->getCompleteBaseUrl())) {
$response->setTargetUrl($destination);
}
}
}
}
示例13: viewElements
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode)
{
// Define element array.
$element = [];
foreach ($items as $delta => $item) {
// Convert the link-field item to a \Drupal\Core\Url object.
$url = $this->buildUrl($item);
// Parse the URL so we can check if the path contains a Github Gist. We do
// this to check if we should actually render the element or provide a
// notice in the log.
$url = UrlHelper::parse($url->getUri());
if (FALSE !== Unicode::strpos($url['path'], 'gist.github.com')) {
$element[$delta] = ['#theme' => 'gist_embed', '#url' => $url];
} else {
$entity = $items->getEntity();
$message = $this->t('The entity type: "@type" with the ID: "@id" is trying to use a link that isn\'t a Github Gist link for the Gist Embed Formatter.', array('@type' => ucfirst($entity->getEntityType()->id()), '@id' => $entity->id()));
\Drupal::logger('gist_embed')->notice($message);
}
}
return $element;
}
示例14: buildCancelLink
/**
* Builds the cancel link for a confirmation form.
*
* @param \Drupal\Core\Form\ConfirmFormInterface $form
* The confirmation form.
* @param \Symfony\Component\HttpFoundation\Request $request
* The current request.
*
* @return array
* The link render array for the cancel form.
*/
public static function buildCancelLink(ConfirmFormInterface $form, Request $request)
{
// Prepare cancel link.
$query = $request->query;
$url = NULL;
// If a destination is specified, that serves as the cancel link.
if ($query->has('destination')) {
$options = UrlHelper::parse($query->get('destination'));
// @todo Revisit this in https://www.drupal.org/node/2418219.
try {
$url = Url::fromUserInput('/' . ltrim($options['path'], '/'), $options);
} catch (\InvalidArgumentException $e) {
// Suppress the exception and fall back to the form's cancel url.
}
}
// Check for a route-based cancel link.
if (!$url) {
$url = $form->getCancelUrl();
}
return ['#type' => 'link', '#title' => $form->getCancelText(), '#attributes' => ['class' => ['button']], '#url' => $url];
}
示例15: checkRedirectUrl
/**
* Allows manipulation of the response object when performing a redirect.
*
* @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
* The Event to process.
*/
public function checkRedirectUrl(FilterResponseEvent $event)
{
$response = $event->getResponse();
if ($response instanceof RedirectResponse) {
$options = array();
$destination = $event->getRequest()->query->get('destination');
// A destination from \Drupal::request()->query always overrides the
// current RedirectResponse. We do not allow absolute URLs to be passed
// via \Drupal::request()->query, as this can be an attack vector, with
// the following exception:
// - Absolute URLs that point to this site (i.e. same base URL and
// base path) are allowed.
if ($destination && (!UrlHelper::isExternal($destination) || UrlHelper::externalIsLocal($destination, $GLOBALS['base_url']))) {
$destination = UrlHelper::parse($destination);
$path = $destination['path'];
$options['query'] = $destination['query'];
$options['fragment'] = $destination['fragment'];
// The 'Location' HTTP header must always be absolute.
$options['absolute'] = TRUE;
$response->setTargetUrl($this->urlGenerator->generateFromPath($path, $options));
}
}
}