Added target field to activity

This commit is contained in:
naomi 2022-04-03 14:37:40 +01:00
parent 60c64afcab
commit 225e92a51c
3 changed files with 57 additions and 21 deletions

View File

@ -4,6 +4,36 @@
use \Drupal\Core\Entity\Sql\SqlContentEntityStorageSchemaConverter; use \Drupal\Core\Entity\Sql\SqlContentEntityStorageSchemaConverter;
use \Drupal\Core\Field\BaseFieldDefinition; use \Drupal\Core\Field\BaseFieldDefinition;
function opencase_entities_update_90002() {
$field_storage_definition = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Target'))
->setRevisionable(TRUE)
->setSetting('target_type', 'oc_actor')
->setSetting('handler', 'default')
->setDefaultValueCallback('\Drupal\opencase_entities\Entity\OCActivity::defaultTarget')
->setDisplayConfigurable('form', true)
->setDisplayConfigurable('view', true);
\Drupal::entityDefinitionUpdateManager()
->installFieldStorageDefinition('oc_target', 'oc_activity', 'opencase_entities', $field_storage_definition);
}
function opencase_entities_update_90001() {
$storage_definition = \Drupal\Core\Field\BaseFieldDefinition::create('entity_reference')
->setLabel(t('Provider'))
->setRevisionable(TRUE)
->setSetting('target_type', 'oc_actor')
->setSetting('handler', 'default')
->setDefaultValueCallback('\Drupal\opencase_entities\Entity\OCActivity::loggedInActorId')
->setDisplayConfigurable('form', true)
->setDisplayConfigurable('view', true);
\Drupal::entityDefinitionUpdateManager()
->installFieldStorageDefinition('oc_provider', 'oc_activity', 'oc_activity', $storage_definition);
}
function opencase_entities_update_80202() { function opencase_entities_update_80202() {
\Drupal::entityTypeManager()->clearCachedDefinitions(); \Drupal::entityTypeManager()->clearCachedDefinitions();
\Drupal::entityDefinitionUpdateManager() \Drupal::entityDefinitionUpdateManager()
@ -151,15 +181,3 @@ function opencase_entities_update_8003() {
} }
function opencase_entities_update_90001() {
$storage_definition = \Drupal\Core\Field\BaseFieldDefinition::create('entity_reference')
->setLabel(t('Provider'))
->setRevisionable(TRUE)
->setSetting('target_type', 'oc_actor')
->setSetting('handler', 'default')
->setDefaultValueCallback('\Drupal\opencase_entities\Entity\OCActivity::loggedInActorId')
->setDisplayConfigurable('form', true)
->setDisplayConfigurable('view', true);
\Drupal::entityDefinitionUpdateManager()
->installFieldStorageDefinition('oc_provider', 'oc_activity', 'oc_activity', $storage_definition);
}

View File

@ -86,6 +86,14 @@ function opencase_entities_theme() {
return $theme; return $theme;
} }
/**
* Get the value of a url parameter and return it in a array
* so that it can be used as a default callback for an entity reference field.
*/
function opencase_entities_get($key) {
\Drupal::request()->query->get($key);
}
/** /**
* Implements hook_theme_suggestions_HOOK(). * Implements hook_theme_suggestions_HOOK().
*/ */

View File

@ -97,7 +97,14 @@ class OCActivity extends RevisionableContentEntityBase implements OCActivityInte
$linked_actor = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id())->field_linked_opencase_actor; $linked_actor = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id())->field_linked_opencase_actor;
if (empty($linked_actor)) return []; if (empty($linked_actor)) return [];
else return $linked_actor->target_id ; else return $linked_actor->target_id ;
} }
public static function defaultTarget() {
if (opencase_entities_get('target_id')) return [opencase_entities_get('target_id')];
else return [];
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
@ -245,6 +252,16 @@ class OCActivity extends RevisionableContentEntityBase implements OCActivityInte
->setDisplayConfigurable('form', true) ->setDisplayConfigurable('form', true)
->setDisplayConfigurable('view', true); ->setDisplayConfigurable('view', true);
$fields['oc_target'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Target'))
->setRevisionable(TRUE)
->setSetting('target_type', 'oc_actor')
->setSetting('handler', 'default')
->setDefaultValueCallback('\Drupal\opencase_entities\Entity\OCActivity::defaultTarget')
->setDisplayConfigurable('form', true)
->setDisplayConfigurable('view', true);
$fields['status'] = BaseFieldDefinition::create('boolean') $fields['status'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Visible')) ->setLabel(t('Visible'))
->setDescription('If this box is not ticked this record will be hidden from view for most users. Users with access to unpublished entities will be able to restore it if needed.') ->setDescription('If this box is not ticked this record will be hidden from view for most users. Users with access to unpublished entities will be able to restore it if needed.')
@ -294,15 +311,8 @@ class OCActivity extends RevisionableContentEntityBase implements OCActivityInte
'text_processing' => 0, 'text_processing' => 0,
]) ])
->setDefaultValue('') ->setDefaultValue('')
->setDisplayOptions('view', [ ->setDisplayConfigurable('form', true)
'label' => 'above', ->setDisplayConfigurable('view', true)
'type' => 'basic_string',
'weight' => -1,
])
->setDisplayOptions('form', [
'type' => 'string_textarea',
'weight' => -1,
])
->setRequired(FALSE); ->setRequired(FALSE);
$fields['time_taken'] = BaseFieldDefinition::create('decimal') $fields['time_taken'] = BaseFieldDefinition::create('decimal')