entityconnect.module 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. /**
  3. * @file
  4. * Handles the main hooks used by entityconnect.
  5. *
  6. * One will find most of the meet in the form and menu includes.
  7. */
  8. define('ENTITYCONNECT_CACHE_TABLE', 'cache_entityconnect');
  9. /**
  10. * Includes.
  11. */
  12. require_once 'includes/entityconnect.fields.inc';
  13. require_once 'includes/entityconnect.utils.inc';
  14. /**
  15. * Implements hook_permission().
  16. *
  17. * @return Assoc
  18. * permission items
  19. */
  20. function entityconnect_permission() {
  21. return array(
  22. 'entityconnect add button' => array(
  23. 'title' => t('Allows users to see add button'),
  24. 'description' => t('Display the add button for user'),
  25. ),
  26. 'entityconnect edit button' => array(
  27. 'title' => t('Allows users to see edit button'),
  28. 'description' => t('Display the edit button for user'),
  29. ),
  30. );
  31. }
  32. /**
  33. * Implements hook_menu().
  34. */
  35. function entityconnect_menu() {
  36. $items = array();
  37. $items['admin/entityconnect/return/%'] = array(
  38. 'description' => 'Return item for original entity.',
  39. 'page callback' => 'entityconnect_return',
  40. 'page arguments' => array(3),
  41. 'access callback' => 'entityconnect_check_access',
  42. 'file' => 'includes/entityconnect.menu.inc',
  43. );
  44. $items['admin/entityconnect/edit/%'] = array(
  45. 'description' => 'Edit item for entity referenced.',
  46. 'page callback' => 'entityconnect_edit',
  47. 'page arguments' => array(3),
  48. 'access callback' => 'user_access',
  49. 'access arguments' => array('entityconnect edit button'),
  50. 'file' => 'includes/entityconnect.menu.inc',
  51. );
  52. $items['admin/entityconnect/add/%'] = array(
  53. 'title' => "Choose type to create and add",
  54. 'description' => 'Add item for entity referenced.',
  55. 'page callback' => 'entityconnect_add',
  56. 'page arguments' => array(3),
  57. 'access callback' => 'user_access',
  58. 'access arguments' => array('entityconnect add button'),
  59. 'file' => 'includes/entityconnect.menu.inc',
  60. );
  61. $items['admin/config/content/entityconnect'] = array(
  62. 'title' => 'Entity Connect',
  63. 'description' => 'Configure default values for Entity Reference fields using Entity Connect',
  64. 'page callback' => 'drupal_get_form',
  65. 'page arguments' => array('_entityconnect_admin_form'),
  66. 'access arguments' => array('administer site configuration'),
  67. 'file' => 'includes/entityconnect.admin.inc',
  68. );
  69. return $items;
  70. }
  71. /**
  72. * Access callback: Used in return menu.
  73. */
  74. function entityconnect_check_access() {
  75. if (user_access('entityconnect add button') || user_access('entityconnect edit button')) {
  76. return TRUE;
  77. }
  78. else {
  79. return FALSE;
  80. }
  81. }
  82. /**
  83. * Implements hook_field_attach_form().
  84. */
  85. function entityconnect_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) {
  86. entityconnect_include_form();
  87. foreach (_entityconnect_get_ref_fields() as $field_name => $field) {
  88. if (isset($form[$field_name])) {
  89. _entityconnect_field_attach_form($entity_type, $entity, $form,
  90. $form_state, $field_name, $field, $langcode);
  91. }
  92. }
  93. }
  94. /**
  95. * Implements hook_theme().
  96. */
  97. function entityconnect_theme() {
  98. $items = array();
  99. $items['entityconnect_taxonomy_term_add_list'] = array(
  100. 'arguments' => array('items' => NULL),
  101. 'file' => 'includes/entityconnect.pages.inc',
  102. );
  103. $items['entityconnect_entity_add_list'] = array(
  104. 'arguments' => array('items' => NULL, 'cache id' => NULL),
  105. 'file' => 'includes/entityconnect.pages.inc',
  106. );
  107. return $items;
  108. }
  109. /**
  110. * Include the form inc file.
  111. *
  112. * This can be used in #submit array before the sumbit functions which are
  113. * located in the inc file.
  114. */
  115. function entityconnect_include_form() {
  116. module_load_include("inc", "entityconnect", "includes/entityconnect.form");
  117. }
  118. /**
  119. * Wraps cache set.
  120. *
  121. * We can set the expire easily if needed.
  122. */
  123. function entityconnect_cache_set($cid, $data) {
  124. // Get cache_lifetime variable.
  125. $cache_lifetime = variable_get('entityconnect_cache_lifetime', CACHE_PERMANENT);
  126. if ($cache_lifetime !== CACHE_PERMANENT) {
  127. $cache_lifetime = REQUEST_TIME + $cache_lifetime;
  128. }
  129. return cache_set($cid, $data, ENTITYCONNECT_CACHE_TABLE, $cache_lifetime);
  130. }
  131. /**
  132. * Wraps cache get.
  133. */
  134. function entityconnect_cache_get($cid) {
  135. return cache_get($cid, ENTITYCONNECT_CACHE_TABLE);
  136. }
  137. /**
  138. * Implements hook_flush_caches().
  139. */
  140. function entityconnect_flush_caches() {
  141. return array('cache_entityconnect');
  142. }
  143. /**
  144. * Implements hook_form_alter().
  145. *
  146. * If we are adding a new entity we pass of to entityconnect_add_form_alter
  147. * if we are returning to the parent form we hand off to
  148. * entityconnect_return_form_alter.
  149. */
  150. function entityconnect_form_alter(&$form, &$form_state, $form_id) {
  151. $child = isset($_REQUEST['child']);
  152. // We can get the cid two different ways
  153. // first we try the $_REQUEST param. if we do not getting it from there we
  154. // try the arg(3) if we are on a add form. Also if we are on an add form we
  155. // know that we are a child page.
  156. foreach(arg() as $key => $value) {
  157. if (stripos($value, 'entityconnect-form-', 0) === 0) {
  158. $cid = $value;
  159. }
  160. }
  161. if ((isset($_REQUEST['build_cache_id']) && ($cid = $_REQUEST['build_cache_id']))
  162. || ((isset($cid)) && $child = TRUE)) {
  163. $cache = entityconnect_cache_get($cid);
  164. entityconnect_include_form();
  165. if ($child) {
  166. $form_state['#entityconnect_child_form'] = $cache;
  167. }
  168. if (isset($_REQUEST['return']) && isset($cache->data) && $cache->data['form']['#form_id'] == $form_id) {
  169. unset($_REQUEST['build_cache_id']);
  170. entityconnect_return_form_alter($form, $form_state, $form_id, $cid, $cache);
  171. }
  172. }
  173. // If this form is a child form let's add alter for that purpose
  174. // Note that we are doing this here becuase when we retrun to a form it gets
  175. // rebuilt so this will get caught in the rebuilt.
  176. if (isset($form_state['#entityconnect_child_form']) && $form_state['#entityconnect_child_form']) {
  177. $cache = $form_state['#entityconnect_child_form'];
  178. module_load_include('inc', 'entityconnect', 'includes/entityconnect.form');
  179. entityconnect_child_form_alter($form, $form_state, $form_id, $cache->cid, $cache);
  180. }
  181. }
  182. /**
  183. * Implements hook_module_implements_alter().
  184. *
  185. * Ensure entityconnect gets called after workbench_moderation when altering the node form
  186. *
  187. * @param array $module_list
  188. * @param string $context
  189. */
  190. function entityconnect_module_implements_alter(&$module_list, $context) {
  191. // Need to override both to get correct sort order.
  192. // @see: https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_module_implements_alter/7
  193. if($context == 'form_node_form_alter' || $context == 'form_alter') {
  194. // Move entityconnect implementation to the end of the list to ensure it gets called last.
  195. $group = isset($module_list['entityconnect']) ? $module_list['entityconnect'] : FALSE;
  196. unset($module_list['entityconnect']);
  197. $module_list['entityconnect'] = $group;
  198. }
  199. }