Commit d45326e6 authored by RoSk0's avatar RoSk0

Added relation entity.

parent b7694e4a
<?php
namespace Drupal\crm_core_user_sync\Entity;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\crm_core_contact\IndividualInterface;
use Drupal\crm_core_user_sync\RelationInterface;
use Drupal\user\UserInterface;
/**
* Defines the relation entity class.
*
* @ContentEntityType(
* id = "relation",
* label = @Translation("Relation"),
* label_collection = @Translation("Relations"),
* handlers = {
* "view_builder" = "Drupal\crm_core_user_sync\RelationViewBuilder",
* "list_builder" = "Drupal\crm_core_user_sync\RelationListBuilder",
* "views_data" = "Drupal\views\EntityViewsData",
* "form" = {
* "add" = "Drupal\crm_core_user_sync\Form\RelationForm",
* "edit" = "Drupal\crm_core_user_sync\Form\RelationForm",
* "delete" = "Drupal\Core\Entity\ContentEntityDeleteForm"
* },
* "route_provider" = {
* "html" = "Drupal\Core\Entity\Routing\AdminHtmlRouteProvider",
* }
* },
* base_table = "crm_core_user_sync_relation",
* data_table = "crm_core_user_sync_relation_field_data",
* admin_permission = "administer crm-core-user-sync",
* entity_keys = {
* "id" = "id",
* "uuid" = "uuid"
* },
* links = {
* "add-form" = "/admin/config/crm-core/user-sync/contact-to-user-management/add",
* "canonical" = "/admin/config/crm-core/user-sync/contact-to-user-management/{relation}",
* "edit-form" = "/admin/config/crm-core/user-sync/contact-to-user-management/{relation}/edit",
* "delete-form" = "/admin/config/crm-core/user-sync/contact-to-user-management/{relation}/delete",
* "collection" = "/admin/config/crm-core/user-sync/contact-to-user-management"
* }
* )
*/
class Relation extends ContentEntityBase implements RelationInterface {
/**
* {@inheritdoc}
*/
public function getUser() {
return $this->get('user_id')->entity;
}
/**
* {@inheritdoc}
*/
public function getUserId() {
return $this->get('user_id')->target_id;
}
/**
* {@inheritdoc}
*/
public function setUserId($uid) {
$this->set('user_id', $uid);
return $this;
}
/**
* {@inheritdoc}
*/
public function setUser(UserInterface $account) {
$this->set('user_id', $account->id());
return $this;
}
/**
* {@inheritdoc}
*/
public function getIndividual() {
return $this->get('individual_id')->entity;
}
/**
* {@inheritdoc}
*/
public function getIndividualId() {
return $this->get('individual_id')->target_id;
}
/**
* {@inheritdoc}
*/
public function setIndividualId($individual_id) {
$this->set('individual_id', $individual_id);
return $this;
}
/**
* {@inheritdoc}
*/
public function setIndividual(IndividualInterface $individual) {
$this->set('individual_id', $individual->id());
return $this;
}
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields['user_id'] = BaseFieldDefinition::create('entity_reference')
->setRequired(TRUE)
->setCardinality(1)
->setLabel(t('User'))
->setDescription(t('The user ID of the relation.'))
->setSetting('target_type', 'user')
->setDisplayOptions('form', [
'type' => 'entity_reference_autocomplete',
'settings' => [
'match_operator' => 'CONTAINS',
'size' => 60,
'placeholder' => '',
],
'weight' => 10,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'entity_reference_label',
'weight' => 10,
])
->setDisplayConfigurable('view', TRUE);
$fields['individual_id'] = BaseFieldDefinition::create('entity_reference')
->setRequired(TRUE)
->setCardinality(1)
->setLabel(t('Individual'))
->setDescription(t('The individual ID of the relation.'))
->setSetting('target_type', 'crm_core_individual')
->setDisplayOptions('form', [
'type' => 'entity_reference_autocomplete',
'settings' => [
'match_operator' => 'CONTAINS',
'size' => 60,
'placeholder' => '',
],
'weight' => 15,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'entity_reference_label',
'weight' => 15,
])
->setDisplayConfigurable('view', TRUE);
return $fields;
}
}
<?php
namespace Drupal\crm_core_user_sync;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\crm_core_contact\IndividualInterface;
use Drupal\user\UserInterface;
/**
* Provides an interface defining a relation entity type.
*/
interface RelationInterface extends ContentEntityInterface {
/**
* Returns the relation user entity.
*
* @return \Drupal\user\UserInterface
* The relation user entity.
*/
public function getUser();
/**
* Sets the relation user entity.
*
* @param \Drupal\user\UserInterface $account
* The relation user entity.
*
* @return $this
*/
public function setUser(UserInterface $account);
/**
* Returns the relation user ID.
*
* @return int|null
* The relation user ID, or NULL in case the user ID field has not been set.
*/
public function getUserId();
/**
* Sets the relation user ID.
*
* @param int $uid
* The relation user id.
*
* @return $this
*/
public function setUserId($uid);
/**
* Returns the relation individual entity.
*
* @return \Drupal\crm_core_contact\IndividualInterface
* The relation individual entity.
*/
public function getIndividual();
/**
* Sets the relation individual entity.
*
* @param \Drupal\crm_core_contact\IndividualInterface $individual
* The relation individual entity.
*
* @return $this
*/
public function setIndividual(IndividualInterface $individual);
/**
* Returns the relation individual ID.
*
* @return int|null
* The relation individual ID, or NULL in case the individual ID field has
* not been set.
*/
public function getIndividualId();
/**
* Sets the relation individual ID.
*
* @param int $individual_id
* The relation individual id.
*
* @return $this
*/
public function setIndividualId($individual_id);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment