Skip to content
This repository has been archived by the owner on Nov 20, 2024. It is now read-only.

D9 compatibility changing $file->url() to $file->toUrl() #124

Open
queenielow opened this issue May 19, 2020 · 1 comment
Open

D9 compatibility changing $file->url() to $file->toUrl() #124

queenielow opened this issue May 19, 2020 · 1 comment

Comments

@queenielow
Copy link

Hi There,

I have encountered an error below when changing from $file->url() to $file->toUrl() as to adapt the compatibility for D9.

Below error will appear if the site is using header background image in the theme settings. Below is the code found in .theme file.

$background_image = theme_get_setting('header_background_image');

To fix the issue, you need to use $path = file_create_url($file->getFileUri()); instead of $file->toUrl();

The website encountered an unexpected error. Please try again later.

Drupal\Core\Entity\Exception\UndefinedLinkTemplateException: No link template 'canonical' found for the 'file' entity type in Drupal\Core\Entity\EntityBase->toUrl() (line 227 of core/lib/Drupal/Core/Entity/EntityBase.php).
govcms8_uikit_starter_preprocess_html(Array, 'html', Array) (Line: 287)
Drupal\Core\Theme\ThemeManager->render('html', Array) (Line: 431)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 200)
Drupal\Core\Render\Renderer->render(Array) (Line: 147)
Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 573)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 148)
Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object) (Line: 90)
Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object)
call_user_func(Array, Object, 'kernel.view', Object) (Line: 111)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.view', Object) (Line: 156)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 68)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 50)
Drupal\ban\BanMiddleware->handle(Object, 1, 1) (Line: 84)
Drupal\shield\ShieldMiddleware->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 52)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 708)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
@maedi
Copy link

maedi commented May 19, 2020

We've done some more research and it looks like the best replacement is:

$file->createFileUrl();

For more info see:
https://www.drupal.org/project/drupal/issues/2402533

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants