Relation.php 4.87 KB
Newer Older
RoSk0's avatar
RoSk0 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?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(
RoSk0's avatar
RoSk0 committed
16
 *   id = "crm_core_user_sync_relation",
RoSk0's avatar
RoSk0 committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
 *   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 = {
40 41 42 43 44
 *     "add-form" = "/admin/config/crm-core/user-sync/relation/add",
 *     "canonical" = "/admin/config/crm-core/user-sync/relation/{crm_core_user_sync_relation}",
 *     "edit-form" = "/admin/config/crm-core/user-sync/relation/{crm_core_user_sync_relation}/edit",
 *     "delete-form" = "/admin/config/crm-core/user-sync/relation/{crm_core_user_sync_relation}/delete",
 *     "collection" = "/admin/config/crm-core/user-sync/relation"
RoSk0's avatar
RoSk0 committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
 *   }
 * )
 */
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)
120 121 122
      // @todo Replace with what ever would work with entity reference
      // from core. https://www.drupal.org/project/drupal/issues/2973455
      ->addConstraint('UniqueReference')
RoSk0's avatar
RoSk0 committed
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
      ->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)
146 147 148
      // @todo Replace with what ever would work with entity reference
      // from core. https://www.drupal.org/project/drupal/issues/2973455
      ->addConstraint('UniqueReference')
RoSk0's avatar
RoSk0 committed
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
      ->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;
  }

}