View source
<?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;
class Relation extends ContentEntityBase implements RelationInterface {
public function getUser() {
return $this
->get('user_id')->entity;
}
public function getUserId() {
return $this
->get('user_id')->target_id;
}
public function setUserId($uid) {
$this
->set('user_id', $uid);
return $this;
}
public function setUser(UserInterface $account) {
$this
->set('user_id', $account
->id());
return $this;
}
public function getIndividual() {
return $this
->get('individual_id')->entity;
}
public function getIndividualId() {
return $this
->get('individual_id')->target_id;
}
public function setIndividualId($individual_id) {
$this
->set('individual_id', $individual_id);
return $this;
}
public function setIndividual(IndividualInterface $individual) {
$this
->set('individual_id', $individual
->id());
return $this;
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields['user_id'] = BaseFieldDefinition::create('entity_reference')
->setRequired(TRUE)
->setCardinality(1)
->addConstraint('UniqueReference')
->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)
->addConstraint('UniqueReference')
->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;
}
}