1

I am porting code from Drupal 7 to Drupal 8.

In Drupal 7, my code looks like the following:

return url($parts[0] . '/' . $parts[1] . '/profile/id/' . $context['profile']->uuid . '/cancel');

I know that I have to use Drupal::url(), but I have to use a route name or an external URI, but I don't know what to pass.

How should I update my code for Drupal 8?

7
  • kiamlaluno, what part of porting my code to Drupal 8 is not clear to you?
    – Dfan
    Commented Nov 11, 2015 at 10:58
  • I edited my question, I hope it's clear as water to you.
    – Dfan
    Commented Nov 11, 2015 at 11:05
  • I think you have to tell us, where do you want to put this code, what function is this and what you want to return. The code you are using now looks like drupal 6, and you don't want keep this in drupal 8.
    – 4uk4
    Commented Nov 11, 2015 at 12:19
  • I think that the question is "How do I create a dynamic Url from a menu router item that I can use in a link (or possibly in a redirect)?"
    – mradcliffe
    Commented Nov 11, 2015 at 12:42
  • return Url::fromRoute('your_route_name', ['profile' => 123'])) assuming the parts 0 and 1 are not entities or some sort of placeholders. If so then they would be part of that second argument.
    – user21641
    Commented Nov 11, 2015 at 14:33

1 Answer 1

0

Try following code:

$path = '/' . $parts[0] . '/' . $parts[1] . '/profile/id/' . $context['profile']->uuid . '/cancel';
$url_object = \Drupal::service('path.validator')->getUrlIfValid($path);
$url_object->setAbsolute();
$url = $url_object->toString();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.