2020-11-12 17:13:28 +00:00
< ? php
namespace Drupal\opencase_entities\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 Organisation entity .
*
* @ ingroup opencase_entities
*
* @ ContentEntityType (
* id = " oc_organisation " ,
* label = @ Translation ( " Organisation " ),
* bundle_label = @ Translation ( " Organisation type " ),
* handlers = {
* " storage " = " Drupal \ opencase_entities \ OCOrganisationStorage " ,
* " view_builder " = " Drupal \ Core \ Entity \ EntityViewBuilder " ,
* " list_builder " = " Drupal \ opencase_entities \ OCOrganisationListBuilder " ,
* " views_data " = " Drupal \ opencase_entities \ Entity \ OCOrganisationViewsData " ,
* " translation " = " Drupal \ opencase_entities \ OCOrganisationTranslationHandler " ,
*
* " form " = {
* " default " = " Drupal \ opencase_entities \ Form \ OCOrganisationForm " ,
* " add " = " Drupal \ opencase_entities \ Form \ OCOrganisationForm " ,
* " edit " = " Drupal \ opencase_entities \ Form \ OCOrganisationForm " ,
* " delete " = " Drupal \ opencase_entities \ Form \ OCOrganisationDeleteForm " ,
* },
* " route_provider " = {
* " html " = " Drupal \ opencase_entities \ OCOrganisationHtmlRouteProvider " ,
* },
* " access " = " Drupal \ opencase_entities \ OCOrganisationAccessControlHandler " ,
* },
* base_table = " oc_organisation " ,
* data_table = " oc_organisation_field_data " ,
* revision_table = " oc_organisation_revision " ,
* revision_data_table = " oc_organisation_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 "
* },
2020-11-12 17:13:28 +00:00
* translatable = TRUE ,
* permission_granularity = " bundle " ,
* admin_permission = " administer organisation entities " ,
* entity_keys = {
* " id " = " id " ,
* " revision " = " vid " ,
* " bundle " = " type " ,
* " label " = " name " ,
* " uuid " = " uuid " ,
* " uid " = " user_id " ,
* " langcode " = " langcode " ,
* " published " = " status " ,
* },
* links = {
2020-11-15 17:39:50 +00:00
* " canonical " = " /opencase/oc_organisation/ { oc_organisation} " ,
* " add-page " = " /opencase/oc_organisation/add " ,
* " add-form " = " /opencase/oc_organisation/add/ { oc_organisation_type} " ,
* " edit-form " = " /opencase/oc_organisation/ { oc_organisation}/edit " ,
* " delete-form " = " /opencase/oc_organisation/ { oc_organisation}/delete " ,
* " version-history " = " /opencase/oc_organisation/ { oc_organisation}/revisions " ,
* " revision " = " /opencase/oc_organisation/ { oc_organisation}/revisions/ { oc_organisation_revision}/view " ,
* " revision_revert " = " /opencase/oc_organisation/ { oc_organisation}/revisions/ { oc_organisation_revision}/revert " ,
* " revision_delete " = " /opencase/oc_organisation/ { oc_organisation}/revisions/ { oc_organisation_revision}/delete " ,
* " translation_revert " = " /opencase/oc_organisation/ { oc_organisation}/revisions/ { oc_organisation_revision}/revert/ { langcode} " ,
* " collection " = " /opencase/oc_organisation " ,
2020-11-12 17:13:28 +00:00
* },
* bundle_entity_type = " oc_organisation_type " ,
* field_ui_base_route = " entity.oc_organisation_type.edit_form "
* )
*/
class OCOrganisation extends EditorialContentEntityBase implements OCOrganisationInterface {
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_organisation 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 );
2020-11-12 17:35:34 +00:00
$fields [ 'notes' ] = BaseFieldDefinition :: create ( 'string_long' )
-> setRevisionable ( TRUE )
-> setLabel ( t ( 'Notes' ))
-> setSettings ( array (
'default_value' => '' ,
'max_length' => 255 ,
'text_processing' => 0 ,
))
-> setDisplayOptions ( 'view' , array (
'label' => 'above' ,
'type' => 'basic_string' ,
2020-11-15 17:39:50 +00:00
'weight' => 50 ,
2020-11-12 17:35:34 +00:00
))
-> setDisplayOptions ( 'form' , array (
'type' => 'string_textarea' ,
2020-11-15 17:39:50 +00:00
'weight' => 50 ,
2020-11-12 17:35:34 +00:00
));
2021-06-15 18:13:42 +00:00
$fields [ 'website' ] = BaseFieldDefinition :: create ( 'string' )
-> setLabel ( t ( 'Website' ))
-> setRevisionable ( TRUE )
-> setSettings ([
'max_length' => 50 ,
'text_processing' => 0 ,
])
-> setDefaultValue ( '' )
-> setDisplayOptions ( 'view' , [
'label' => 'above' ,
'type' => 'string' ,
'weight' => 49 ,
])
-> setDisplayOptions ( 'form' , [
'type' => 'string_textfield' ,
'weight' => 49 ,
])
-> setRequired ( FALSE );
2020-11-12 17:35:34 +00:00
$fields [ 'contact_name' ] = BaseFieldDefinition :: create ( 'string' )
-> setLabel ( t ( 'Contact Name' ))
2021-06-15 17:55:45 +00:00
-> setDescription ( t ( 'Name of the main contact for this organisation.' ))
2020-11-12 17:35:34 +00:00
-> setRevisionable ( TRUE )
-> setSettings ([
'max_length' => 50 ,
'text_processing' => 0 ,
])
-> setDefaultValue ( '' )
-> setDisplayOptions ( 'view' , [
'label' => 'above' ,
'type' => 'string' ,
2020-11-15 17:39:50 +00:00
'weight' => 51 ,
2020-11-12 17:35:34 +00:00
])
-> setDisplayOptions ( 'form' , [
'type' => 'string_textfield' ,
2020-11-15 17:39:50 +00:00
'weight' => 51 ,
2020-11-12 17:35:34 +00:00
])
-> setRequired ( FALSE );
2021-06-15 17:55:45 +00:00
$fields [ 'contact_role' ] = BaseFieldDefinition :: create ( 'string' )
-> setLabel ( t ( 'Contact Role' ))
-> setDescription ( t ( 'Role of the contact within the organisation' ))
-> setRevisionable ( TRUE )
-> setSettings ([
'max_length' => 50 ,
'text_processing' => 0 ,
])
-> setDefaultValue ( '' )
-> setDisplayOptions ( 'view' , [
'label' => 'above' ,
'type' => 'string' ,
'weight' => 52 ,
])
-> setDisplayOptions ( 'form' , [
'type' => 'string_textfield' ,
'weight' => 52 ,
])
-> setRequired ( FALSE );
2020-11-12 17:35:34 +00:00
$fields [ 'email' ] = BaseFieldDefinition :: create ( 'string' )
-> setLabel ( t ( 'Email Address' ))
-> setRevisionable ( TRUE )
-> setSettings ( array (
'default_value' => '' ,
2021-10-06 09:39:37 +00:00
'max_length' => 100 ,
2020-11-12 17:35:34 +00:00
'text_processing' => 0 ,
))
-> setDisplayOptions ( 'view' , array (
'label' => 'above' ,
'type' => 'string' ,
2020-11-15 17:39:50 +00:00
'weight' => 52 ,
2020-11-12 17:35:34 +00:00
))
-> setDisplayOptions ( 'form' , array (
'type' => 'string_textfield' ,
2020-11-15 17:39:50 +00:00
'weight' => 52 ,
2020-11-12 17:35:34 +00:00
));
$fields [ 'phone' ] = BaseFieldDefinition :: create ( 'string' )
-> setLabel ( t ( 'Phone Number' ))
-> setRevisionable ( TRUE )
-> setSettings ( array (
'default_value' => '' ,
'max_length' => 20 ,
'text_processing' => 0 ,
))
-> setDisplayOptions ( 'view' , array (
'label' => 'above' ,
'type' => 'string' ,
2020-11-15 17:39:50 +00:00
'weight' => 53 ,
2020-11-12 17:35:34 +00:00
))
-> setDisplayOptions ( 'form' , array (
'type' => 'string_textfield' ,
2020-11-15 17:39:50 +00:00
'weight' => 53 ,
2020-11-12 17:35:34 +00:00
));
$fields [ 'postal_address' ] = BaseFieldDefinition :: create ( 'string_long' )
-> setRevisionable ( TRUE )
-> setLabel ( t ( 'Postal Address' ))
-> setDescription ( t ( 'Full address, apart from post code.' ))
-> setSettings ( array (
'default_value' => '' ,
'max_length' => 255 ,
'text_processing' => 0 ,
))
-> setDisplayOptions ( 'view' , array (
'label' => 'above' ,
'type' => 'basic_string' ,
2020-11-15 17:39:50 +00:00
'weight' => 54 ,
2020-11-12 17:35:34 +00:00
))
-> setDisplayOptions ( 'form' , array (
'type' => 'string_textarea' ,
2020-11-15 17:39:50 +00:00
'weight' => 54 ,
2020-11-12 17:35:34 +00:00
));
$fields [ 'post_code' ] = BaseFieldDefinition :: create ( 'string' )
-> setRevisionable ( TRUE )
-> setLabel ( t ( 'Post Code' ))
-> setSettings ( array (
'default_value' => '' ,
'max_length' => 10 ,
'text_processing' => 0 ,
))
-> setDisplayOptions ( 'view' , array (
'label' => 'above' ,
'type' => 'string' ,
2020-11-15 17:39:50 +00:00
'weight' => 55 ,
2020-11-12 17:35:34 +00:00
))
-> setDisplayOptions ( 'form' , array (
'type' => 'string_textfield' ,
2020-11-15 17:39:50 +00:00
'weight' => 55 ,
2020-11-12 17:35:34 +00:00
));
$fields [ 'billing_contact_name' ] = BaseFieldDefinition :: create ( 'string' )
-> setLabel ( t ( 'Billing Contact Name' ))
-> setDescription ( t ( 'Name of the main contact for this client.' ))
-> setRevisionable ( TRUE )
-> setSettings ([
'max_length' => 50 ,
'text_processing' => 0 ,
])
-> setDefaultValue ( '' )
-> setDisplayOptions ( 'view' , [
'label' => 'above' ,
'type' => 'string' ,
2020-11-15 17:39:50 +00:00
'weight' => 56 ,
2020-11-12 17:35:34 +00:00
])
-> setDisplayOptions ( 'form' , [
'type' => 'string_textfield' ,
2020-11-15 17:39:50 +00:00
'weight' => 56 ,
2020-11-12 17:35:34 +00:00
])
-> setRequired ( FALSE );
$fields [ 'billing_email' ] = BaseFieldDefinition :: create ( 'string' )
-> setLabel ( t ( 'Billing Email Address' ))
-> setRevisionable ( TRUE )
-> setSettings ( array (
'default_value' => '' ,
2021-10-06 09:39:37 +00:00
'max_length' => 100 ,
2020-11-12 17:35:34 +00:00
'text_processing' => 0 ,
))
-> setDisplayOptions ( 'view' , array (
'label' => 'above' ,
'type' => 'string' ,
2020-11-15 17:39:50 +00:00
'weight' => 57 ,
2020-11-12 17:35:34 +00:00
))
-> setDisplayOptions ( 'form' , array (
'type' => 'string_textfield' ,
2020-11-15 17:39:50 +00:00
'weight' => 57 ,
2020-11-12 17:35:34 +00:00
));
$fields [ 'billing_phone' ] = BaseFieldDefinition :: create ( 'string' )
-> setLabel ( t ( 'Billing Phone Number' ))
-> setRevisionable ( TRUE )
-> setSettings ( array (
'default_value' => '' ,
'max_length' => 20 ,
'text_processing' => 0 ,
))
-> setDisplayOptions ( 'view' , array (
'label' => 'above' ,
'type' => 'string' ,
2020-11-15 17:39:50 +00:00
'weight' => 58 ,
2020-11-12 17:35:34 +00:00
))
-> setDisplayOptions ( 'form' , array (
'type' => 'string_textfield' ,
2020-11-15 17:39:50 +00:00
'weight' => 58 ,
2020-11-12 17:35:34 +00:00
));
$fields [ 'billing_postal_address' ] = BaseFieldDefinition :: create ( 'string_long' )
-> setRevisionable ( TRUE )
-> setLabel ( t ( 'Billing Postal Address' ))
-> setDescription ( t ( 'Full address, apart from post code.' ))
-> setSettings ( array (
'default_value' => '' ,
'max_length' => 255 ,
'text_processing' => 0 ,
))
-> setDisplayOptions ( 'view' , array (
'label' => 'above' ,
'type' => 'basic_string' ,
2020-11-15 17:39:50 +00:00
'weight' => 59 ,
2020-11-12 17:35:34 +00:00
))
-> setDisplayOptions ( 'form' , array (
'type' => 'string_textarea' ,
2020-11-15 17:39:50 +00:00
'weight' => 59 ,
2020-11-12 17:35:34 +00:00
));
$fields [ 'billing_post_code' ] = BaseFieldDefinition :: create ( 'string' )
-> setRevisionable ( TRUE )
-> setLabel ( t ( 'Billing Post Code' ))
-> setSettings ( array (
'default_value' => '' ,
'max_length' => 10 ,
'text_processing' => 0 ,
))
-> setDisplayOptions ( 'view' , array (
'label' => 'above' ,
'type' => 'string' ,
2020-11-15 17:39:50 +00:00
'weight' => 60 ,
2020-11-12 17:35:34 +00:00
))
-> setDisplayOptions ( 'form' , array (
'type' => 'string_textfield' ,
2020-11-15 17:39:50 +00:00
'weight' => 60 ,
2020-11-12 17:35:34 +00:00
));
2020-11-12 17:13:28 +00:00
$fields [ 'user_id' ] = BaseFieldDefinition :: create ( 'entity_reference' )
2020-11-15 17:39:50 +00:00
-> setLabel ( t ( 'Added by' ))
2020-11-12 17:13:28 +00:00
-> setRevisionable ( TRUE )
-> setSetting ( 'target_type' , 'user' )
-> setSetting ( 'handler' , 'default' )
2020-11-17 10:47:24 +00:00
-> setTranslatable ( TRUE );
2020-11-12 17:13:28 +00:00
$fields [ 'name' ] = BaseFieldDefinition :: create ( 'string' )
-> setLabel ( t ( 'Name' ))
-> setRevisionable ( TRUE )
-> setSettings ([
'max_length' => 50 ,
'text_processing' => 0 ,
])
-> setDefaultValue ( '' )
-> setDisplayOptions ( 'view' , [
2020-11-15 17:39:50 +00:00
'label' => 'hidden' ,
2020-11-12 17:13:28 +00:00
'type' => 'string' ,
2020-11-15 17:39:50 +00:00
'weight' => - 100 ,
2020-11-12 17:13:28 +00:00
])
-> setDisplayOptions ( 'form' , [
'type' => 'string_textfield' ,
2020-11-15 17:39:50 +00:00
'weight' => - 100 ,
2020-11-12 17:13:28 +00:00
])
-> setDisplayConfigurable ( 'form' , TRUE )
-> setDisplayConfigurable ( 'view' , TRUE )
-> setRequired ( TRUE );
2021-09-13 13:15:02 +00:00
$fields [ 'status' ] = BaseFieldDefinition :: create ( 'boolean' )
-> setLabel ( t ( 'Published' ))
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.' )
2021-09-13 13:15:02 +00:00
-> setRevisionable ( TRUE )
-> setDisplayConfigurable ( " form " , true )
-> setDefaultValue ( TRUE );
2020-11-12 17:13:28 +00:00
$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 );
2021-01-19 14:55:18 +00:00
$fields [ 'files' ] = BaseFieldDefinition :: create ( 'file' )
-> setLabel ( t ( 'Files' ))
-> setDescription ( t ( 'Files relating to this organisation' ))
-> 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' )
-> setSetting ( 'uri_scheme' , 'private' )
-> setCardinality ( - 1 )
-> setDisplayOptions ( 'form' , [
'type' => 'file_generic' ,
'weight' => 61 ,
'settings' => [
'progress_indicator' => 'throbber' ,
],
])
-> setDisplayOptions ( 'view' , [
'label' => 'above' ,
'settings' => [ 'use_description_as_link_text' => 'true' ]
]);
2020-11-12 17:13:28 +00:00
return $fields ;
}
}