Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
API page: https://api.drupal.org/api/drupal/core!lib!Drupal.php/function/Drupal%3A...
The documentation says to use $link = Link::fromTextAndUrl($text, $url); instead, as the function is deprecated, but this is leading to a fatal error when passed into Drupal set message.
This works:
$url = Url::fromUri(file_create_url($file->getFileUri()));
$link = \Drupal::l($file->label(), $url);
drupal_set_message(t('PDF added: @link', ['@link' => $link]));
and this works:
$url = Url::fromUri(file_create_url($file->getFileUri()));
$link = \Drupal::service('link_generator')->generate($file->label(), $url);
drupal_set_message(t('PDF added: @link', ['@link' => $link]));
But this doesn't:
$url = Url::fromUri(file_create_url($file->getFileUri()));
$link = Link::fromTextAndUrl($file->label(), $url);
drupal_set_message(t('PDF added: @link', ['@link' => $link]));
So the documentation needs to be updated. It looks like \Drupal::l returns a FileGenerated object, not a File Object
Comments
Comment #12
quietone CreditAttribution: quietone at PreviousNext commented@oknate, Thank you for reporting this problem. We rely on issue reports like this to improve Drupal core.
l was deprecated and removed in Drupal 9.0. Therefore, closing as outdated. If this is incorrect reopen the issue, by setting the status to 'Active', and add a comment explaining what still needs to be done.
Thanks!