Creating an artist entity (Part 1)

Public

</> CopyGet raw version
php
  1. #artwork.info
  2.  
  3.  
  4. name = Artwork
  5. description = Artwork"
  6. core = 7.x
  7. package = Entities
  8. files[] = artwork.module
  9. files[] = artwork.pages.inc
  10. files[] = artwork.admin.inc
  11. files[] = artwork.install
  12. files[] = artwork.controller.inc
  13. dependencies[] = field_ui
  14.  
  15.  
  16. #artwork.install
  17.  
  18. <?php
  19.  
  20. function artwork_schema() {
  21. $schema['artwork'] = array(
  22. 'description' => 'The base table for artworks.',
  23. 'fields' => array(
  24. 'aid' => array(
  25. 'description' => 'The primary identifier for an artwork.',
  26. 'type' => 'serial',
  27. 'unsigned' => TRUE,
  28. 'not null' => TRUE,
  29. ),
  30. 'vid' => array(
  31. 'description' => 'The current {artwork_revision}.vid version identifier.',
  32. 'type' => 'int',
  33. 'unsigned' => TRUE,
  34. 'not null' => TRUE,
  35. 'default' => 0,
  36. ),
  37. 'type' => array(
  38. 'description' => 'The {artwork_type} of this artwork.',
  39. 'type' => 'varchar',
  40. 'length' => 32,
  41. 'not null' => TRUE,
  42. 'default' => '',
  43. ),
  44. 'title' => array(
  45. 'description' => 'The title of this artwork.',
  46. 'type' => 'varchar',
  47. 'length' => 255,
  48. 'not null' => TRUE,
  49. 'default' => '',
  50. ),
  51. 'created' => array(
  52. 'description' => 'The Unix timestamp when the artwork was created.',
  53. 'type' => 'int',
  54. 'not null' => TRUE,
  55. 'default' => 0,
  56. ),
  57. 'changed' => array(
  58. 'description' => 'The Unix timestamp when the artwork was most recently saved.',
  59. 'type' => 'int',
  60. 'not null' => TRUE,
  61. 'default' => 0,
  62. ),
  63.  
  64. ),
  65. 'unique keys' => array(
  66. 'aid_vid' => array('aid', 'vid'),
  67. 'aid' => array('aid')
  68. ),
  69. 'primary key' => array('aid'),
  70. );
  71.  
  72. $schema['artwork_revision'] = array(
  73. 'description' => 'Stores information about each saved version of an {artwork}.',
  74. 'fields' => array(
  75. 'aid' => array(
  76. 'description' => 'The {artwork} this version belongs to.',
  77. 'type' => 'int',
  78. 'unsigned' => TRUE,
  79. 'not null' => TRUE,
  80. 'default' => 0,
  81. ),
  82. 'vid' => array(
  83. 'description' => 'The primary identifier for this version.',
  84. 'type' => 'serial',
  85. 'unsigned' => TRUE,
  86. 'not null' => TRUE,
  87. ),
  88. 'title' => array(
  89. 'description' => 'The title of this version.',
  90. 'type' => 'varchar',
  91. 'length' => 255,
  92. 'not null' => TRUE,
  93. 'default' => '',
  94. ),
  95. 'created' => array(
  96. 'description' => 'The Unix timestamp when the artwork was created.',
  97. 'type' => 'int',
  98. 'not null' => TRUE,
  99. 'default' => 0,
  100. ),
  101. ),
  102. 'indexes' => array(
  103. 'aid' => array('aid'),
  104. ),
  105. 'primary key' => array('vid'),
  106. 'foreign keys' => array(
  107. 'artwork' => array(
  108. 'table' => 'artwork',
  109. 'columns' => array(
  110. 'aid' => 'aid',
  111. ),
  112. ),
  113. ),
  114. );
  115.  
  116. return $schema;
  117. }
  118.  
  119.  
  120. #artwork.controller.inc
  121.  
  122. <?php
  123.  
  124. /**
  125.  * Controller for loading, creating, and saving artworks.
  126.  *
  127.  * The default loader, which we extend, handles load() already. We only
  128.  * need to add saving and creating.
  129.  */
  130. class ArtworkController extends DrupalDefaultEntityController {
  131.  
  132. public function save($artwork) {
  133. $transaction = db_transaction();
  134.  
  135. try {
  136. global $user;
  137.  
  138. // Determine if we will be inserting a new artwork.
  139. $artwork->is_new = empty($artwork->aid);
  140.  
  141. // Set the timestamp fields.
  142. if (empty($artwork->created)) {
  143. $artwork->created = REQUEST_TIME;
  144. }
  145. $artwork->changed = REQUEST_TIME;
  146.  
  147. $artwork->revision_timestamp = REQUEST_TIME;
  148. $update_artwork = TRUE;
  149.  
  150. // Give modules the opportunity to prepare field data for saving.
  151. field_attach_presave('artwork', $artwork);
  152.  
  153. // When saving a new artwork revision, unset any existing $artwork->vid
  154. // to ensure a new revision will actually be created and store the old
  155. // revision ID in a separate property for artwork hook implementations.
  156. if (!$artwork->is_new && !empty($artwork->revision) && $artwork->vid) {
  157. $artwork->old_vid = $artwork->vid;
  158. unset($artwork->vid);
  159. }
  160.  
  161. // If this is a new artwork...
  162. if ($artwork->is_new) {
  163. // Save the new artwork.
  164. drupal_write_record('artwork', $artwork);
  165.  
  166. // Save the initial revision.
  167. $this->saveRevision($artwork, $user->uid);
  168.  
  169. $op = 'insert';
  170. }
  171. else {
  172. // Save the updated artwork.
  173. drupal_write_record('artwork', $artwork, 'aid');
  174.  
  175. // If a new artwork revision was requested, save a new record for that;
  176. // otherwise, update the artwork revision record that matches the value
  177. // of $artwork->vid.
  178. if (!empty($artwork->revision)) {
  179. $this->saveRevision($artwork, $user->uid);
  180. }
  181. else {
  182. $this->saveRevision($artwork, $user->uid, TRUE);
  183. $update_artwork = FALSE;
  184. }
  185.  
  186. $op = 'update';
  187. }
  188.  
  189. // If the revision ID is new or updated, save it to the artwork.
  190. if ($update_artwork) {
  191. db_update('artwork')
  192. ->fields(array('vid' => $artwork->vid))
  193. ->condition('aid', $artwork->aid)
  194. ->execute();
  195. }
  196.  
  197. // Save fields.
  198. $function = 'field_attach_' . $op;
  199. $function('artwork', $artwork);
  200.  
  201. module_invoke_all('entity_' . $op, $artwork, 'artwork');
  202.  
  203. // Clear internal properties.
  204. unset($artwork->is_new);
  205.  
  206. // Ignore slave server temporarily to give time for the saved order to be
  207. // propagated to the slave.
  208. db_ignore_slave();
  209.  
  210. return $artwork;
  211. }
  212. catch (Exception $e) {
  213. $transaction->rollback();
  214. watchdog_exception('artwork', $e, NULL, WATCHDOG_ERROR);
  215. return FALSE;
  216. }
  217. }
  218.  
  219. /**
  220.   * Saves an artwork revision with the uid of the current user.
  221.   *
  222.   * @param $artwork
  223.   * The fully loaded artwork object.
  224.   * @param $uid
  225.   * The user's uid for the current revision.
  226.   * @param $update
  227.   * TRUE or FALSE indicating whether or not the existing revision should be
  228.   * updated instead of a new one created.
  229.   */
  230. function saveRevision($artwork, $uid, $update = FALSE) {
  231. // Hold on to the artwork's original creator_uid but swap in the revision's
  232. // creator_uid for the momentary write.
  233. $temp_uid = $artwork->uid;
  234. $artwork->uid = $uid;
  235.  
  236. // Update the existing revision if specified.
  237. if ($update) {
  238. drupal_write_record('artwork_revision', $artwork, 'vid');
  239. }
  240. else {
  241. // Otherwise insert a new revision. This will automatically update $artwork
  242. // to include the vid.
  243. drupal_write_record('artwork_revision', $artwork);
  244. }
  245.  
  246. // Reset the order's creator_uid to the original value.
  247. $artwork->uid = $temp_uid;
  248. }
  249.  
  250. /**
  251.   * Deletes multiple artworks by ID.
  252.   *
  253.   * @param $aids
  254.   * An array of artwork IDs to delete.
  255.   * @return
  256.   * TRUE on success, FALSE otherwise.
  257.   */
  258. public function delete($aids) {
  259. if (!empty($aids)) {
  260. $artworks = $this->load($aids, array());
  261.  
  262. $transaction = db_transaction();
  263.  
  264. try {
  265. db_delete('artwork')
  266. ->condition('aid', $aids, 'IN')
  267. ->execute();
  268.  
  269. db_delete('artwork_revision')
  270. ->condition('aid', $aids, 'IN')
  271. ->execute();
  272.  
  273. foreach ($artworks as $artwork_id => $artwork) {
  274. field_attach_delete('artwork', $artwork);
  275. }
  276.  
  277. // Ignore slave server temporarily to give time for the
  278. // saved artwork to be propagated to the slave.
  279. db_ignore_slave();
  280. }
  281. catch (Exception $e) {
  282. $transaction->rollback();
  283. watchdog_exception('artwork', $e, NULL, WATCHDOG_ERROR);
  284. return FALSE;
  285. }
  286.  
  287. module_invoke_all('entity_delete', $artwork, 'artwork');
  288.  
  289. // Clear the page and block and artwork caches.
  290. cache_clear_all();
  291. $this->resetCache();
  292. }
  293.  
  294. return TRUE;
  295. }
  296.  
  297. /**
  298.   * Create a default artwork.
  299.   *
  300.   * @param $type
  301.   * The machine-readable type of the artwork.
  302.   *
  303.   * @return
  304.   * An artwork object with all default fields initialized.
  305.   */
  306. public function create($type = '') {
  307. return (object) array(
  308. 'aid' => '',
  309. 'type' => $type,
  310. 'title' => '',
  311. );
  312. }
  313. }

Comments

Why is there no info hook?'s picture

Shouldn't there be an info hook?