Added total cases to actors

This commit is contained in:
naomi 2022-05-08 18:34:02 +01:00
parent b8c592104d
commit b9481d39d5
4 changed files with 49 additions and 0 deletions

View File

@ -1,6 +1,16 @@
<?php <?php
use \Drupal\Core\Field\BaseFieldDefinition; use \Drupal\Core\Field\BaseFieldDefinition;
function opencase_cases_update_90005() {
$field_storage_definition = BaseFieldDefinition::create('integer')
->setLabel(t('Number of Cases'))
->setRevisionable(TRUE)
->setDisplayConfigurable('view', true);
\Drupal::entityDefinitionUpdateManager()
->installFieldStorageDefinition('total_cases', 'oc_actor', 'opencase_cases', $field_storage_definition);
}
function opencase_cases_update_90004() { function opencase_cases_update_90004() {
$field_storage_definition = BaseFieldDefinition::create('string_long') $field_storage_definition = BaseFieldDefinition::create('string_long')
->setRevisionable(TRUE) ->setRevisionable(TRUE)

View File

@ -97,6 +97,23 @@ function opencase_cases_oc_case_fee_delete(Drupal\opencase_cases\entity\OCCaseFe
$case->addToTotalFee(0 - $case_fee->amount->value); $case->addToTotalFee(0 - $case_fee->amount->value);
} }
function opencase_cases_oc_case_provision_insert(Drupal\opencase_cases\entity\OCCaseProvision $case_provision): void {
$provider = $case_provision->getProvider()->incrementTotalCases();
}
function opencase_cases_oc_case_provision_delete(Drupal\opencase_cases\entity\OCCaseProvision $case_provision): void {
$provider = $case_provision->getProvider()->decrementTotalCases();
}
function opencase_cases_oc_case_provision_update(Drupal\opencase_cases\entity\OCCaseProvision $case_provision): void {
$new_provider_id = $case_provision->oc_provider->target_id;
$old_provider_id = $case_provision->original->oc_provider->target_id;
if ($new_provider_id != $old_provider_id) {
$case_provision->getProvider()->incrementTotalCases();
$case_provision->original->getProvider()->decrementTotalCases();
}
}
function opencase_cases_entity_base_field_info($entity_type) { function opencase_cases_entity_base_field_info($entity_type) {
$fields = array(); $fields = array();

View File

@ -10,6 +10,8 @@ use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityPublishedTrait; use Drupal\Core\Entity\EntityPublishedTrait;
use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\user\UserInterface; use Drupal\user\UserInterface;
use Drupal\opencase_entities\Entity\OCActor;
use Error;
/** /**
* Defines the Case Provision entity. * Defines the Case Provision entity.
@ -201,6 +203,10 @@ class OCCaseProvision extends EditorialContentEntityBase implements OCCaseProvis
return $this; return $this;
} }
public function getProvider():?OCActor {
return OCActor::load($this->oc_provider->target_id);
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */

View File

@ -207,6 +207,17 @@ class OCActor extends RevisionableContentEntityBase implements OCActorInterface
return $this; return $this;
} }
public function incrementTotalCases(): void {
$this->set('total_cases', $this->total_cases->value + 1);
$this->save();
}
public function decrementTotalCases(): void {
$this->set('total_cases', $this->total_cases->value - 1);
$this->save();
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
@ -331,6 +342,11 @@ class OCActor extends RevisionableContentEntityBase implements OCActorInterface
->setDisplayConfigurable("form", true) ->setDisplayConfigurable("form", true)
->setDisplayConfigurable("view", true); ->setDisplayConfigurable("view", true);
$fields['total_cases'] = BaseFieldDefinition::create('integer')
->setLabel(t('Number of Cases'))
->setRevisionable(TRUE)
->setDisplayConfigurable('view', true);
$fields['created'] = BaseFieldDefinition::create('created') $fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Created')) ->setLabel(t('Created'))
->setDescription(t('The time that the entity was created.')); ->setDescription(t('The time that the entity was created.'));