Added Organisation Relation entity with update hook to install it, bumped version
This commit is contained in:
156
modules/opencase_entities/src/Entity/OCOrganisationRelation.php
Normal file
156
modules/opencase_entities/src/Entity/OCOrganisationRelation.php
Normal file
@ -0,0 +1,156 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\opencase_entities\Entity;
|
||||
|
||||
use Drupal\Core\Field\BaseFieldDefinition;
|
||||
use Drupal\Core\Entity\ContentEntityBase;
|
||||
use Drupal\Core\Entity\EntityChangedTrait;
|
||||
use Drupal\Core\Entity\EntityPublishedTrait;
|
||||
use Drupal\Core\Entity\EntityTypeInterface;
|
||||
|
||||
/**
|
||||
* Defines the Organisation Relation entity.
|
||||
*
|
||||
* @ingroup opencase_entities
|
||||
*
|
||||
* @ContentEntityType(
|
||||
* id = "oc_organisation_relation",
|
||||
* label = @Translation("Organisation Relation"),
|
||||
* bundle_label = @Translation("Organisation Relation type"),
|
||||
* handlers = {
|
||||
* "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
|
||||
* "list_builder" = "Drupal\opencase_entities\OCOrganisationRelationListBuilder",
|
||||
* "views_data" = "Drupal\opencase_entities\Entity\OCOrganisationRelationViewsData",
|
||||
*
|
||||
* "form" = {
|
||||
* "default" = "Drupal\opencase_entities\Form\OCOrganisationRelationForm",
|
||||
* "add" = "Drupal\opencase_entities\Form\OCOrganisationRelationForm",
|
||||
* "edit" = "Drupal\opencase_entities\Form\OCOrganisationRelationForm",
|
||||
* "delete" = "Drupal\opencase_entities\Form\OCOrganisationRelationDeleteForm",
|
||||
* },
|
||||
* "route_provider" = {
|
||||
* "html" = "Drupal\opencase_entities\OCOrganisationRelationHtmlRouteProvider",
|
||||
* },
|
||||
* "access" = "Drupal\opencase_entities\OCOrganisationRelationAccessControlHandler",
|
||||
* },
|
||||
* base_table = "oc_organisation_relation",
|
||||
* translatable = FALSE,
|
||||
* admin_permission = "administer organisation relation entities",
|
||||
* entity_keys = {
|
||||
* "id" = "id",
|
||||
* "bundle" = "type",
|
||||
* "label" = "name",
|
||||
* "uuid" = "uuid",
|
||||
* "langcode" = "langcode",
|
||||
* "published" = "status",
|
||||
* },
|
||||
* links = {
|
||||
* "canonical" = "/opencase/oc_organisation_relation/{oc_organisation_relation}",
|
||||
* "add-page" = "/opencase//oc_organisation_relation/add",
|
||||
* "add-form" = "/opencase/oc_organisation_relation/add/{oc_organisation_relation_type}",
|
||||
* "edit-form" = "/opencase/oc_organisation_relation/{oc_organisation_relation}/edit",
|
||||
* "delete-form" = "/opencase/oc_organisation_relation/{oc_organisation_relation}/delete",
|
||||
* "collection" = "/opencase/oc_organisation_relation",
|
||||
* },
|
||||
* bundle_entity_type = "oc_organisation_relation_type",
|
||||
* field_ui_base_route = "entity.oc_organisation_relation_type.edit_form"
|
||||
* )
|
||||
*/
|
||||
class OCOrganisationRelation extends ContentEntityBase implements OCOrganisationRelationInterface {
|
||||
|
||||
use EntityChangedTrait;
|
||||
use EntityPublishedTrait;
|
||||
|
||||
/**
|
||||
* {@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 static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
|
||||
$fields = parent::baseFieldDefinitions($entity_type);
|
||||
|
||||
// Add the published field.
|
||||
$fields += static::publishedBaseFieldDefinitions($entity_type);
|
||||
|
||||
$fields['name'] = BaseFieldDefinition::create('string')
|
||||
->setLabel(t('Name'))
|
||||
->setDescription(t('The name of the Organisation Relation entity.'))
|
||||
->setSettings([
|
||||
'max_length' => 50,
|
||||
'text_processing' => 0,
|
||||
])
|
||||
->setDefaultValue('Link between organisations')
|
||||
->setRequired(TRUE);
|
||||
|
||||
$fields['status']->setDescription(t('A boolean indicating whether the Organisation Relation 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['organisations'] = BaseFieldDefinition::create('entity_reference')
|
||||
->setLabel(t('Organisations'))
|
||||
->setDescription(t('Linked organisations'))
|
||||
->setSetting('target_type', 'oc_organisation')
|
||||
->setSetting('handler', 'default')
|
||||
->setCardinality(2)
|
||||
->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_entities_organisation_relation_callback')
|
||||
->setRequired(TRUE);
|
||||
return $fields;
|
||||
}
|
||||
}
|
||||
|
||||
function opencase_entities_organisation_relation_callback() {
|
||||
return [\Drupal::request()->query->get('organisation_id')];
|
||||
}
|
||||
|
Reference in New Issue
Block a user