Guest User

Untitled

a guest
Feb 19th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. <?php
  2. /**
  3. * @file
  4. * Contains \Drupal\example\Entity\Example.
  5. */
  6.  
  7. namespace Drupal\example\Entity;
  8.  
  9. use Drupal\Core\Entity\EntityStorageInterface;
  10. use Drupal\Core\Field\BaseFieldDefinition;
  11. use Drupal\Core\Entity\ContentEntityBase;
  12. use Drupal\Core\Entity\EntityTypeInterface;
  13. use Drupal\example\ExampleInterface;
  14. use Drupal\user\UserInterface;
  15.  
  16. /**
  17. * Defines the Example entity.
  18. *
  19. * @ingroup example
  20. *
  21. * @ContentEntityType(
  22. * id = "example",
  23. * label = @Translation("Example entity"),
  24. * handlers = {
  25. * "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
  26. * "views_data" = "Drupal\views\EntityViewsData",
  27. * "list_builder" = "Drupal\example\Entity\Controller\ExampleListBuilder",
  28. * },
  29. * base_table = "examples",
  30. * admin_permission = "administer example entity",
  31. * fieldable = FALSE,
  32. * entity_keys = {
  33. * "id" = "id",
  34. * "label" = "title",
  35. * },
  36. * links = {
  37. * "collection" = "/example/list"
  38. * },
  39. * )
  40. */
  41. class Example extends ContentEntityBase implements ExampleInterface {
  42.  
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function getChangedTimeAcrossTranslations() {
  47. $changed = $this->getUntranslated()->getChangedTime();
  48. foreach ($this->getTranslationLanguages(FALSE) as $language) {
  49. $translation_changed = $this->getTranslation($language->getId())->getChangedTime();
  50. $changed = max($translation_changed, $changed);
  51. }
  52. return $changed;
  53. }
  54.  
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function getCreatedTime() {
  59. return $this->get('created')->value;
  60. }
  61.  
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function getChangedTime() {
  66. return $this->get('changed')->value;
  67. }
  68.  
  69. /**
  70. * {@inheritdoc}
  71. */
  72. public function setChangedTime($timestamp) {
  73. $this->set('changed', $timestamp);
  74. return $this;
  75. }
  76.  
  77. /**
  78. * {@inheritdoc}
  79. *
  80. * Define the field properties here.
  81. *
  82. * Field name, type and size determine the table structure.
  83. *
  84. * In addition, we can define how the field and its content can be manipulated
  85. * in the GUI. The behaviour of the widgets used can be determined here.
  86. */
  87. public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
  88.  
  89. // Some example fields.
  90. $fields['id'] = BaseFieldDefinition::create('integer')
  91. ->setLabel(t('ID'))
  92. ->setSettings([
  93. 'size' => 'big',
  94. ])
  95. ->setDescription(t('The ID of the Example entity.'))
  96. ->setReadOnly(TRUE);
  97.  
  98. $fields['uid'] = BaseFieldDefinition::create('integer')
  99. ->setLabel(t('Customer user id'))
  100. ->setDescription(t('Customer user id in origin'))
  101. ->setSettings(array(
  102. 'default_value' => null,
  103. ))
  104. ->setDisplayOptions('view', array(
  105. 'label' => 'above',
  106. 'type' => 'int',
  107. ))
  108. ->setDisplayConfigurable('view', TRUE)
  109. ->setReadOnly(TRUE);
  110.  
  111. $fields['title'] = BaseFieldDefinition::create('string')
  112. ->setLabel(t('Contact textfield'))
  113. ->setDescription(t('Example textfield property'))
  114. ->setSettings(array(
  115. 'default_value' => '',
  116. 'max_length' => 255,
  117. ))
  118. ->setDisplayOptions('view', array(
  119. 'label' => 'above',
  120. 'type' => 'string',
  121. ))
  122. ->setDisplayConfigurable('view', TRUE);
  123.  
  124.  
  125. //Meta fields
  126. $fields['created'] = BaseFieldDefinition::create('created')
  127. ->setLabel(t('Created'))
  128. ->setDescription(t('The time that the entity was created.'));
  129.  
  130. $fields['changed'] = BaseFieldDefinition::create('changed')
  131. ->setLabel(t('Changed'))
  132. ->setDescription(t('The time that the entity was last edited.'));
  133.  
  134. return $fields;
  135. }
  136. }
Add Comment
Please, Sign In to add comment