Skip to content

Commit

Permalink
chore: update codebase for compatibility with D10
Browse files Browse the repository at this point in the history
Refs: ODSG-45
  • Loading branch information
orakili committed Aug 23, 2023
1 parent dc244f5 commit 4aeb8d7
Show file tree
Hide file tree
Showing 5 changed files with 227 additions and 29 deletions.
14 changes: 14 additions & 0 deletions html/modules/custom/odsg_ocha/odsg_ocha.module
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,17 @@
* @file
* ODSG OCHA module file.
*/

use Drupal\views\ViewExecutable;

/**
* Implements hook_views_pre_build().
*/
function odsg_ocha_views_pre_build(ViewExecutable $view) {
// Set the year filter to the current year as default.
if ($view->id() === 'ocha_funding') {
if (empty($view->getExposedInput())) {
$view->setExposedInput(['year' => gmdate('Y')]);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/**
* Settings form for the OCHA feeds.
*/
class OchaFeedsSettingsForm extends FormBase {
final class OchaFeedsSettingsForm extends FormBase {

/**
* The state service.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Drupal\odsg_ocha\Plugin\views\filter;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\filter\FilterPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Simple filter to select a year for the OCHA funding.
Expand All @@ -12,7 +14,14 @@
*
* @ViewsFilter("odsg_funding_year_filter")
*/
class OdsgFundingYearFilter extends FilterPluginBase {
final class OdsgFundingYearFilter extends FilterPluginBase {

/**
* The config factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;

/**
* Disable the possibility to force a single value.
Expand All @@ -21,6 +30,40 @@ class OdsgFundingYearFilter extends FilterPluginBase {
*/
protected $alwaysMultiple = TRUE;

/**
* Constructs a PluginBase object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory.
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
ConfigFactoryInterface $config_factory
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->configFactory = $config_factory;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('config.factory')
);
}

/**
* Provide simple equality operator.
*
Expand All @@ -38,7 +81,7 @@ public function operatorOptions() {
*/
protected function valueForm(&$form, FormStateInterface $form_state) {
$end = date('Y');
$start = \Drupal::config('odsg_ocha.settings')->get('funding.start') ?? $end - 10;
$start = $this->configFactory->get('odsg_ocha.settings')?->get('funding.start') ?? $end - 10;
$range = range($end, $start, -1);

$form['value'] = [
Expand Down
114 changes: 92 additions & 22 deletions html/modules/custom/odsg_ocha/src/Plugin/views/query/OdsgOchaFeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

namespace Drupal\odsg_ocha\Plugin\views\query;

use Drupal\Component\Datetime\TimeInterface;
use Drupal\Component\Serialization\Json;
use Drupal\Component\Utility\Html;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\State\StateInterface;
use Drupal\Core\Utility\Error;
use Drupal\views\Plugin\views\query\QueryPluginBase;
use Drupal\views\ResultRow;
use Drupal\views\ViewExecutable;
Expand All @@ -28,7 +33,14 @@
*
* @see https://www.drupal.org/node/2484565
*/
class OdsgOchaFeed extends QueryPluginBase {
final class OdsgOchaFeed extends QueryPluginBase {

/**
* The config factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;

/**
* GuzzleHttp\Client definition.
Expand All @@ -37,6 +49,41 @@ class OdsgOchaFeed extends QueryPluginBase {
*/
protected $httpClient;

/**
* The logger factory.
*
* @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
*/
protected $loggerFactory;

/**
* The state service.
*
* @var \Drupal\Core\State\StateInterface
*/
protected $state;

/**
* The time service.
*
* @var \Drupal\Component\Datetime\TimeInterface
*/
protected $time;

/**
* The feed information from the configuration.
*
* @var array
*/
protected $feeds;

/**
* The OCHA base feed URL.
*
* @var string
*/
protected $baseFeedUrl;

/**
* Constructs a PluginBase object.
*
Expand All @@ -46,13 +93,34 @@ class OdsgOchaFeed extends QueryPluginBase {
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory.
* @param \GuzzleHttp\Client $http_client
* The http_client.
* @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory
* The logger factory.
* @param \Drupal\Core\State\StateInterface $state
* The state service.
* @param \Drupal\Component\Datetime\TimeInterface $time
* The time service.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, Client $http_client) {
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
ConfigFactoryInterface $config_factory,
Client $http_client,
LoggerChannelFactoryInterface $logger_factory,
StateInterface $state,
TimeInterface $time
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);

$this->configFactory = $config_factory;
$this->httpClient = $http_client;
$this->loggerFactory = $logger_factory;
$this->state = $state;
$this->time = $time;
}

/**
Expand All @@ -63,7 +131,11 @@ public static function create(ContainerInterface $container, array $configuratio
$configuration,
$plugin_id,
$plugin_definition,
$container->get('http_client')
$container->get('config.factory'),
$container->get('http_client'),
$container->get('logger.factory'),
$container->get('state'),
$container->get('datetime.time')
);
}

Expand Down Expand Up @@ -120,7 +192,7 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);

// Retrieve the list of feeds fromt the configuration.
$feeds = \Drupal::config('odsg_ocha.settings')->get('feeds') ?? [];
$feeds = $this->configFactory->get('odsg_ocha.settings')?->get('feeds') ?? [];

$options = ['' => $this->t('- Select a feed -')];
foreach ($feeds as $key => $feed) {
Expand All @@ -140,7 +212,7 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) {
* {@inheritdoc}
*/
public function execute(ViewExecutable $view) {
$feeds = static::getOchaFeedData();
$feeds = $this->getOchaFeedData();

$view->result = [];

Expand Down Expand Up @@ -184,7 +256,7 @@ public function execute(ViewExecutable $view) {
$total = $data['totalCount'] ?? 0;

$index = 0;
$unocha_url = rtrim(static::getOchaFeedBaseUrl(), '/') . '/';
$unocha_url = rtrim($this->getOchaFeedBaseUrl(), '/') . '/';
foreach ($data['data'] ?? [] as $item) {
// Convert the RW API data to what was returned by the OCHA feeds.
if (empty($item['fields'])) {
Expand All @@ -211,7 +283,7 @@ public function execute(ViewExecutable $view) {
}
}
catch (\Exception $exception) {
watchdog_exception('odsg_ocha', $exception);
Error::logException($this->loggerFactory->get('odsg_ocha'), $exception);
}
}

Expand All @@ -221,7 +293,7 @@ public function execute(ViewExecutable $view) {

// Explicitly set the number of rows and the execution time.
$view->total_rows = count($view->result);
$view->execute_time = time() - REQUEST_TIME;
$view->execute_time = time() - $this->time->getRequestTime();
}

/**
Expand All @@ -238,7 +310,7 @@ public function execute(ViewExecutable $view) {
* Sanitized data or empty array if invalid.
*/
protected function validateDocument(array $data) {
$data['link'] = static::getOchaFeedBaseUrl() . ltrim($data['path'] ?? '', '/');
$data['link'] = $this->getOchaFeedBaseUrl() . ltrim($data['path'] ?? '', '/');

$filters = [
// Node id on the https://www.unocha.org.
Expand Down Expand Up @@ -359,7 +431,7 @@ protected function validateDate($date) {
* URL or NULL if the validation failed.
*/
protected function validateUrl($url) {
$base_url = static::getOchaFeedBaseUrl();
$base_url = $this->getOchaFeedBaseUrl();

if (!is_string($url) || empty($base_url)) {
return NULL;
Expand All @@ -380,17 +452,16 @@ protected function validateUrl($url) {
* @return array
* Feeds data.
*/
public static function getOchaFeedData() {
static $feeds;
if (!isset($feeds)) {
$feeds = \Drupal::config('odsg_ocha.settings')->get('feeds') ?? [];
public function getOchaFeedData() {
if (!isset($this->feeds)) {
$this->feeds = $this->configFactory->get('odsg_ocha.settings')?->get('feeds') ?? [];

// Use the overriden feeds in state if any.
foreach ($feeds as $feed => $info) {
$feeds[$feed]['url'] = \Drupal::state()->get('odsg_ocha.feeds.' . $feed, '');
foreach ($this->feeds as $feed => $info) {
$this->feeds[$feed]['url'] = $this->state->get('odsg_ocha.feeds.' . $feed, '');
}
}
return $feeds;
return $this->feeds;
}

/**
Expand All @@ -399,12 +470,11 @@ public static function getOchaFeedData() {
* @return string
* Base URL.
*/
public static function getOchaFeedBaseUrl() {
static $base_url;
if (!isset($base_url)) {
$base_url = trim(\Drupal::config('odsg_ocha.settings')->get('base-url') ?? '');
public function getOchaFeedBaseUrl() {
if (!isset($this->baseFeedUrl)) {
$this->baseFeedUrl = trim($this->configFactory->get('odsg_ocha.settings')?->get('base-url') ?? '');
}
return $base_url;
return $this->baseFeedUrl;
}

}
Loading

0 comments on commit 4aeb8d7

Please sign in to comment.