From b4c9f26aef4ad8d20cf1f441dfb7f6a502b97191 Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Thu, 11 Jan 2024 15:59:59 +0100 Subject: [PATCH 1/4] Switch keyfigures to using figure_id --- PATCHES/keyfigure-figureid.diff | 149 ++++++++++++++++++++++++++++++++ composer.patches.json | 3 + 2 files changed, 152 insertions(+) create mode 100644 PATCHES/keyfigure-figureid.diff diff --git a/PATCHES/keyfigure-figureid.diff b/PATCHES/keyfigure-figureid.diff new file mode 100644 index 00000000..3ea3f338 --- /dev/null +++ b/PATCHES/keyfigure-figureid.diff @@ -0,0 +1,149 @@ +diff --git a/ocha_key_figures.install b/ocha_key_figures.install +new file mode 100644 +index 0000000..5d7e97a +--- /dev/null ++++ b/ocha_key_figures.install +@@ -0,0 +1,50 @@ ++getStorage('field_storage_config'); ++ $fields = $field_storage_config_storage->loadByProperties([ ++ 'type' => 'key_figure', ++ ]); ++ ++ /** @var \Drupal\field\Entity\FieldStorageConfig $field */ ++ foreach ($fields as $field) { ++ // Load all entities. ++ $entities = \Drupal::entityTypeManager()->getStorage($field->getTargetEntityTypeId())->loadMultiple(); ++ foreach ($entities as $entity) { ++ if ($entity instanceof ContentEntityBase) { ++ if (!$entity->hasField($field->getName())) { ++ continue; ++ } ++ ++ $values = $entity->get($field->getName())->getValue(); ++ print_r($values); ++ foreach ($values as &$value) { ++ if (!isset($value['id'])) { ++ continue; ++ } ++ ++ if (strpos($value['id'], '_') === FALSE) { ++ continue; ++ } ++ ++ $v = $value['id']; ++ $v = explode('_', $v); ++ $value['id'] = array_pop($v); ++ } ++ ++ $entity->set($field->getName(), $values); ++ $entity->save(); ++ } ++ } ++ } ++} +diff --git a/src/Controller/OchaKeyFiguresController.php b/src/Controller/OchaKeyFiguresController.php +index 66f1c2b..e858d75 100644 +--- a/src/Controller/OchaKeyFiguresController.php ++++ b/src/Controller/OchaKeyFiguresController.php +@@ -276,6 +276,41 @@ class OchaKeyFiguresController implements ContainerInjectionInterface { + return $data; + } + ++ /** ++ * Get figure by figure id. ++ */ ++ public function getFigureByFigureId(string $provider, string $country, string $year, string $figure_id) : array { ++ $query = [ ++ 'iso3' => $country, ++ 'year' => $year, ++ 'archived' => 0, ++ 'figure_id' => $figure_id, ++ 'order' => [ ++ 'year' => 'desc', ++ ], ++ ]; ++ ++ if ($year == 2) { ++ unset($query['year']); ++ // Get current and previous year. ++ $query['year'] = [ ++ date('Y'), ++ date('Y') - 1, ++ ]; ++ } ++ ++ $data = $this->query($provider, '', $query); ++ if (!$data) { ++ return []; ++ } ++ ++ // Keep first record. ++ $data = reset($data); ++ $data['cache_tags'] = $this->getCacheTags($data); ++ ++ return $data; ++ } ++ + /** + * Get the figures available for the figure provider, country and year. + * +@@ -1087,7 +1122,6 @@ class OchaKeyFiguresController implements ContainerInjectionInterface { + public function getExternalLookup(string $provider) { + $options = []; + +- $prefix = $this->getPrefix($provider); + $query = [ + 'provider' => $provider, + ]; +diff --git a/src/Plugin/Field/FieldFormatter/KeyFigureBase.php b/src/Plugin/Field/FieldFormatter/KeyFigureBase.php +index 4de34ca..6f1fd0f 100644 +--- a/src/Plugin/Field/FieldFormatter/KeyFigureBase.php ++++ b/src/Plugin/Field/FieldFormatter/KeyFigureBase.php +@@ -155,7 +155,7 @@ class KeyFigureBase extends FormatterBase { + $data = $this->ochaKeyFiguresApiClient->getFigures($provider, $country, $year); + if (!empty($data)) { + foreach ($data as $item) { +- $figures[$item['id']] = $item; ++ $figures[$item['figure_id']] = $item; + } + } + +diff --git a/src/Plugin/Field/FieldFormatter/KeyFigureCondensed.php b/src/Plugin/Field/FieldFormatter/KeyFigureCondensed.php +index 9cf3399..b6fb58a 100644 +--- a/src/Plugin/Field/FieldFormatter/KeyFigureCondensed.php ++++ b/src/Plugin/Field/FieldFormatter/KeyFigureCondensed.php +@@ -89,7 +89,7 @@ class KeyFigureCondensed extends KeyFigureBase { + + $data = []; + if ($item->getFigureProvider() != 'manual') { +- $data = $this->ochaKeyFiguresApiClient->getFigure($item->getFigureProvider(), strtolower($item->getFigureId())); ++ $data = $this->ochaKeyFiguresApiClient->getFigureByFigureId($item->getFigureProvider(), $item->getFigureCountry(), $item->getFigureYear(), strtolower($item->getFigureId())); + + if (isset($data['value'], $data['value_type'])) { + $value = $data['value']; +diff --git a/src/Plugin/Field/FieldWidget/KeyFigureBaseWidget.php b/src/Plugin/Field/FieldWidget/KeyFigureBaseWidget.php +index d3501c8..278f4d0 100644 +--- a/src/Plugin/Field/FieldWidget/KeyFigureBaseWidget.php ++++ b/src/Plugin/Field/FieldWidget/KeyFigureBaseWidget.php +@@ -250,7 +250,7 @@ abstract class KeyFigureBaseWidget extends WidgetBase { + $figures = []; + if (!empty($data)) { + foreach ($data as $item) { +- $figures[$item['id']] = $item; ++ $figures[$item['figure_id']] = $item; + } + } + diff --git a/composer.patches.json b/composer.patches.json index 8b5bfd42..55147cf8 100644 --- a/composer.patches.json +++ b/composer.patches.json @@ -19,6 +19,9 @@ }, "drupal/username_enumeration_prevention": { "Avoid leaking the path via Drupal.settings json": "PATCHES/username_enumeration_prevention-user-login-block-form-3312288.patch" + }, + "unocha/ocha_key_figures": { + "Switch to figure ids": "PATCHES/keyfigure-figureid.diff" } } } From 8bfcaa81fe26435e11f8aa0b289baa2b7aad52cf Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Fri, 12 Jan 2024 11:57:15 +0100 Subject: [PATCH 2/4] Use figure-id, add year to output --- PATCHES/keyfigure-figureid.diff | 149 ------------------ composer.lock | 14 +- composer.patches.json | 3 - .../Plugin/Field/FieldFormatter/Donors.php | 6 +- .../ocha-key-figures-figure.html.twig | 5 +- 5 files changed, 15 insertions(+), 162 deletions(-) delete mode 100644 PATCHES/keyfigure-figureid.diff diff --git a/PATCHES/keyfigure-figureid.diff b/PATCHES/keyfigure-figureid.diff deleted file mode 100644 index 3ea3f338..00000000 --- a/PATCHES/keyfigure-figureid.diff +++ /dev/null @@ -1,149 +0,0 @@ -diff --git a/ocha_key_figures.install b/ocha_key_figures.install -new file mode 100644 -index 0000000..5d7e97a ---- /dev/null -+++ b/ocha_key_figures.install -@@ -0,0 +1,50 @@ -+getStorage('field_storage_config'); -+ $fields = $field_storage_config_storage->loadByProperties([ -+ 'type' => 'key_figure', -+ ]); -+ -+ /** @var \Drupal\field\Entity\FieldStorageConfig $field */ -+ foreach ($fields as $field) { -+ // Load all entities. -+ $entities = \Drupal::entityTypeManager()->getStorage($field->getTargetEntityTypeId())->loadMultiple(); -+ foreach ($entities as $entity) { -+ if ($entity instanceof ContentEntityBase) { -+ if (!$entity->hasField($field->getName())) { -+ continue; -+ } -+ -+ $values = $entity->get($field->getName())->getValue(); -+ print_r($values); -+ foreach ($values as &$value) { -+ if (!isset($value['id'])) { -+ continue; -+ } -+ -+ if (strpos($value['id'], '_') === FALSE) { -+ continue; -+ } -+ -+ $v = $value['id']; -+ $v = explode('_', $v); -+ $value['id'] = array_pop($v); -+ } -+ -+ $entity->set($field->getName(), $values); -+ $entity->save(); -+ } -+ } -+ } -+} -diff --git a/src/Controller/OchaKeyFiguresController.php b/src/Controller/OchaKeyFiguresController.php -index 66f1c2b..e858d75 100644 ---- a/src/Controller/OchaKeyFiguresController.php -+++ b/src/Controller/OchaKeyFiguresController.php -@@ -276,6 +276,41 @@ class OchaKeyFiguresController implements ContainerInjectionInterface { - return $data; - } - -+ /** -+ * Get figure by figure id. -+ */ -+ public function getFigureByFigureId(string $provider, string $country, string $year, string $figure_id) : array { -+ $query = [ -+ 'iso3' => $country, -+ 'year' => $year, -+ 'archived' => 0, -+ 'figure_id' => $figure_id, -+ 'order' => [ -+ 'year' => 'desc', -+ ], -+ ]; -+ -+ if ($year == 2) { -+ unset($query['year']); -+ // Get current and previous year. -+ $query['year'] = [ -+ date('Y'), -+ date('Y') - 1, -+ ]; -+ } -+ -+ $data = $this->query($provider, '', $query); -+ if (!$data) { -+ return []; -+ } -+ -+ // Keep first record. -+ $data = reset($data); -+ $data['cache_tags'] = $this->getCacheTags($data); -+ -+ return $data; -+ } -+ - /** - * Get the figures available for the figure provider, country and year. - * -@@ -1087,7 +1122,6 @@ class OchaKeyFiguresController implements ContainerInjectionInterface { - public function getExternalLookup(string $provider) { - $options = []; - -- $prefix = $this->getPrefix($provider); - $query = [ - 'provider' => $provider, - ]; -diff --git a/src/Plugin/Field/FieldFormatter/KeyFigureBase.php b/src/Plugin/Field/FieldFormatter/KeyFigureBase.php -index 4de34ca..6f1fd0f 100644 ---- a/src/Plugin/Field/FieldFormatter/KeyFigureBase.php -+++ b/src/Plugin/Field/FieldFormatter/KeyFigureBase.php -@@ -155,7 +155,7 @@ class KeyFigureBase extends FormatterBase { - $data = $this->ochaKeyFiguresApiClient->getFigures($provider, $country, $year); - if (!empty($data)) { - foreach ($data as $item) { -- $figures[$item['id']] = $item; -+ $figures[$item['figure_id']] = $item; - } - } - -diff --git a/src/Plugin/Field/FieldFormatter/KeyFigureCondensed.php b/src/Plugin/Field/FieldFormatter/KeyFigureCondensed.php -index 9cf3399..b6fb58a 100644 ---- a/src/Plugin/Field/FieldFormatter/KeyFigureCondensed.php -+++ b/src/Plugin/Field/FieldFormatter/KeyFigureCondensed.php -@@ -89,7 +89,7 @@ class KeyFigureCondensed extends KeyFigureBase { - - $data = []; - if ($item->getFigureProvider() != 'manual') { -- $data = $this->ochaKeyFiguresApiClient->getFigure($item->getFigureProvider(), strtolower($item->getFigureId())); -+ $data = $this->ochaKeyFiguresApiClient->getFigureByFigureId($item->getFigureProvider(), $item->getFigureCountry(), $item->getFigureYear(), strtolower($item->getFigureId())); - - if (isset($data['value'], $data['value_type'])) { - $value = $data['value']; -diff --git a/src/Plugin/Field/FieldWidget/KeyFigureBaseWidget.php b/src/Plugin/Field/FieldWidget/KeyFigureBaseWidget.php -index d3501c8..278f4d0 100644 ---- a/src/Plugin/Field/FieldWidget/KeyFigureBaseWidget.php -+++ b/src/Plugin/Field/FieldWidget/KeyFigureBaseWidget.php -@@ -250,7 +250,7 @@ abstract class KeyFigureBaseWidget extends WidgetBase { - $figures = []; - if (!empty($data)) { - foreach ($data as $item) { -- $figures[$item['id']] = $item; -+ $figures[$item['figure_id']] = $item; - } - } - diff --git a/composer.lock b/composer.lock index ec8c5acf..b07bc847 100644 --- a/composer.lock +++ b/composer.lock @@ -15496,16 +15496,16 @@ }, { "name": "unocha/ocha_key_figures", - "version": "2.1.8", + "version": "2.1.9", "source": { "type": "git", "url": "https://github.com/UN-OCHA/ocha_key_figures.git", - "reference": "300a8a55ce875cf59aafdc9eeb58324be7dfdfc2" + "reference": "ab3d78f0c86f655b156ebde57f8cae37533ea4d0" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/UN-OCHA/ocha_key_figures/zipball/300a8a55ce875cf59aafdc9eeb58324be7dfdfc2", - "reference": "300a8a55ce875cf59aafdc9eeb58324be7dfdfc2", + "url": "https://api.github.com/repos/UN-OCHA/ocha_key_figures/zipball/ab3d78f0c86f655b156ebde57f8cae37533ea4d0", + "reference": "ab3d78f0c86f655b156ebde57f8cae37533ea4d0", "shasum": "" }, "require": { @@ -15519,9 +15519,9 @@ "description": "UNOCHA Key Figures", "support": { "issues": "https://github.com/UN-OCHA/ocha_key_figures/issues", - "source": "https://github.com/UN-OCHA/ocha_key_figures/tree/2.1.8" + "source": "https://github.com/UN-OCHA/ocha_key_figures/tree/2.1.9" }, - "time": "2023-08-31T09:38:22+00:00" + "time": "2024-01-12T10:52:33+00:00" }, { "name": "unocha/ocha_monitoring", @@ -17139,5 +17139,5 @@ "php": ">=8.2" }, "platform-dev": [], - "plugin-api-version": "2.6.0" + "plugin-api-version": "2.3.0" } diff --git a/composer.patches.json b/composer.patches.json index 55147cf8..8b5bfd42 100644 --- a/composer.patches.json +++ b/composer.patches.json @@ -19,9 +19,6 @@ }, "drupal/username_enumeration_prevention": { "Avoid leaking the path via Drupal.settings json": "PATCHES/username_enumeration_prevention-user-login-block-form-3312288.patch" - }, - "unocha/ocha_key_figures": { - "Switch to figure ids": "PATCHES/keyfigure-figureid.diff" } } } diff --git a/html/modules/custom/unocha_donors/src/Plugin/Field/FieldFormatter/Donors.php b/html/modules/custom/unocha_donors/src/Plugin/Field/FieldFormatter/Donors.php index 41879499..f68c1649 100644 --- a/html/modules/custom/unocha_donors/src/Plugin/Field/FieldFormatter/Donors.php +++ b/html/modules/custom/unocha_donors/src/Plugin/Field/FieldFormatter/Donors.php @@ -33,9 +33,11 @@ public function viewElements(FieldItemListInterface $items, $langcode) { $elements = []; foreach ($items as $delta => $item) { if ($field_type === 'key_figure') { - $data = $this->ochaKeyFiguresApiClient->getFigure( + $data = $this->ochaKeyFiguresApiClient->getFigureByFigureId( $item->getFigureProvider(), - $item->getFigureId() + $item->getFigureYear(), + $item->getFigureCountry(), + $item->getFigureId(), ); } else { diff --git a/html/themes/custom/common_design_subtheme/templates/modules/ocha_key_figures/ocha-key-figures-figure.html.twig b/html/themes/custom/common_design_subtheme/templates/modules/ocha_key_figures/ocha-key-figures-figure.html.twig index 84866eff..e8de27a9 100644 --- a/html/themes/custom/common_design_subtheme/templates/modules/ocha_key_figures/ocha-key-figures-figure.html.twig +++ b/html/themes/custom/common_design_subtheme/templates/modules/ocha_key_figures/ocha-key-figures-figure.html.twig @@ -30,7 +30,10 @@ ({{ unit }}) {%- endif -%} - {{ value == 0 ? 0 : value }} + + {{ value == 0 ? 0 : value }} + {{ year }} + {% endif %} From 520dc5b1156e2f3fe60fccd60cbc30a7b1d0e63b Mon Sep 17 00:00:00 2001 From: left23 Date: Fri, 12 Jan 2024 12:24:22 +0100 Subject: [PATCH 3/4] chore: first pass styles for figure year Refs: OHA-63 --- .../components/uno-figures/uno-figures.css | 10 ++++++++++ .../ocha_key_figures/ocha-key-figures-figure.html.twig | 6 +++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/html/themes/custom/common_design_subtheme/components/uno-figures/uno-figures.css b/html/themes/custom/common_design_subtheme/components/uno-figures/uno-figures.css index 245edeba..60fbfd3e 100644 --- a/html/themes/custom/common_design_subtheme/components/uno-figures/uno-figures.css +++ b/html/themes/custom/common_design_subtheme/components/uno-figures/uno-figures.css @@ -39,11 +39,21 @@ dt.uno-figure__label-wrapper { color: var(--brand-primary); font-size: 1.625rem; font-weight: bold; + display: flex; + flex-wrap: wrap; } dd.uno-figure__value { margin: 0; } +dd.uno-figure__value > .uno-figure__year { + clear: both; + font-size: 1rem; + font-weight: 400; + flex: 1 0 100%; + color: var(--brand-default-text-color); +} + @media screen and (min-width: 480px) { .uno-figure { display: flex; diff --git a/html/themes/custom/common_design_subtheme/templates/modules/ocha_key_figures/ocha-key-figures-figure.html.twig b/html/themes/custom/common_design_subtheme/templates/modules/ocha_key_figures/ocha-key-figures-figure.html.twig index 84866eff..0e91f94c 100644 --- a/html/themes/custom/common_design_subtheme/templates/modules/ocha_key_figures/ocha-key-figures-figure.html.twig +++ b/html/themes/custom/common_design_subtheme/templates/modules/ocha_key_figures/ocha-key-figures-figure.html.twig @@ -30,7 +30,11 @@ ({{ unit }}) {%- endif -%} - {{ value == 0 ? 0 : value }} + {{ value == 0 ? 0 : value }} + {%- if year is not empty -%} + {{ year }} + {%- endif -%} + {% endif %} From 8dbcf47847f4f899ec8530b9bba2079c7a7c5380 Mon Sep 17 00:00:00 2001 From: left23 Date: Fri, 12 Jan 2024 12:47:53 +0100 Subject: [PATCH 4/4] chore: remove label classes Refs: OHA-63 --- .../modules/ocha_key_figures/ocha-key-figures-figure.html.twig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/html/themes/custom/common_design_subtheme/templates/modules/ocha_key_figures/ocha-key-figures-figure.html.twig b/html/themes/custom/common_design_subtheme/templates/modules/ocha_key_figures/ocha-key-figures-figure.html.twig index e8de27a9..112ed913 100644 --- a/html/themes/custom/common_design_subtheme/templates/modules/ocha_key_figures/ocha-key-figures-figure.html.twig +++ b/html/themes/custom/common_design_subtheme/templates/modules/ocha_key_figures/ocha-key-figures-figure.html.twig @@ -32,7 +32,7 @@ {{ value == 0 ? 0 : value }} - {{ year }} + {{ year }} {% endif %}