Hi,

I am learning Drupal 8 and want to rewrite a module from Drupal 7. In my 7 module I have some links with images instead of text.
In Drupal 7 this looks like

$Link=l('Only local images are allowed.
Text ', 'http://www.somewhere.xyz', array('html' => TRUE));

This seams not to work anymore. I found 'html' => TRUE is deprecated:
https://www.drupal.org/node/2273923

Here I found this to use in Drupal 8:

\Drupal::l(String::format('@cell_content@image', array('@cell_content' => $cell_content, '@image' => $image)), ...

... but it is not working! Error.log complains:

[Tue Jan 05 23:06:08.753111 2016] [:error] [pid 5172] [client ::1:54416] PHP Fatal error: Class 'Drupal\\bb\\Controller\\String' not found in /var/www/html/d8/modules/bb/src/Controller/bbDatenbankabfragen.php on line 72

What's my Error?

Thank you,
Stefan

Comments

XTaz’s picture

I'm not sure if is the good way to do but i use something like this:

$module_path = drupal_get_path('module', 'my_module');
$path_image = '/' . $module_path . '/images/check.png';

$logo_render_array = [
    '#theme' => 'image',
    '#title' => t('Title'),
    '#alt' => t('Title'),
    '#style_name' => 'original',
    '#uri' => $path_image,    
    '#attributes' => array('class' => 'my-class',     
        ),
];
$img = \Drupal::service('renderer')->render($logo_render_array);
$url = Url::fromUri('http://drupal.org');
$link = \Drupal::l($img, $url);