Skip to content

Commit

Permalink
Merge pull request #787 from UN-OCHA/berliner/error-82-plan-version-a…
Browse files Browse the repository at this point in the history
…rgument

Use plan version argument also when fetching related entities
  • Loading branch information
berliner authored Nov 7, 2023
2 parents 8158d6f + 92ee240 commit b6b4188
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,11 @@ public function getEntityTypeName() {
return ucfirst(strtolower(implode(' ', $pieces)));
}

/**
* {@inheritdoc}
*/
public function getPlanId() {
return $this->getRawData()->planId;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,12 @@ public function getFullName();
*/
public function getTags();

/**
* Get the plan id to which the entity belongs.
*
* @return int
* The plan id.
*/
public function getPlanId();

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
namespace Drupal\ghi_plans\ApiObjects\Entities;

use Drupal\ghi_plans\Helpers\PlanEntityHelper;
use Drupal\ghi_plans\Traits\PlanVersionArgument;
use Drupal\hpc_api\Helpers\ApiEntityHelper;

/**
* Abstraction class for API plan entity objects.
*/
class PlanEntity extends EntityObjectBase {

use PlanVersionArgument;

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -113,7 +116,7 @@ public function getPlanEntityParents() {
return [];
}
return array_filter(array_map(function ($entity_id) {
return PlanEntityHelper::getPlanEntity($entity_id);
return PlanEntityHelper::getPlanEntity($entity_id, $this->getPlanVersionArgument());
}, $first_ref->planEntityIds));
}

Expand Down Expand Up @@ -184,7 +187,7 @@ public function getEntityName() {
* {@inheritdoc}
*/
public function getFullName() {
$parent_entity = $this->governing_entity_parent_id ? PlanEntityHelper::getGoverningEntity($this->governing_entity_parent_id) : NULL;
$parent_entity = $this->getParentGoverningEntity();
if (!$parent_entity) {
return $this->t('@type @custom_reference', [
'@type' => $this->name,
Expand All @@ -198,4 +201,29 @@ public function getFullName() {
]);
}

/**
* Get the parent governing entity.
*
* @return \Drupal\ghi_plans\ApiObjects\Entities\GoverningEntity
* The parent governing entity if found.
*/
private function getParentGoverningEntity() {
$entity_id = $this->governing_entity_parent_id ?? NULL;
if (!$entity_id) {
return NULL;
}
$entity = PlanEntityHelper::getGoverningEntity($entity_id, $this->getPlanVersionArgument());
return $entity instanceof GoverningEntity ? $entity : NULL;
}

/**
* Get the version argument to use for this entity.
*
* @return string
* The version argument as a string.
*/
private function getPlanVersionArgument() {
return $this->getPlanVersionArgumentForPlanId($this->getPlanId());
}

}
18 changes: 12 additions & 6 deletions html/modules/custom/ghi_plans/src/Helpers/PlanEntityHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,13 @@ public static function getPlanEntityObjects($plan_data) {
*
* @param int $entity_id
* The plan entity id for which to retrieve the data.
* @param string $version_argument
* The plan version argument.
*
* @return \Drupal\ghi_plans\ApiObjects\Entities\PlanEntity
* The plan entity object.
*/
public static function getPlanEntity($entity_id) {
public static function getPlanEntity($entity_id, $version_argument = 'current') {
/** @var \Drupal\hpc_api\Query\EndpointQuery $query */
$query = \Drupal::service('hpc_api.endpoint_query');
$query->setArguments([
Expand All @@ -96,22 +98,25 @@ public static function getPlanEntity($entity_id) {
'query_args' => [
'addPercentageOfTotalTarget' => 'true',
'disaggregation' => 'false',
'version' => 'current',
'version' => $version_argument,
],
]);
return new PlanEntity($query->getData());
$data = $query->getData();
return $data ? new PlanEntity($data) : NULL;
}

/**
* Get governing entity data from the API.
*
* @param int $entity_id
* The plan entity id for which to retrieve the data.
* @param string $version_argument
* The plan version argument.
*
* @return \Drupal\ghi_plans\ApiObjects\Entities\GoverningEntity
* The governing entity object.
*/
public static function getGoverningEntity($entity_id) {
public static function getGoverningEntity($entity_id, $version_argument = 'current') {
/** @var \Drupal\hpc_api\Query\EndpointQuery $query */
$query = \Drupal::service('hpc_api.endpoint_query');
$query->setArguments([
Expand All @@ -121,10 +126,11 @@ public static function getGoverningEntity($entity_id) {
'query_args' => [
'addPercentageOfTotalTarget' => 'true',
'disaggregation' => 'false',
'version' => 'current',
'version' => $version_argument,
],
]);
return new GoverningEntity($query->getData());
$data = $query->getData();
return $data ? new GoverningEntity($data) : NULL;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\ghi_plans\ApiObjects\PlanPrototype;
use Drupal\ghi_plans\Traits\PlanVersionArgument;
use Drupal\hpc_api\Helpers\ApiEntityHelper;

/**
Expand All @@ -13,6 +14,8 @@
*/
class PlanStructureHelper {

use PlanVersionArgument;

/**
* Retrieve the plan entity structure based on the given plan data.
*
Expand All @@ -26,6 +29,7 @@ public static function getPlanEntityStructure($plan_data) {

$plan_entities = PlanEntityHelper::getPlanEntityObjects($plan_data);
$governing_entities = PlanEntityHelper::getGoverningEntityObjects($plan_data);
$version_argument = self::getPlanVersionArgumentForPlanId($plan_data->id);

$remove_ids = [];
$ple_structure = [];
Expand All @@ -42,7 +46,7 @@ public static function getPlanEntityStructure($plan_data) {
// there.
$parent_id = $plan_entity->root_parent_id;
if (!array_key_exists($parent_id, $plan_entities)) {
$plan_entities[$parent_id] = PlanEntityHelper::getPlanEntity($parent_id);
$plan_entities[$parent_id] = PlanEntityHelper::getPlanEntity($parent_id, $version_argument);
}
$plan_entities[$parent_id]->addChild($plan_entity);
$remove_ids[] = $entity_id;
Expand All @@ -51,7 +55,7 @@ public static function getPlanEntityStructure($plan_data) {
// If not, put the PLEs according to their structure.
foreach ($plan_entity->support[0]->planEntityIds as $ple_id) {
if (!array_key_exists($ple_id, $plan_entities)) {
$plan_entities[$ple_id] = PlanEntityHelper::getPlanEntity($ple_id);
$plan_entities[$ple_id] = PlanEntityHelper::getPlanEntity($ple_id, $version_argument);
}
if ($plan_entities[$ple_id]->entity_type == 'PE') {
$ple_structure[$plan_entity->id] = $plan_entity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class AttachmentQuery extends EndpointQueryBase implements ContainerFactoryPlugi
* {@inheritdoc}
*/
public function getData(array $placeholders = [], array $query_args = []) {
$this->endpointQuery->setPlaceholders($placeholders);
if ($plan_id = $this->getPlaceholder('plan_id')) {
$query_args['version'] = $this->getPlanVersionArgumentForPlanId($plan_id);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class AttachmentSearchQuery extends EndpointQueryBase {
* {@inheritdoc}
*/
public function getData(array $placeholders = [], array $query_args = []) {
$this->endpointQuery->setPlaceholders($placeholders);
if ($plan_id = $this->getPlaceholder('plan_id')) {
$query_args['version'] = $this->getPlanVersionArgumentForPlanId($plan_id);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* endpoint = {
* "public" = "public/plan/{plan_id}",
* "authenticated" = "plan/{plan_id}",
* "api_key" = "plan/{plan_id}",
* "version" = "v2",
* "query" = {
* "content" = "entities",
Expand All @@ -46,6 +47,7 @@ class PlanEntitiesQuery extends EndpointQueryBase {
* {@inheritdoc}
*/
public function getData(array $placeholders = [], array $query_args = []) {
$this->endpointQuery->setPlaceholders($placeholders);
if ($plan_id = $this->getPlaceholder('plan_id')) {
$query_args['version'] = $this->getPlanVersionArgumentForPlanId($plan_id);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public static function create(ContainerInterface $container, array $configuratio
* {@inheritdoc}
*/
public function getData(array $placeholders = [], array $query_args = []) {
$this->endpointQuery->setPlaceholders($placeholders);
$year = $this->getPlaceholder('year');
if (!$year) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ trait PlanVersionArgument {
* @return string
* The version argument as a string for the API.
*/
public function getPlanVersionArgumentForPlanId($plan_id) {
public static function getPlanVersionArgumentForPlanId($plan_id) {
if (self::getCurrentUser()->isAnonymous()) {
return 'current';
}
Expand Down

0 comments on commit b6b4188

Please sign in to comment.