Status.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. namespace Drupal\statusmessage\Entity;
  3. use Drupal\Core\Entity\EntityStorageInterface;
  4. use Drupal\Core\Field\BaseFieldDefinition;
  5. use Drupal\Core\Entity\ContentEntityBase;
  6. use Drupal\Core\Entity\EntityChangedTrait;
  7. use Drupal\Core\Entity\EntityTypeInterface;
  8. use Drupal\statusmessage\StatusInterface;
  9. use Drupal\user\UserInterface;
  10. /**
  11. * Defines the Status entity.
  12. *
  13. * @ingroup statusmessage
  14. *
  15. * @ContentEntityType(
  16. * id = "status",
  17. * label = @Translation("Status"),
  18. * bundle_label = @Translation("Status type"),
  19. * handlers = {
  20. * "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
  21. * "list_builder" = "Drupal\statusmessage\StatusListBuilder",
  22. * "views_data" = "Drupal\statusmessage\Entity\StatusViewsData",
  23. *
  24. * "form" = {
  25. * "default" = "Drupal\statusmessage\Form\StatusForm",
  26. * "add" = "Drupal\statusmessage\Form\StatusForm",
  27. * "edit" = "Drupal\statusmessage\Form\StatusForm",
  28. * "delete" = "Drupal\statusmessage\Form\StatusDeleteForm",
  29. * },
  30. * "access" = "Drupal\statusmessage\StatusAccessControlHandler",
  31. * "route_provider" = {
  32. * "html" = "Drupal\statusmessage\StatusHtmlRouteProvider",
  33. * },
  34. * },
  35. * base_table = "status",
  36. * admin_permission = "administer status entities",
  37. * entity_keys = {
  38. * "id" = "id",
  39. * "bundle" = "type",
  40. * "label" = "name",
  41. * "uuid" = "uuid",
  42. * "uid" = "uid",
  43. * "langcode" = "langcode",
  44. * "status" = "status",
  45. * },
  46. * links = {
  47. * "canonical" = "/admin/structure/status/{status}",
  48. * "add-form" = "/admin/structure/status/add/{status_type}",
  49. * "edit-form" = "/admin/structure/status/{status}/edit",
  50. * "delete-form" = "/admin/structure/status/{status}/delete",
  51. * "collection" = "/admin/structure/status",
  52. * },
  53. * bundle_entity_type = "status_type",
  54. * field_ui_base_route = "entity.status_type.edit_form"
  55. * )
  56. */
  57. const TWEET_NODE_TYPE = 'tweet';
  58. class Status extends ContentEntityBase implements StatusInterface {
  59. use EntityChangedTrait;
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
  64. parent::preCreate($storage_controller, $values);
  65. $values += array(
  66. 'user_id' => \Drupal::currentUser()->id(),
  67. );
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. public function getType() {
  73. return $this->bundle();
  74. }
  75. /**
  76. * {@inheritdoc}
  77. */
  78. public function getName() {
  79. return $this->get('name')->value;
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. public function setName($name) {
  85. $this->set('name', $name);
  86. return $this;
  87. }
  88. /**
  89. * {@inheritdoc}
  90. */
  91. public function getCreatedTime() {
  92. return $this->get('created')->value;
  93. }
  94. /**
  95. * {@inheritdoc}
  96. */
  97. public function setCreatedTime($timestamp) {
  98. $this->set('created', $timestamp);
  99. return $this;
  100. }
  101. /**
  102. * {@inheritdoc}
  103. */
  104. public function getOwner() {
  105. return $this->get('user_id')->entity;
  106. }
  107. /**
  108. * {@inheritdoc}
  109. */
  110. public function getOwnerId() {
  111. return $this->get('user_id')->target_id;
  112. }
  113. /**
  114. * {@inheritdoc}
  115. */
  116. public function setOwnerId($uid) {
  117. $this->set('user_id', $uid);
  118. return $this;
  119. }
  120. /**
  121. * {@inheritdoc}
  122. */
  123. public function setOwner(UserInterface $account) {
  124. $this->set('user_id', $account->id());
  125. return $this;
  126. }
  127. /**
  128. * {@inheritdoc}
  129. */
  130. public function isPublished() {
  131. return (bool) $this->getEntityKey('status');
  132. }
  133. /**
  134. * {@inheritdoc}
  135. */
  136. public function setPublished($published) {
  137. $this->set('status', $published ? NODE_PUBLISHED : NODE_NOT_PUBLISHED);
  138. return $this;
  139. }
  140. /**
  141. * {@inheritdoc}
  142. */
  143. public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
  144. $fields['id'] = BaseFieldDefinition::create('integer')
  145. ->setLabel(t('ID'))
  146. ->setDescription(t('The ID of the Status entity.'))
  147. ->setReadOnly(TRUE);
  148. $fields['type'] = BaseFieldDefinition::create('entity_reference')
  149. ->setLabel(t('Type'))
  150. ->setDescription(t('The Status type/bundle.'))
  151. ->setSetting('target_type', 'status_type')
  152. ->setRequired(TRUE);
  153. $fields['uuid'] = BaseFieldDefinition::create('uuid')
  154. ->setLabel(t('UUID'))
  155. ->setDescription(t('The UUID of the Status entity.'))
  156. ->setReadOnly(TRUE);
  157. $fields['uid'] = BaseFieldDefinition::create('entity_reference')
  158. ->setLabel(t('Authored by'))
  159. ->setDescription(t('The user ID of author of the Status entity.'))
  160. ->setRevisionable(TRUE)
  161. ->setSetting('target_type', 'user')
  162. ->setSetting('handler', 'default')
  163. ->setDefaultValueCallback('Drupal\node\Entity\Node::getCurrentUserId')
  164. ->setTranslatable(TRUE)
  165. ->setDisplayOptions('view', array(
  166. 'label' => 'hidden',
  167. 'type' => 'author',
  168. 'weight' => 0,
  169. ))
  170. ->setDisplayOptions('form', array(
  171. 'type' => 'entity_reference_autocomplete',
  172. 'weight' => 5,
  173. 'settings' => array(
  174. 'match_operator' => 'CONTAINS',
  175. 'size' => '60',
  176. 'autocomplete_type' => 'tags',
  177. 'placeholder' => '',
  178. ),
  179. ))
  180. ->setDisplayConfigurable('form', TRUE)
  181. ->setDisplayConfigurable('view', TRUE);
  182. $fields['recipient'] = BaseFieldDefinition::create('entity_reference')
  183. ->setLabel(t('Received by'))
  184. ->setDescription(t('The user ID of recipient of the Status entity.'))
  185. ->setRevisionable(TRUE)
  186. ->setSetting('target_type', 'user')
  187. ->setSetting('handler', 'default')
  188. ->setTranslatable(TRUE)
  189. ->setDisplayOptions('view', array(
  190. 'label' => 'hidden',
  191. 'type' => 'author',
  192. 'weight' => 0,
  193. ))
  194. ->setDisplayOptions('form', array(
  195. 'type' => 'entity_reference_autocomplete',
  196. 'weight' => 5,
  197. 'settings' => array(
  198. 'match_operator' => 'CONTAINS',
  199. 'size' => '60',
  200. 'autocomplete_type' => 'tags',
  201. 'placeholder' => '',
  202. ),
  203. ))
  204. ->setDisplayConfigurable('form', TRUE)
  205. ->setDisplayConfigurable('view', TRUE);
  206. $fields['entity_target'] = BaseFieldDefinition::create('entity_reference')
  207. ->setLabel(t('Node'))
  208. ->setDescription(t('The content associated with this Status message'))
  209. ->setSetting('target_type', 'node')
  210. ->setSetting('handler', 'default')
  211. ->setRevisionable(TRUE);
  212. $fields['name'] = BaseFieldDefinition::create('string')
  213. ->setLabel(t('Name'))
  214. ->setDescription(t('The name of the Status entity.'))
  215. ->setSettings(array(
  216. 'max_length' => 50,
  217. 'text_processing' => 0,
  218. ))
  219. ->setDefaultValue('')
  220. ->setDisplayOptions('view', array(
  221. 'label' => 'above',
  222. 'type' => 'string',
  223. 'weight' => -4,
  224. ))
  225. ->setDisplayOptions('form', array(
  226. 'type' => 'string_textfield',
  227. 'weight' => -4,
  228. ))
  229. ->setDisplayConfigurable('form', TRUE)
  230. ->setDisplayConfigurable('view', TRUE);
  231. $fields['message'] = BaseFieldDefinition::create('string_long')
  232. ->setLabel(t('Message'))
  233. ->setDescription(t('The message of the Status entity.'))
  234. ->setRevisionable(TRUE);
  235. $fields['status'] = BaseFieldDefinition::create('boolean')
  236. ->setLabel(t('Publishing status'))
  237. ->setDescription(t('A boolean indicating whether the Status is published.'))
  238. ->setDefaultValue(TRUE);
  239. $fields['langcode'] = BaseFieldDefinition::create('language')
  240. ->setLabel(t('Language code'))
  241. ->setDescription(t('The language code for the Status entity.'))
  242. ->setDisplayOptions('form', array(
  243. 'type' => 'language_select',
  244. 'weight' => 10,
  245. ))
  246. ->setDisplayConfigurable('form', TRUE);
  247. $fields['created'] = BaseFieldDefinition::create('created')
  248. ->setLabel(t('Created'))
  249. ->setDescription(t('The time that the entity was created.'));
  250. $fields['changed'] = BaseFieldDefinition::create('changed')
  251. ->setLabel(t('Changed'))
  252. ->setDescription(t('The time that the entity was last edited.'));
  253. return $fields;
  254. }
  255. public function setMessage($message) {
  256. $this->set('message', $message);
  257. }
  258. public function getMessage() {
  259. return $this->get('message');
  260. }
  261. // public function setSender($sender) {
  262. // $this->set('sender', $sender);
  263. // }
  264. //
  265. // public function getSender() {
  266. // return $this->get('sender');
  267. // }
  268. public function setRecipient($recipient) {
  269. $this->set('recipient', $recipient);
  270. }
  271. public function getRecipient() {
  272. return $this->get('recipient');
  273. }
  274. public function setEntityTarget($entityTarget) {
  275. $this->set('entity_target', $entityTarget);
  276. }
  277. public function getEntityTarget() {
  278. return $this->get('entity_target');
  279. }
  280. }