From cde0bbc7e6a7b157cb3ea1cbde1e6798335ae49c Mon Sep 17 00:00:00 2001 From: Naomi Date: Tue, 17 Nov 2020 11:24:18 +0000 Subject: [PATCH] moved actors_involved field to defaults --- .../opencase_defaults.module | 42 +++++++++++++++++++ .../opencase_entities/src/Entity/OCCase.php | 34 --------------- 2 files changed, 42 insertions(+), 34 deletions(-) diff --git a/modules/opencase_defaults/opencase_defaults.module b/modules/opencase_defaults/opencase_defaults.module index 8211001..a3af7a4 100644 --- a/modules/opencase_defaults/opencase_defaults.module +++ b/modules/opencase_defaults/opencase_defaults.module @@ -89,4 +89,46 @@ function opencase_defaults_block_access(\Drupal\block\Entity\Block $block, $oper return AccessResult::neutral(); } +function opencase_defaults_entity_base_field_info($entity_type) { + $fields = array(); + + // Add a Client field to cases + if ($entity_type->id() === 'oc_case') { + $fields['actors_involved'] = BaseFieldDefinition::create('entity_reference') + ->setLabel(t('Involved Parties')) + ->setDescription(t('People involved in this case. To add one, start typing their name.')) + ->setSetting('target_type', 'oc_actor') + ->setSetting('handler', 'default') + ->setTranslatable(TRUE) + ->setCardinality(-1) + ->setDisplayOptions('form', [ + 'label' => 'above', + 'type' => 'entity_reference_autocomplete', + 'weight' => -2, + 'settings' => [ + 'match_operator' => 'CONTAINS', + 'size' => '60', + 'autocomplete_type' => 'tags', + 'placeholder' => '', + ], + ]) + ->setDisplayOptions('view', [ + 'label' => 'above', + ]) + ->setDefaultValueCallback('opencase_defaults_actors_involved_callback') + ->setRequired(TRUE); + + return $fields; +} + +/** + * When creating a case, it sets the first involved party to the actor + * id from the URL, and the second to the author's linked actor + * (if it exists and is different) + */ +function opencase_defaults_actors_involved_callback() { + $author_linked_actor_id = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id())->get('field_linked_opencase_actor')->target_id; + $currently_viewed_actor_id = \Drupal::request()->query->get('actor_id'); + return array_unique([$currently_viewed_actor_id, $author_linked_actor_id]); +} diff --git a/modules/opencase_entities/src/Entity/OCCase.php b/modules/opencase_entities/src/Entity/OCCase.php index 496a7d1..ebd5ee7 100644 --- a/modules/opencase_entities/src/Entity/OCCase.php +++ b/modules/opencase_entities/src/Entity/OCCase.php @@ -74,17 +74,6 @@ class OCCase extends RevisionableContentEntityBase implements OCCaseInterface { use EntityChangedTrait; - /** - * When creating a case, it sets the first involved party to the actor - * id from the URL, and the second to the author's linked actor - * (if it exists and is different) - */ - public static function defaultVal() { - $author_linked_actor_id = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id())->get('field_linked_opencase_actor')->target_id; - $currently_viewed_actor_id = \Drupal::request()->query->get('actor_id'); - return array_unique([$currently_viewed_actor_id, $author_linked_actor_id]); - } - /** * {@inheritdoc} */ @@ -250,29 +239,6 @@ class OCCase extends RevisionableContentEntityBase implements OCCaseInterface { ]) ->setRequired(TRUE); - $fields['actors_involved'] = BaseFieldDefinition::create('entity_reference') - ->setLabel(t('Involved Parties')) - ->setDescription(t('People involved in this case. To add one, start typing their name.')) - ->setSetting('target_type', 'oc_actor') - ->setSetting('handler', 'default') - ->setTranslatable(TRUE) - ->setCardinality(-1) - ->setDisplayOptions('form', [ - 'label' => 'above', - 'type' => 'entity_reference_autocomplete', - 'weight' => -2, - 'settings' => [ - 'match_operator' => 'CONTAINS', - 'size' => '60', - 'autocomplete_type' => 'tags', - 'placeholder' => '', - ], - ]) - ->setDisplayOptions('view', [ - 'label' => 'above', - ]) - ->setDefaultValueCallback('Drupal\opencase_entities\Entity\OCCase::defaultVal') - ->setRequired(TRUE); $fields['files'] = BaseFieldDefinition::create('file') ->setLabel(t('Files'))