2018-04-29 11:58:46 +00:00
< ? php
2021-02-18 12:02:25 +00:00
namespace Drupal\opencase_cases\Entity ;
2018-04-29 11:58:46 +00:00
use Drupal\Core\Entity\EntityStorageInterface ;
use Drupal\Core\Field\BaseFieldDefinition ;
use Drupal\Core\Entity\RevisionableContentEntityBase ;
use Drupal\Core\Entity\RevisionableInterface ;
use Drupal\Core\Entity\EntityChangedTrait ;
use Drupal\Core\Entity\EntityTypeInterface ;
use Drupal\user\UserInterface ;
/**
* Defines the Case entity .
*
2021-02-18 12:02:25 +00:00
* @ ingroup opencase_cases
2018-04-29 11:58:46 +00:00
*
* @ ContentEntityType (
* id = " oc_case " ,
* label = @ Translation ( " Case " ),
* bundle_label = @ Translation ( " Case type " ),
* handlers = {
2021-02-18 12:02:25 +00:00
* " storage " = " Drupal \ opencase_cases \ OCCaseStorage " ,
2018-04-29 11:58:46 +00:00
* " view_builder " = " Drupal \ Core \ Entity \ EntityViewBuilder " ,
2021-02-18 12:02:25 +00:00
* " list_builder " = " Drupal \ opencase_cases \ OCCaseListBuilder " ,
* " views_data " = " Drupal \ opencase_cases \ Entity \ OCCaseViewsData " ,
* " translation " = " Drupal \ opencase_cases \ OCCaseTranslationHandler " ,
2018-04-29 11:58:46 +00:00
*
* " form " = {
2021-02-18 12:02:25 +00:00
* " default " = " Drupal \ opencase_cases \ Form \ OCCaseForm " ,
* " add " = " Drupal \ opencase_cases \ Form \ OCCaseForm " ,
* " edit " = " Drupal \ opencase_cases \ Form \ OCCaseForm " ,
* " delete " = " Drupal \ opencase_cases \ Form \ OCCaseDeleteForm " ,
2018-04-29 11:58:46 +00:00
* },
2021-02-18 12:02:25 +00:00
* " access " = " Drupal \ opencase_cases \ OCCaseAccessControlHandler " ,
2018-04-29 11:58:46 +00:00
* " route_provider " = {
2021-02-18 12:02:25 +00:00
* " html " = " Drupal \ opencase_cases \ OCCaseHtmlRouteProvider " ,
2018-04-29 11:58:46 +00:00
* },
* },
* base_table = " oc_case " ,
* data_table = " oc_case_field_data " ,
* revision_table = " oc_case_revision " ,
* revision_data_table = " oc_case_field_revision " ,
2021-10-23 17:18:41 +00:00
* revision_metadata_keys = {
* " revision_user " = " revision_user " ,
* " revision_created " = " revision_created " ,
* " revision_log_message " = " revision_log "
* },
2018-04-29 11:58:46 +00:00
* translatable = TRUE ,
* admin_permission = " administer case entities " ,
* entity_keys = {
* " id " = " id " ,
* " revision " = " vid " ,
* " bundle " = " type " ,
* " label " = " name " ,
* " uuid " = " uuid " ,
* " uid " = " user_id " ,
* " langcode " = " langcode " ,
* " status " = " status " ,
* },
* links = {
2018-04-29 13:23:15 +00:00
* " canonical " = " /opencase/oc_case/ { oc_case} " ,
* " add-page " = " /opencase/oc_case/add " ,
* " add-form " = " /opencase/oc_case/add/ { oc_case_type} " ,
* " edit-form " = " /opencase/oc_case/ { oc_case}/edit " ,
* " delete-form " = " /opencase/oc_case/ { oc_case}/delete " ,
* " version-history " = " /opencase/oc_case/ { oc_case}/revisions " ,
* " revision " = " /opencase/oc_case/ { oc_case}/revisions/ { oc_case_revision}/view " ,
* " revision_revert " = " /opencase/oc_case/ { oc_case}/revisions/ { oc_case_revision}/revert " ,
* " revision_delete " = " /opencase/oc_case/ { oc_case}/revisions/ { oc_case_revision}/delete " ,
* " translation_revert " = " /opencase/oc_case/ { oc_case}/revisions/ { oc_case_revision}/revert/ { langcode} " ,
* " collection " = " /opencase/oc_case " ,
2018-04-29 11:58:46 +00:00
* },
* bundle_entity_type = " oc_case_type " ,
* field_ui_base_route = " entity.oc_case_type.edit_form "
* )
*/
class OCCase extends RevisionableContentEntityBase implements OCCaseInterface {
2018-04-30 15:14:51 +00:00
use EntityChangedTrait ;
2018-04-30 13:45:34 +00:00
2018-04-29 11:58:46 +00:00
/**
* { @ 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 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 function isPublished () {
return ( bool ) $this -> getEntityKey ( 'status' );
}
/**
* { @ inheritdoc }
*/
public function setPublished ( $published ) {
$this -> set ( 'status' , $published ? TRUE : FALSE );
return $this ;
}
/**
* { @ inheritdoc }
*/
public static function baseFieldDefinitions ( EntityTypeInterface $entity_type ) {
$fields = parent :: baseFieldDefinitions ( $entity_type );
2018-05-07 12:49:31 +00:00
// not currently used. Will add form and view settings when ready
$fields [ 'status' ] = BaseFieldDefinition :: create ( 'boolean' )
-> setLabel ( t ( 'Publishing status' ))
2021-09-13 13:31:12 +00:00
-> 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.' )
2018-05-07 12:49:31 +00:00
-> setRevisionable ( TRUE )
2021-09-13 13:15:02 +00:00
-> setDisplayConfigurable ( " form " , TRUE )
2018-05-07 12:49:31 +00:00
-> setDefaultValue ( TRUE );
2018-04-29 11:58:46 +00:00
$fields [ 'user_id' ] = BaseFieldDefinition :: create ( 'entity_reference' )
2020-05-13 14:41:59 +00:00
-> setLabel ( t ( 'Created by' ))
2018-04-29 11:58:46 +00:00
-> setDescription ( t ( 'The user ID of author of the Case entity.' ))
-> setRevisionable ( TRUE )
-> setSetting ( 'target_type' , 'user' )
-> setSetting ( 'handler' , 'default' )
-> setTranslatable ( TRUE )
2021-08-26 20:41:36 +00:00
-> setDisplayConfigurable ( 'form' , true )
-> setDisplayConfigurable ( 'view' , true )
2018-04-29 11:58:46 +00:00
-> setDisplayOptions ( 'view' , [
2020-05-13 14:41:59 +00:00
'label' => 'above' ,
2018-04-29 11:58:46 +00:00
'type' => 'author' ,
2020-11-20 17:36:51 +00:00
'weight' => 80 ,
2018-05-07 12:49:31 +00:00
]);
2018-04-29 11:58:46 +00:00
$fields [ 'name' ] = BaseFieldDefinition :: create ( 'string' )
2020-11-17 10:47:24 +00:00
-> setLabel ( t ( 'Title' ))
2018-04-29 11:58:46 +00:00
-> setRevisionable ( TRUE )
-> setSettings ([
'max_length' => 50 ,
'text_processing' => 0 ,
])
-> setDefaultValue ( '' )
-> setDisplayOptions ( 'view' , [
2018-05-03 12:11:04 +00:00
'label' => 'hidden' ,
2018-04-29 11:58:46 +00:00
'type' => 'string' ,
])
-> setDisplayOptions ( 'form' , [
'type' => 'string_textfield' ,
2020-11-20 14:19:22 +00:00
'weight' => - 100 ,
2018-04-29 11:58:46 +00:00
])
-> setRequired ( TRUE );
2018-04-30 10:59:07 +00:00
2018-05-07 13:04:01 +00:00
$fields [ 'files' ] = BaseFieldDefinition :: create ( 'file' )
-> setLabel ( t ( 'Files' ))
-> setDescription ( t ( 'Files attached to this case' ))
-> setSetting ( 'file_directory' , '[date:custom:Y]-[date:custom:m]' )
-> setSetting ( 'handler' , 'default:file' )
-> setSetting ( 'file_extensions' , 'txt jpg jpeg gif rtf xls xlsx doc swf png pdf docx csv' )
-> setSetting ( 'description_field' , 'true' )
2018-05-28 13:11:06 +00:00
-> setSetting ( 'uri_scheme' , 'private' )
2018-05-07 13:04:01 +00:00
-> setCardinality ( - 1 )
2021-08-26 20:41:36 +00:00
-> setDisplayConfigurable ( 'form' , true )
-> setDisplayConfigurable ( 'view' , true )
2018-05-07 13:04:01 +00:00
-> setDisplayOptions ( 'form' , [
'type' => 'file_generic' ,
'weight' => - 1 ,
'settings' => [
'progress_indicator' => 'throbber' ,
],
])
-> setDisplayOptions ( 'view' , [
'label' => 'above' ,
'settings' => [ 'use_description_as_link_text' => 'true' ]
]);
2018-04-29 11:58:46 +00:00
$fields [ 'created' ] = BaseFieldDefinition :: create ( 'created' )
2020-05-13 14:41:59 +00:00
-> setLabel ( t ( 'Created on' ))
-> setDescription ( t ( 'When the case was created.' ))
-> setDisplayOptions ( 'view' , [
'label' => 'above' ,
2020-11-20 17:36:51 +00:00
'weight' => 80 ,
2020-05-13 14:41:59 +00:00
]);
2018-04-29 11:58:46 +00:00
$fields [ 'changed' ] = BaseFieldDefinition :: create ( 'changed' )
2020-05-13 14:41:59 +00:00
-> setLabel ( t ( 'Last updated' ))
-> setDescription ( t ( 'When the case was last edited.' ))
-> setDisplayOptions ( 'view' , [
'label' => 'above' ,
2020-11-20 17:36:51 +00:00
'weight' => 80 ,
2020-05-13 14:41:59 +00:00
]);
2018-04-29 11:58:46 +00:00
$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 );
2021-11-11 15:27:25 +00:00
$fields [ 'notes' ] = BaseFieldDefinition :: create ( 'string_long' )
-> setRevisionable ( TRUE )
-> setLabel ( t ( 'Notes' ))
-> setSettings ( array (
'default_value' => '' ,
'max_length' => 255 ,
'text_processing' => 0 ,
))
-> setDisplayConfigurable ( 'form' , true )
-> setDisplayConfigurable ( 'view' , true );
2021-11-12 09:11:00 +00:00
$fields [ 'total_fee' ] = BaseFieldDefinition :: create ( 'decimal' )
-> setLabel ( t ( 'Total Fee' ))
-> setRevisionable ( TRUE )
-> setSettings ([
'prefix' => '£' ,
])
-> SetDisplayConfigurable ( " view " , true );
2018-04-29 11:58:46 +00:00
return $fields ;
}
}