LiveWeatherController.php in Live Weather 8
File
src/Controller/LiveWeatherController.php
View source
<?php
namespace Drupal\live_weather\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Component\Utility\Html;
class LiveWeatherController extends ControllerBase {
protected $formBuilder;
protected $configFactory;
public function __construct(FormBuilderInterface $form_builder, ConfigFactoryInterface $config_factory) {
$this->formBuilder = $form_builder;
$this->configFactory = $config_factory;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('form_builder'), $container
->get('config.factory'));
}
public function locationList() {
$rows = array();
$location_list = $this->configFactory
->get('live_weather.location')
->get('location');
$build['live_weather_form'] = $this->formBuilder
->getForm('Drupal\\live_weather\\Form\\LiveWeatherForm');
$header = array(
t('Woeid'),
t('Location'),
array(
'data' => t('Operations'),
'colspan' => 2,
),
);
if (!empty($location_list)) {
foreach ($location_list as $key => $value) {
$data['woeid'] = $key;
$data['location'] = Html::escape($value);
$operations = array();
$operations['delete'] = array(
'title' => $this
->t('Delete'),
'url' => Url::fromRoute('live_weather.delete', [
'woeid' => $key,
]),
);
$data['operations'] = array(
'data' => array(
'#type' => 'operations',
'#links' => $operations,
),
);
$rows[] = $data;
}
}
$build['live_weather_table'] = array(
'#type' => 'table',
'#header' => $header,
'#rows' => $rows,
'#empty' => $this
->t('No locations available.'),
);
return $build;
}
}