EntityTest.php 4.05 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\entity_test\Entity\EntityTest.
6 7
 */

8
namespace Drupal\entity_test\Entity;
9

10
use Drupal\Core\Entity\ContentEntityBase;
11
use Drupal\Core\Entity\EntityTypeInterface;
12
use Drupal\Core\Field\BaseFieldDefinition;
13
use Drupal\Core\Entity\EntityStorageInterface;
14 15
use Drupal\user\EntityOwnerInterface;
use Drupal\user\UserInterface;
16 17 18

/**
 * Defines the test entity class.
19
 *
20
 * @ContentEntityType(
21 22
 *   id = "entity_test",
 *   label = @Translation("Test entity"),
23
 *   handlers = {
24
 *     "list_builder" = "Drupal\entity_test\EntityTestListBuilder",
25
 *     "view_builder" = "Drupal\entity_test\EntityTestViewBuilder",
26
 *     "views_data" = "Drupal\entity_test\EntityTestViewsData",
27
 *     "access" = "Drupal\entity_test\EntityTestAccessControlHandler",
28
 *     "form" = {
29 30
 *       "default" = "Drupal\entity_test\EntityTestForm",
 *       "delete" = "Drupal\entity_test\EntityTestDeleteForm"
31
 *     },
32
 *     "translation" = "Drupal\content_translation\ContentTranslationHandler"
33 34 35
 *   },
 *   base_table = "entity_test",
 *   fieldable = TRUE,
36
 *   persistent_cache = FALSE,
37 38
 *   entity_keys = {
 *     "id" = "id",
39
 *     "uuid" = "uuid",
40
 *     "bundle" = "type",
41
 *     "label" = "name"
42
 *   },
43
 *   links = {
44 45 46
 *     "canonical" = "entity.entity_test.canonical",
 *     "edit-form" = "entity.entity_test.edit_form",
 *     "delete-form" = "entity.entity_test.delete_form",
47
 *   },
48
 *   field_ui_base_route = "entity.entity_test.admin_form",
49
 * )
50
 */
51
class EntityTest extends ContentEntityBase implements EntityOwnerInterface {
52

53 54 55
  /**
   * {@inheritdoc}
   */
56 57
  public static function preCreate(EntityStorageInterface $storage, array &$values) {
    parent::preCreate($storage, $values);
58
    if (empty($values['type'])) {
59
      $values['type'] = $storage->getEntityTypeId();
60 61 62
    }
  }

63 64 65
  /**
   * {@inheritdoc}
   */
66
  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
67
    $fields['id'] = BaseFieldDefinition::create('integer')
68 69
      ->setLabel(t('ID'))
      ->setDescription(t('The ID of the test entity.'))
70 71
      ->setReadOnly(TRUE)
      ->setSetting('unsigned', TRUE);
72

73
    $fields['uuid'] = BaseFieldDefinition::create('uuid')
74 75 76 77
      ->setLabel(t('UUID'))
      ->setDescription(t('The UUID of the test entity.'))
      ->setReadOnly(TRUE);

78
    $fields['langcode'] = BaseFieldDefinition::create('language')
79 80 81
      ->setLabel(t('Language code'))
      ->setDescription(t('The language code of the test entity.'));

82
    $fields['name'] = BaseFieldDefinition::create('string')
83 84 85
      ->setLabel(t('Name'))
      ->setDescription(t('The name of the test entity.'))
      ->setTranslatable(TRUE)
86 87 88 89 90 91
      ->setSetting('max_length', 32)
      ->setDisplayOptions('view', array(
        'label' => 'hidden',
        'type' => 'string',
        'weight' => -5,
      ));
92 93

    // @todo: Add allowed values validation.
94
    $fields['type'] = BaseFieldDefinition::create('string')
95 96 97 98
      ->setLabel(t('Type'))
      ->setDescription(t('The bundle of the test entity.'))
      ->setRequired(TRUE);

99
    $fields['user_id'] = BaseFieldDefinition::create('entity_reference')
100 101
      ->setLabel(t('User ID'))
      ->setDescription(t('The ID of the associated user.'))
102 103
      ->setSettings(array('target_type' => 'user'))
      ->setTranslatable(TRUE);
104

105 106
    return $fields;
  }
107

108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
  /**
   * {@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;
  }

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
  /**
   * Sets the name.
   *
   * @param string $name
   *   Name of the entity.
   *
   * @return $this
   */
  public function setName($name) {
    $this->set('name', $name);
    return $this;
  }

  /**
   * Returns the name.
   *
   * @return string
   */
  public function getName() {
    return $this->get('name')->value;
  }

160
}