EntityTest.php 4.11 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 33
 *     "translation" = "Drupal\content_translation\ContentTranslationHandler",
 *     "views_data" = "Drupal\views\EntityViewsData"
34 35 36
 *   },
 *   base_table = "entity_test",
 *   fieldable = TRUE,
37
 *   persistent_cache = FALSE,
38 39
 *   entity_keys = {
 *     "id" = "id",
40
 *     "uuid" = "uuid",
41
 *     "bundle" = "type",
42
 *     "label" = "name"
43
 *   },
44
 *   links = {
45 46 47
 *     "canonical" = "entity.entity_test.canonical",
 *     "edit-form" = "entity.entity_test.edit_form",
 *     "delete-form" = "entity.entity_test.delete_form",
48
 *   },
49
 *   field_ui_base_route = "entity.entity_test.admin_form",
50
 * )
51
 */
52
class EntityTest extends ContentEntityBase implements EntityOwnerInterface {
53

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

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

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

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

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

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

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

106 107
    return $fields;
  }
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 138
  /**
   * {@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;
  }

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
  /**
   * 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;
  }

161
}