You are here

protected function ThemeService::formatImage in Weather 8

Same name and namespace in other branches
  1. 2.0.x src/Service/ThemeService.php \Drupal\weather\Service\ThemeService::formatImage()

Returns the <img> tag for the weather image for the current condition.

Parameters

mixed $symbol: The weather condition number from yr.no.

string $condition: The translated condition text.

Return value

array Image render array.

1 call to ThemeService::formatImage()
ThemeService::setForecastsVariables in src/Service/ThemeService.php
Adds variables related to weather forecast.

File

src/Service/ThemeService.php, line 252

Class

ThemeService
ThemeService service.

Namespace

Drupal\weather\Service

Code

protected function formatImage($symbol, $condition) {

  // Support a custom image directory.
  // If the variable is not set or the specified file is not available,
  // fall back to the default images of the module.
  // Determine the active theme path.
  $theme_path = $this->themeManager
    ->getActiveTheme()
    ->getPath();
  $custom_path = $theme_path . '/' . $this->weatherConfig
    ->get('weather_image_directory') . '/';

  // Construct the filename.
  $image = $custom_path . $symbol . '.png';
  if (!is_readable($image)) {
    $default_path = drupal_get_path('module', 'weather') . '/images/';
    $image = $default_path . $symbol . '.png';
  }
  $size = getimagesize($image);

  // Prepare the <img> tag.
  return [
    '#theme' => 'image',
    '#uri' => $image,
    '#width' => $size[0],
    '#height' => $size[1],
    '#alt' => $condition,
    '#title' => $condition,
    '#attributes' => [
      'class' => 'weather-image',
    ],
  ];
}