tripal_entities.module 14 KB

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