If config is missing return empty build array and throw an error to the drupal logger

There is a function that returns the name of the city. If the user’s ip exists and the site http://ip-api.com can determine the user’s location city by ip address, then the city name is taken from json, if not, then from the configuration.

  public function getCity() {       $ip = '193.62.157.66';     try {         $response_ip = $this->httpClient->get('http://ip-api.com/json/' . $ip);       $response_data_ip = $response_ip->getBody();       $data_ip = json_decode($response_data_ip);       if (  $data_ip->status == 'success') {         return $data_ip->city;       }       else {         return $this->configFactory->get('sydneypro_weather.settings')->get('weather_city');       }     }     catch (  RequestException $e) {       return FALSE;     }   } 

But the configuration is added to ignore using the Config ignore module and when deploy the website to another environment, my configuration (which is ignored) will not be created in the db during drush cim.

Therefore, if the configuration is missing, I need to return empty build array and throw an error to the drupal logger: https://api.drupal.org/api/drupal/core%21lib%21Drupal.php/function/Drupal%3A%3Alogger/8.2.x

I looked at several examples that drupal logger is used something like this:
Drupal::logger(' ')->error(' ', []);

But I don’t understand how in my case to return an empty build array, throw an error in drupal logger and where to insert all this in the code ? In catch? Or somewhere else in the WeatherBlock block?

The code of my whole block WeatherBlock:

class WeatherBlock extends BlockBase implements ContainerFactoryPluginInterface {     // dependency injection code here      public function getCity() {       $ip = '193.62.157.66';     try {         $response_ip = $this->httpClient->get('http://ip-api.com/json/' . $ip);       $response_data_ip = $response_ip->getBody();       $data_ip = json_decode($response_data_ip);       if (  $data_ip->status == 'success') {         return $data_ip->city;       }       else {         return $this->configFactory->get('sydneypro_weather.settings')->get('weather_city');       }     }     catch (  RequestException $e) {       return FALSE;     }   }    public function   build() {     $client = $this->httpClient;     $api_key = $this->configFactory->get('sydneypro_weather.settings')->get('weather_api_key');     $cid = 'sydneypro_weather';     if (empty(  $api_key)) {       return [         '#type' => 'markup',         '#markup' => $this->t('Please enter your API key in the Admin panel to see the weather'),       ];     }     try {       if (  $cache = $this->cacheBackend->get($cid)) {         $data = $cache->data;       }       else {         $response = $client->get('http://api.openweathermap.org/data/2.5/weather?q=' . $this->getCity() . ',&appid=' . $api_key . '&units=metric');         $response_data = $response->getBody();         $data = json_decode($response_data);         $this->cacheBackend->set($cid, $data, $this->time->getRequestTime() + 21600);       }         $build = [         '#theme' => 'weather_block',         '#data' => $data,         '#attached' => [           'library' => [             'sydneypro_weather/sydneypro_weather',           ],         ],       ];       return   $build;     }      catch (  RequestException $e) {       return [         '#markup' => Markup::create('<h1>Sorry, no data available</h1>'),       ];     }   }  }  
This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

If config is missing return empty build array and throw an error to the drupal logger

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.