Added Case Provision entity for recording caseworker involvement with cases

This commit is contained in:
2021-11-11 08:39:18 +00:00
parent 6dca2e98e9
commit f601890ad0
34 changed files with 2179 additions and 3 deletions

View File

@ -0,0 +1,301 @@
<?php
namespace Drupal\opencase_cases\Entity;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\EditorialContentEntityBase;
use Drupal\Core\Entity\RevisionableInterface;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityPublishedTrait;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\user\UserInterface;
/**
* Defines the Case Provision entity.
*
* @ingroup opencase_cases
*
* @ContentEntityType(
* id = "oc_case_provision",
* label = @Translation("Case Provision"),
* bundle_label = @Translation("Case Provision type"),
* handlers = {
* "storage" = "Drupal\opencase_cases\OCCaseProvisionStorage",
* "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
* "list_builder" = "Drupal\opencase_cases\OCCaseProvisionListBuilder",
* "views_data" = "Drupal\opencase_cases\Entity\OCCaseProvisionViewsData",
* "translation" = "Drupal\opencase_cases\OCCaseProvisionTranslationHandler",
*
* "form" = {
* "default" = "Drupal\opencase_cases\Form\OCCaseProvisionForm",
* "add" = "Drupal\opencase_cases\Form\OCCaseProvisionForm",
* "edit" = "Drupal\opencase_cases\Form\OCCaseProvisionForm",
* "delete" = "Drupal\opencase_cases\Form\OCCaseProvisionDeleteForm",
* },
* "route_provider" = {
* "html" = "Drupal\opencase_cases\OCCaseProvisionHtmlRouteProvider",
* },
* "access" = "Drupal\opencase_cases\OCCaseProvisionAccessControlHandler",
* },
* base_table = "oc_case_provision",
* data_table = "oc_case_provision_field_data",
* revision_table = "oc_case_provision_revision",
* revision_data_table = "oc_case_provision_field_revision",
* translatable = TRUE,
* permission_granularity = "bundle",
* admin_permission = "administer case provision entities",
* entity_keys = {
* "id" = "id",
* "revision" = "vid",
* "bundle" = "type",
* "label" = "name",
* "uuid" = "uuid",
* "uid" = "user_id",
* "langcode" = "langcode",
* "published" = "status",
* },
* links = {
* "canonical" = "/admin/opencase/oc_case_provision/{oc_case_provision}",
* "add-page" = "/admin/opencase/oc_case_provision/add",
* "add-form" = "/admin/opencase/oc_case_provision/add/{oc_case_provision_type}",
* "edit-form" = "/admin/opencase/oc_case_provision/{oc_case_provision}/edit",
* "delete-form" = "/admin/opencase/oc_case_provision/{oc_case_provision}/delete",
* "version-history" = "/admin/opencase/oc_case_provision/{oc_case_provision}/revisions",
* "revision" = "/admin/opencase/oc_case_provision/{oc_case_provision}/revisions/{oc_case_provision_revision}/view",
* "revision_revert" = "/admin/opencase/oc_case_provision/{oc_case_provision}/revisions/{oc_case_provision_revision}/revert",
* "revision_delete" = "/admin/opencase/oc_case_provision/{oc_case_provision}/revisions/{oc_case_provision_revision}/delete",
* "translation_revert" = "/admin/opencase/oc_case_provision/{oc_case_provision}/revisions/{oc_case_provision_revision}/revert/{langcode}",
* "collection" = "/admin/opencase/oc_case_provision",
* },
* bundle_entity_type = "oc_case_provision_type",
* field_ui_base_route = "entity.oc_case_provision_type.edit_form"
* )
*/
class OCCaseProvision extends EditorialContentEntityBase implements OCCaseProvisionInterface {
use EntityChangedTrait;
use EntityPublishedTrait;
/**
* {@inheritdoc}
*/
public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
parent::preCreate($storage_controller, $values);
$values += [
'user_id' => \Drupal::currentUser()->id(),
];
}
/**
* {@inheritdoc}
*/
protected function urlRouteParameters($rel) {
$uri_route_parameters = parent::urlRouteParameters($rel);
if ($rel === 'revision_revert' && $this instanceof RevisionableInterface) {
$uri_route_parameters[$this->getEntityTypeId() . '_revision'] = $this->getRevisionId();
}
elseif ($rel === 'revision_delete' && $this instanceof RevisionableInterface) {
$uri_route_parameters[$this->getEntityTypeId() . '_revision'] = $this->getRevisionId();
}
return $uri_route_parameters;
}
/**
* {@inheritdoc}
*/
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
foreach (array_keys($this->getTranslationLanguages()) as $langcode) {
$translation = $this->getTranslation($langcode);
// If no owner has been set explicitly, make the anonymous user the owner.
if (!$translation->getOwner()) {
$translation->setOwnerId(0);
}
}
// If no revision author has been set explicitly,
// make the oc_case_provision owner the revision author.
if (!$this->getRevisionUser()) {
$this->setRevisionUserId($this->getOwnerId());
}
}
/**
* {@inheritdoc}
*/
public function getName() {
return $this->get('name')->value;
}
/**
* {@inheritdoc}
*/
public function setName($name) {
$this->set('name', $name);
return $this;
}
/**
* {@inheritdoc}
*/
public function getCreatedTime() {
return $this->get('created')->value;
}
/**
* {@inheritdoc}
*/
public function setCreatedTime($timestamp) {
$this->set('created', $timestamp);
return $this;
}
/**
* {@inheritdoc}
*/
public function getOwner() {
return $this->get('user_id')->entity;
}
/**
* {@inheritdoc}
*/
public function getOwnerId() {
return $this->get('user_id')->target_id;
}
/**
* {@inheritdoc}
*/
public function setOwnerId($uid) {
$this->set('user_id', $uid);
return $this;
}
/**
* {@inheritdoc}
*/
public function setOwner(UserInterface $account) {
$this->set('user_id', $account->id());
return $this;
}
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
// Add the published field.
$fields += static::publishedBaseFieldDefinitions($entity_type);
$fields['user_id'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Authored by'))
->setDescription(t('The user ID of author of the Case Provision entity.'))
->setRevisionable(TRUE)
->setSetting('target_type', 'user')
->setSetting('handler', 'default')
->setTranslatable(TRUE)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'author',
'weight' => 0,
])
->setDisplayOptions('form', [
'type' => 'entity_reference_autocomplete',
'weight' => 5,
'settings' => [
'match_operator' => 'CONTAINS',
'size' => '60',
'autocomplete_type' => 'tags',
'placeholder' => '',
],
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['name'] = BaseFieldDefinition::create('string')
->setLabel(t('Name'))
->setDescription(t('The name of the Case Provision entity.'))
->setRevisionable(TRUE)
->setSettings([
'max_length' => 50,
'text_processing' => 0,
])
->setDefaultValue('')
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'string',
'weight' => -4,
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -4,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE)
->setRequired(TRUE);
$fields['status']->setDescription(t('A boolean indicating whether the Case Provision is published.'))
->setDisplayOptions('form', [
'type' => 'boolean_checkbox',
'weight' => -3,
]);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Created'))
->setDescription(t('The time that the entity was created.'));
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time that the entity was last edited.'));
$fields['revision_translation_affected'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Revision translation affected'))
->setDescription(t('Indicates if the last edit of a translation belongs to current revision.'))
->setReadOnly(TRUE)
->setRevisionable(TRUE)
->setTranslatable(TRUE);
$fields['oc_case'] = \Drupal\Core\Field\BaseFieldDefinition::create('entity_reference')
->setLabel(t('Case'))
->setSetting('target_type', 'oc_case')
->setSetting('handler', 'default')
->setTranslatable(TRUE)
->setCardinality(1)
->setDefaultValueCallback('opencase_cases_default_case_id') // defined in opencase_cases.module
->setDisplayOptions('view', [
'type' => 'string',
'weight' => 0,
])
->setRequired(TRUE);
$fields['oc_provider'] = \Drupal\Core\Field\BaseFieldDefinition::create('entity_reference')
->setLabel(t('Provider'))
->setSetting('target_type', 'oc_actor') // TODO: this should eventually point to a Provider rather than an Actor
->setSetting('handler', 'default')
->setTranslatable(TRUE)
->setCardinality(1)
->setDisplayConfigurable("view", true)
->setDisplayConfigurable("form", true)
->setRequired(TRUE);
$fields['oc_case_provider_role'] = \Drupal\Core\Field\BaseFieldDefinition::create('entity_reference')
->setLabel(t('Role'))
->setSetting('target_type', 'taxonomy_term')
->setSetting('handler_settings', ['target_bundles' => ['oc_case_provider_role' => 'oc_case_provider_role']])
->setSetting('handler', 'default')
->setTranslatable(TRUE)
->setCardinality(1)
->setDisplayConfigurable("view", true)
->setDisplayConfigurable("form", true)
->setRequired(FALSE);
return $fields;
}
}

View File

@ -0,0 +1,98 @@
<?php
namespace Drupal\opencase_cases\Entity;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\RevisionLogInterface;
use Drupal\Core\Entity\EntityChangedInterface;
use Drupal\Core\Entity\EntityPublishedInterface;
use Drupal\user\EntityOwnerInterface;
/**
* Provides an interface for defining Case Provision entities.
*
* @ingroup opencase_cases
*/
interface OCCaseProvisionInterface extends ContentEntityInterface, RevisionLogInterface, EntityChangedInterface, EntityPublishedInterface, EntityOwnerInterface {
/**
* Add get/set methods for your configuration properties here.
*/
/**
* Gets the Case Provision name.
*
* @return string
* Name of the Case Provision.
*/
public function getName();
/**
* Sets the Case Provision name.
*
* @param string $name
* The Case Provision name.
*
* @return \Drupal\opencase_cases\Entity\OCCaseProvisionInterface
* The called Case Provision entity.
*/
public function setName($name);
/**
* Gets the Case Provision creation timestamp.
*
* @return int
* Creation timestamp of the Case Provision.
*/
public function getCreatedTime();
/**
* Sets the Case Provision creation timestamp.
*
* @param int $timestamp
* The Case Provision creation timestamp.
*
* @return \Drupal\opencase_cases\Entity\OCCaseProvisionInterface
* The called Case Provision entity.
*/
public function setCreatedTime($timestamp);
/**
* Gets the Case Provision revision creation timestamp.
*
* @return int
* The UNIX timestamp of when this revision was created.
*/
public function getRevisionCreationTime();
/**
* Sets the Case Provision revision creation timestamp.
*
* @param int $timestamp
* The UNIX timestamp of when this revision was created.
*
* @return \Drupal\opencase_cases\Entity\OCCaseProvisionInterface
* The called Case Provision entity.
*/
public function setRevisionCreationTime($timestamp);
/**
* Gets the Case Provision revision author.
*
* @return \Drupal\user\UserInterface
* The user entity for the revision author.
*/
public function getRevisionUser();
/**
* Sets the Case Provision revision author.
*
* @param int $uid
* The user ID of the revision author.
*
* @return \Drupal\opencase_cases\Entity\OCCaseProvisionInterface
* The called Case Provision entity.
*/
public function setRevisionUserId($uid);
}

View File

@ -0,0 +1,58 @@
<?php
namespace Drupal\opencase_cases\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
/**
* Defines the Case Provision type entity.
*
* @ConfigEntityType(
* id = "oc_case_provision_type",
* label = @Translation("Case Provision type"),
* handlers = {
* "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
* "list_builder" = "Drupal\opencase_cases\OCCaseProvisionTypeListBuilder",
* "form" = {
* "add" = "Drupal\opencase_cases\Form\OCCaseProvisionTypeForm",
* "edit" = "Drupal\opencase_cases\Form\OCCaseProvisionTypeForm",
* "delete" = "Drupal\opencase_cases\Form\OCCaseProvisionTypeDeleteForm"
* },
* "route_provider" = {
* "html" = "Drupal\opencase_cases\OCCaseProvisionTypeHtmlRouteProvider",
* },
* },
* config_prefix = "oc_case_provision_type",
* admin_permission = "administer site configuration",
* bundle_of = "oc_case_provision",
* entity_keys = {
* "id" = "id",
* "label" = "label",
* "uuid" = "uuid"
* },
* links = {
* "canonical" = "/opencase/oc_case_provision_type/{oc_case_provision_type}",
* "add-form" = "/opencase/oc_case_provision_type/add",
* "edit-form" = "/opencase/oc_case_provision_type/{oc_case_provision_type}/edit",
* "delete-form" = "/opencase/oc_case_provision_type/{oc_case_provision_type}/delete",
* "collection" = "/opencase/oc_case_provision_type"
* }
* )
*/
class OCCaseProvisionType extends ConfigEntityBundleBase implements OCCaseProvisionTypeInterface {
/**
* The Case Provision type ID.
*
* @var string
*/
protected $id;
/**
* The Case Provision type label.
*
* @var string
*/
protected $label;
}

View File

@ -0,0 +1,13 @@
<?php
namespace Drupal\opencase_cases\Entity;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
/**
* Provides an interface for defining Case Provision type entities.
*/
interface OCCaseProvisionTypeInterface extends ConfigEntityInterface {
// Add get/set methods for your configuration properties here.
}

View File

@ -0,0 +1,23 @@
<?php
namespace Drupal\opencase_cases\Entity;
use Drupal\views\EntityViewsData;
/**
* Provides Views data for Case Provision entities.
*/
class OCCaseProvisionViewsData extends EntityViewsData {
/**
* {@inheritdoc}
*/
public function getViewsData() {
$data = parent::getViewsData();
// Additional information for Views integration, such as table joins, can be
// put here.
return $data;
}
}