tripal_entities.module 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. <?php
  2. require_once "api/tripal_entities.api.inc";
  3. require_once "includes/tripal_entities.entity_form.inc";
  4. require_once "includes/TripalVocab.inc";
  5. require_once "includes/TripalVocabController.inc";
  6. require_once "includes/TripalTerm.inc";
  7. require_once "includes/TripalTermController.inc";
  8. require_once "includes/TripalEntity.inc";
  9. require_once "includes/TripalEntityController.inc";
  10. require_once "includes/TripalEntityUIController.inc";
  11. require_once "includes/TripalBundle.inc";
  12. require_once "includes/TripalBundleController.inc";
  13. require_once "includes/TripalBundleUIController.inc";
  14. /**
  15. * Implements hook_views_api().
  16. */
  17. function tripal_entities_views_api() {
  18. return array(
  19. 'api' => 3,
  20. );
  21. }
  22. /**
  23. * Implements hook_menu().
  24. */
  25. function tripal_entities_menu() {
  26. // Note: menu items for the entities can be found in the
  27. // Entity's UI Controller class.
  28. $items = array();
  29. return $items;
  30. }
  31. /**
  32. * Implements hook_admin_paths().
  33. * Define administrative paths.
  34. */
  35. function tripal_entities_admin_paths() {
  36. if (variable_get('node_admin_theme')) {
  37. $paths = array(
  38. 'bio-data/*/edit' => TRUE,
  39. 'bio-data/*/delete' => TRUE,
  40. 'bio-data/add' => TRUE,
  41. 'bio-data/add/*' => TRUE,
  42. );
  43. return $paths;
  44. }
  45. }
  46. /**
  47. * Implements hook_menu_local_tasks_alter().
  48. *
  49. * Used to add action links to pages.
  50. */
  51. function tripal_entities_menu_local_tasks_alter(&$data, $router_item, $root_path) {
  52. // Add an "Add Tripal Content" action link to the Admin >> Content >>
  53. // Biological Content page.
  54. if ($root_path == 'admin/content/bio-data') {
  55. $item = menu_get_item('bio-data/add');
  56. if ($item['access']) {
  57. $data['actions']['output'][] = array(
  58. '#theme' => 'menu_local_action',
  59. '#link' => $item,
  60. );
  61. }
  62. }
  63. }
  64. /**
  65. * Implements hook_shortcut_default_set().
  66. * Modify the shortcut menu to include Biological content links.
  67. *
  68. * @param object $account
  69. * The user account whose default shortcut set will be returned. If not provided, the
  70. * function will return the currently logged-in user's default shortcut set.
  71. *
  72. * @return
  73. * An object representing the default shortcut set.
  74. */
  75. function tripal_entities_shortcut_default_set($account) {
  76. $sets = shortcut_sets();
  77. $found = FALSE;
  78. foreach ($sets as $set) {
  79. if ($set->title == 'TripalDefault') {
  80. $found = TRUE;
  81. }
  82. }
  83. if (!$found) {
  84. $t = get_t();
  85. // Create an initial default shortcut set.
  86. $shortcut_set = new stdClass();
  87. $shortcut_set->title = $t('TripalDefault');
  88. $shortcut_set->links = array(
  89. array(
  90. 'link_path' => 'node/add',
  91. 'link_title' => $t('Add content'),
  92. 'weight' => -35,
  93. ),
  94. array(
  95. 'link_path' => 'bio-data/add',
  96. 'link_title' => 'Add Tripal Content',
  97. 'weight' => -30,
  98. ),
  99. array(
  100. 'link_path' => 'admin/content',
  101. 'link_title' => $t('Find content'),
  102. 'weight' => -25,
  103. ),
  104. array(
  105. 'link_path' => 'admin/content/bio-data',
  106. 'link_title' => 'Find Tripal Content',
  107. 'weight' => -20,
  108. ),
  109. );
  110. shortcut_set_save($shortcut_set);
  111. }
  112. $sets = shortcut_sets();
  113. foreach ($sets as $set) {
  114. if ($set->title == 'TripalDefault') {
  115. return $set->set_name;
  116. }
  117. }
  118. }
  119. /**
  120. * Implements hook_permission().
  121. */
  122. function tripal_entities_permission() {
  123. // We set up permisssions to manage entity types, manage all entities and the
  124. // permissions for each individual entity
  125. $permissions = array(
  126. 'administer tripal data types' => array(
  127. 'title' => t('Administer Tripal data types'),
  128. 'description' => t('Create and delete fields for Tripal data types, and set their permissions.'),
  129. ),
  130. 'administer tripal data' => array(
  131. 'title' => t('Administer Tripal data'),
  132. 'description' => t('Edit and delete all tripal data'),
  133. ),
  134. );
  135. return $permissions;
  136. }
  137. /**
  138. * Implements hook_theme().
  139. */
  140. function tripal_entities_theme($existing, $type, $theme, $path) {
  141. return array(
  142. 'tripal_entity' => array(
  143. 'render element' => 'elements',
  144. 'template' => 'tripal_entity',
  145. 'path' => "$path/theme/templates"
  146. ),
  147. 'tripal_entities_add_list' => array(
  148. 'variables' => array('content' => NULL),
  149. 'file' => 'includes/tripal_entities.entity_form.inc',
  150. ),
  151. );
  152. }
  153. /**
  154. * https://api.drupal.org/api/drupal/modules!rdf!rdf.module/group/rdf/7
  155. */
  156. function tripal_entities_rdf_mapping() {
  157. return array();
  158. /* return array(
  159. 'type' => 'tripal_entity',
  160. 'bundle' => 'gene',
  161. 'mapping' => array(
  162. 'rdftype' => array('sioc:Item', 'foaf:Document'),
  163. 'title' => array(
  164. 'predicates' => array('dc:title'),
  165. ),
  166. 'uid' => array(
  167. 'predicates' => array('sioc:has_creator'),
  168. 'type' => 'rel',
  169. ),
  170. 'name' => array(
  171. 'predicates' => array('foaf:name'),
  172. ),
  173. 'uniquename' => array(
  174. 'predicates' => array('foaf:name'),
  175. ),
  176. 'organism_id' => array(
  177. 'predicates' => array('sioc:has_parent'),
  178. 'type' => 'rel'
  179. )
  180. ),
  181. ); */
  182. }
  183. // http://www.bluespark.com/blog/drupal-entities-part-3-programming-hello-drupal-entity
  184. // http://dikini.net/31.08.2010/entities_bundles_fields_and_field_instances
  185. /**
  186. * Implement hook_entity_info().
  187. */
  188. function tripal_entities_entity_info() {
  189. $entities = array();
  190. //
  191. // The TripalVocab entity is meant to house vocabularies. It is these
  192. // vocabs that are used by the TripalTerm entities. The storage backend
  193. // is responsible for setting the values of this entity.
  194. //
  195. $entities['TripalVocab'] = array(
  196. // A human readable label to identify our entity.
  197. 'label' => 'Controlled Vocabulary',
  198. 'plural label' => 'Controlled Vocabularies',
  199. // The entity class and controller class extend the classes provided by the
  200. // Entity API.
  201. 'entity class' => 'TripalVocab',
  202. 'controller class' => 'TripalVocabController',
  203. // The table for this entity defined in hook_schema()
  204. 'base table' => 'tripal_vocabulary',
  205. // If fieldable == FALSE, we can't attach fields.
  206. 'fieldable' => TRUE,
  207. // entity_keys tells the controller what database fields are used for key
  208. // functions. It is not required if we don't have bundles or revisions.
  209. // Here we do not support a revision, so that entity key is omitted.
  210. 'entity keys' => array (
  211. 'id' => 'id',
  212. ),
  213. // Callback function for access to this entity.
  214. 'access callback' => 'tripal_entity_access',
  215. // FALSE disables caching. Caching functionality is handled by Drupal core.
  216. 'static cache' => FALSE,
  217. // This entity doesn't support bundles.
  218. 'bundles' => array (),
  219. 'view modes' => array (
  220. 'full' => array (
  221. 'label' => t ('Full content'),
  222. 'custom settings' => FALSE
  223. ),
  224. 'teaser' => array (
  225. 'label' => t ('Teaser'),
  226. 'custom settings' => TRUE
  227. ),
  228. ),
  229. );
  230. //
  231. // The TripalTerm entity is meant to house vocabulary terms. It is these
  232. // terms that are used by the TripalEntity entities. The storage backend
  233. // is responsible for setting the values of this entity.
  234. //
  235. $entities['TripalTerm'] = array(
  236. // A human readable label to identify our entity.
  237. 'label' => 'Controlled Vocabulary Term',
  238. 'plural label' => 'Controlled Vocabulary Terms',
  239. // The entity class and controller class extend the classes provided by the
  240. // Entity API.
  241. 'entity class' => 'TripalTerm',
  242. 'controller class' => 'TripalTermController',
  243. // The table for this entity defined in hook_schema()
  244. 'base table' => 'tripal_term',
  245. // If fieldable == FALSE, we can't attach fields.
  246. 'fieldable' => TRUE,
  247. // entity_keys tells the controller what database fields are used for key
  248. // functions. It is not required if we don't have bundles or revisions.
  249. // Here we do not support a revision, so that entity key is omitted.
  250. 'entity keys' => array (
  251. 'id' => 'id',
  252. ),
  253. // Callback function for access to this entity.
  254. 'access callback' => 'tripal_entity_access',
  255. // FALSE disables caching. Caching functionality is handled by Drupal core.
  256. 'static cache' => FALSE,
  257. // This entity doesn't support bundles.
  258. 'bundles' => array (),
  259. 'view modes' => array (
  260. 'full' => array (
  261. 'label' => t ('Full content'),
  262. 'custom settings' => FALSE
  263. ),
  264. 'teaser' => array (
  265. 'label' => t ('Teaser'),
  266. 'custom settings' => TRUE
  267. ),
  268. ),
  269. );
  270. //
  271. // The TripalEntity is used for all data. It links data from a storage
  272. // back-end to a TripalTerm entity.
  273. //
  274. $entities['TripalEntity'] = array (
  275. // A human readable label to identify our entity.
  276. 'label' => 'Tripal Content',
  277. 'plural label' => 'Tripal Content',
  278. // The entity class and controller class extend the classes provided by the
  279. // Entity API.
  280. 'entity class' => 'TripalEntity',
  281. 'controller class' => 'TripalEntityController',
  282. // The table for this entity defined in hook_schema()
  283. 'base table' => 'tripal_entity',
  284. // Returns the uri elements of an entity.
  285. 'uri callback' => 'tripal_entities_vocbulary_term_uri',
  286. // IF fieldable == FALSE, we can't attach fields.
  287. 'fieldable' => TRUE,
  288. // entity_keys tells the controller what database fields are used for key
  289. // functions. It is not required if we don't have bundles or revisions.
  290. // Here we do not support a revision, so that entity key is omitted.
  291. 'entity keys' => array (
  292. 'id' => 'id',
  293. 'bundle' => 'bundle'
  294. ),
  295. 'bundle keys' => array (
  296. 'bundle' => 'bundle'
  297. ),
  298. // Callback function for access to this entity.
  299. 'access callback' => 'tripal_entity_access',
  300. // FALSE disables caching. Caching functionality is handled by Drupal core.
  301. 'static cache' => FALSE,
  302. // Bundles are added dynamically below.
  303. 'bundles' => array (),
  304. 'label callback' => 'tripal_entity_label',
  305. // The information below is used by the TripalEntityUIController
  306. // (which extends the EntityDefaultUIController). The admin_ui
  307. // key here is mean to appear on the 'Find Content' page of the
  308. // administrative menu.
  309. 'admin ui' => array (
  310. 'path' => 'admin/content/bio-data',
  311. 'controller class' => 'TripalEntityUIController',
  312. 'menu wildcard' => '%tripal_entity',
  313. 'file' => 'includes/TripalEntityUIController.inc'
  314. ),
  315. 'view modes' => array (
  316. 'full' => array (
  317. 'label' => t ('Full content'),
  318. 'custom settings' => FALSE
  319. ),
  320. 'teaser' => array (
  321. 'label' => t ('Teaser'),
  322. 'custom settings' => TRUE
  323. )
  324. )
  325. );
  326. //
  327. // The TripalBundle entity is used manage the bundle types. The 'bundle of'
  328. // attribute links this to the TripalEntity and allows the UI provided
  329. // by the entity module to work for each TripalEntity bundle.
  330. //
  331. $entities['TripalBundle'] = array (
  332. 'label' => 'Tripal Content Type',
  333. 'entity class' => 'TripalBundle',
  334. 'controller class' => 'TripalBundleController',
  335. 'base table' => 'tripal_bundle',
  336. 'fieldable' => FALSE,
  337. 'bundle of' => 'TripalEntity',
  338. 'exportable' => FALSE,
  339. 'entity keys' => array (
  340. 'id' => 'id',
  341. 'name' => 'bundle',
  342. 'label' => 'label'
  343. ),
  344. 'access callback' => 'tripal_bundle_access',
  345. 'module' => 'tripal_entities',
  346. // Enable the entity API's admin UI.
  347. 'admin ui' => array (
  348. 'path' => 'admin/structure/bio-data',
  349. 'controller class' => 'TripalBundleUIController',
  350. 'file' => 'includes/TripalBundleUIController.inc',
  351. 'menu wildcard' => '%tripal_bundle',
  352. )
  353. );
  354. return $entities;
  355. }
  356. /**
  357. * Implements hook_entities_info_alter().
  358. *
  359. * Add in the bundles (entity types) to the TripalEntity entity.
  360. */
  361. function tripal_entities_entity_info_alter(&$entity_info){
  362. if (array_key_exists('TripalEntity', $entity_info)) {
  363. // Dynamically add in the bundles. Bundles are alternative groups of fields
  364. // or configuration associated with an entity type .We want to dynamically
  365. // add the bundles to the entity.
  366. $bundles = db_select('tripal_bundle', 'tb')
  367. ->fields('tb')
  368. ->execute();
  369. while ($bundle = $bundles->fetchObject()) {
  370. $bundle_id = $bundle->bundle;
  371. $cvterm_id = preg_replace('/bio-data_/', '', $bundle_id);
  372. $cvterm = tripal_get_cvterm(array('cvterm_id' => $cvterm_id));
  373. $label = preg_replace('/_/', ' ', ucwords($cvterm->name));
  374. $entity_info['TripalEntity']['bundles'][$bundle_id] = array (
  375. 'label' => $label,
  376. 'admin' => array (
  377. 'path' => 'admin/structure/bio-data/manage/%tripal_bundle',
  378. 'real path' => 'admin/structure/bio-data/manage/' . $bundle_id,
  379. 'bundle argument' => 4,
  380. 'access arguments' => array (
  381. 'administer tripal data types'
  382. )
  383. )
  384. );
  385. }
  386. }
  387. }
  388. /**
  389. * Menu argument loader; Load a tripal data type by string.
  390. *
  391. * @param $type
  392. * The machine-readable name of a tripal data type to load.
  393. * @return
  394. * A tripal data type array or FALSE if $type does not exist.
  395. */
  396. function tripal_bundle_load($bundle_type, $reset = FALSE) {
  397. // Get the type of entity by the ID.
  398. $bundle_types = db_select('tripal_bundle', 'tdt')
  399. ->fields('tdt', array('id', 'type'))
  400. ->condition('bundle', $bundle_type)
  401. ->execute()
  402. ->fetchObject();
  403. if ($bundle_types) {
  404. $entity = entity_load('TripalBundle', array($bundle_types->id), array(), $reset);
  405. return reset($entity);
  406. }
  407. return FALSE;
  408. }
  409. /**
  410. * Allows the menu system to use a wildcard to fetch the entity.
  411. *
  412. * Make sure that the wildcard you choose in the tripal_entity entity
  413. * definition fits the function name here.
  414. *
  415. * @param $id
  416. * Integer specifying the tripal_entity id.
  417. * @param $reset
  418. * A boolean indicating that the internal cache should be reset.
  419. * @return
  420. * A fully-loaded $tripal_entity object or FALSE if it cannot be loaded.
  421. *
  422. * @see tripal_entity_load_multiple()
  423. */
  424. function tripal_entity_load($id, $reset = FALSE) {
  425. $entity = entity_load('TripalEntity', array($id), array(), $reset);
  426. return reset($entity);
  427. }