corresponding_entity_references.module 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. /**
  3. * @file
  4. * Module file providing the "corresponding entity reference" module main
  5. * functions.
  6. */
  7. /**
  8. * Implements hook_menu().
  9. */
  10. function corresponding_entity_references_menu() {
  11. $items = array();
  12. $items['admin/config/system/corresponding_entity_references'] = array(
  13. 'title' => 'Corresponding entity references',
  14. 'page callback' => 'drupal_get_form',
  15. 'page arguments' => array('corresponding_entity_references_settings_form'),
  16. 'access arguments' => array('administer corresponding entity references settings'),
  17. 'file' => 'corresponding_entity_references.admin.inc',
  18. 'type' => MENU_NORMAL_ITEM,
  19. );
  20. $items['admin/config/system/corresponding_entity_references/references'] = array(
  21. 'title' => 'Corresponding entity references',
  22. 'page callback' => 'drupal_get_form',
  23. 'page arguments' => array('corresponding_entity_references_settings_form'),
  24. 'access arguments' => array('administer corresponding entity references settings'),
  25. 'file' => 'corresponding_entity_references.admin.inc',
  26. 'type' => MENU_DEFAULT_LOCAL_TASK,
  27. );
  28. $items['admin/config/system/corresponding_entity_references/update'] = array(
  29. 'title' => 'Update existing entities',
  30. 'page callback' => 'drupal_get_form',
  31. 'page arguments' => array('corresponding_entity_references_update_form'),
  32. 'access arguments' => array('administer corresponding entity references settings'),
  33. 'file' => 'corresponding_entity_references.admin.inc',
  34. 'type' => MENU_LOCAL_TASK,
  35. );
  36. return $items;
  37. }
  38. /**
  39. * Implements hook_permission().
  40. */
  41. function corresponding_entity_references_permission() {
  42. return array(
  43. 'administer corresponding entity references settings' => array(
  44. 'title' => t('Administer corresponding entity reference settings'),
  45. 'description' => t('Administer corresponding entity reference settings'),
  46. )
  47. );
  48. }
  49. /**
  50. * Formats a label.
  51. */
  52. function corresponding_entity_references_format_label($key) {
  53. $key = explode(' ', $key);
  54. return t('Field instance:"!field1" on Entity type:"!entity1" - Bundle type:"!bundle1" <b>Corresponds with</b> Field instance:"!field2" on Entity type:"!entity2" Bundle type:"!bundle2"',
  55. array('!entity1' => $key[0], '!bundle1' => $key[1], '!field1' => $key[2], '!entity2' => $key[3], '!bundle2' => $key[4], '!field2' => $key[5]));
  56. }
  57. /**
  58. * Implements hook_entity_insert().
  59. */
  60. function corresponding_entity_references_entity_insert($entity, $type) {
  61. corresponding_entity_references_processing_entity('insert', $entity, $type);
  62. }
  63. /**
  64. * Implements hook_entity_update().
  65. */
  66. function corresponding_entity_references_entity_update($entity, $type) {
  67. corresponding_entity_references_processing_entity('update', $entity, $type);
  68. }
  69. /**
  70. * Implements hook_entity_delete().
  71. */
  72. function corresponding_entity_references_entity_delete($entity, $type) {
  73. corresponding_entity_references_processing_entity('delete', $entity, $type);
  74. }
  75. /**
  76. * Load enabled CNR presets.
  77. */
  78. function corresponding_entity_references_preset_load_enabled() {
  79. ctools_include('export');
  80. return ctools_export_load_object('corresponding_entity_references', 'conditions', array('enabled' => 1));
  81. }
  82. /**
  83. * Return CNR preset by key.
  84. */
  85. function corresponding_entity_references_preset_load($key) {
  86. ctools_include('export');
  87. return ctools_export_crud_load('corresponding_entity_references', $key);
  88. }
  89. /**
  90. * Return 1 if CNR preset specified by given key is enabled.
  91. */
  92. function corresponding_entity_references_preset_enabled($key) {
  93. $preset = corresponding_entity_references_preset_load($key);
  94. return empty($preset) ? 0 : $preset->enabled;
  95. }
  96. /**
  97. * Process a entity's corresponding entity references.
  98. *
  99. * @param $op the operation being performed on the entity.
  100. * @param $entity the entity object
  101. * @param $process_unchanged whether or not to process entity reference fields
  102. * whose values have not changed.
  103. */
  104. function corresponding_entity_references_processing_entity($op, $entity, $type, $process_unchanged = FALSE) {
  105. module_load_include('inc', 'corresponding_entity_references', 'corresponding_entity_references.crud');
  106. $result = corresponding_entity_references_preset_load_enabled();
  107. while ($row = array_shift($result)) {
  108. $key = explode('*', $row->entity_types_content_fields);
  109. if(($type == $key[0]) || ($type == $key[3])){
  110. $entity->home = _corresponding_entity_references_entity_get_bundle($entity, $type);
  111. switch ($entity->home) {
  112. case $key[1]:
  113. // Create an array to pass to op function instead of 6 arguments.
  114. $keys = array(
  115. 'home_entity_type' => $key[0],
  116. 'home_bundle' => $key[1],
  117. 'home_field' => $key[2],
  118. 'away_entity_type' => $key[3],
  119. 'away_bundle' => $key[4],
  120. 'away_field' => $key[5],
  121. );
  122. $args = array($entity, $keys, $process_unchanged);
  123. $function = 'corresponding_entity_references_' . $op;
  124. call_user_func_array($function, $args);
  125. if ($key[0] != $key[2]) {
  126. break;
  127. }
  128. // Fall through.
  129. case $key[4]:
  130. $keys = array(
  131. 'home_entity_type' => $key[3],
  132. 'home_bundle' => $key[4],
  133. 'home_field' => $key[5],
  134. 'away_entity_type' => $key[0],
  135. 'away_bundle' => $key[1],
  136. 'away_field' => $key[2],
  137. );
  138. $args = array($entity, $keys, $process_unchanged);
  139. $function = 'corresponding_entity_references_' . $op;
  140. call_user_func_array($function, $args);
  141. break;
  142. }
  143. }
  144. }
  145. }
  146. /**
  147. * Submit a batch job to index the remaining, unindexed content.
  148. */
  149. function corresponding_entity_references_batch_index_remaining($types, $limit) {
  150. $batch = array(
  151. 'operations' => array(
  152. array(
  153. 'corresponding_entity_references_batch_update_existing_entities',
  154. array($types, $limit)
  155. ),
  156. ),
  157. 'finished' => 'corresponding_entity_references_batch_update_existing_finished',
  158. 'title' => t('Processing'),
  159. 'init_message' => t('Preparing to update corresponding entity references for existing entities...'),
  160. 'progress_message' => t('Processing entities...'),
  161. 'error_message' => t('Corresponding entity references - existing entity update has encountered an error.'),
  162. );
  163. batch_set($batch);
  164. }
  165. /**
  166. * Batch Operation Callback
  167. *
  168. * @see corresponding_entity_references_batch_index_remaining()
  169. */
  170. function corresponding_entity_references_batch_update_existing_entities($types, $limit, &$context) {
  171. // If we are on our first time through.
  172. if (!isset($context['sandbox']['progress'])) {
  173. $context['sandbox']['progress'] = 0;
  174. $context['sandbox']['current_entity'] = 0;
  175. $context['sandbox']['max'] = db_query("SELECT COUNT(DISTINCT nid) FROM {node} WHERE type IN (:types)", array(':types' => $types))->fetchField();
  176. }
  177. $nids = array();
  178. $args = $types;
  179. $args['current_entity'] = $context['sandbox']['current_entity'];
  180. // Get entity IDs to update.
  181. $result = db_query_range("SELECT nid FROM {node} WHERE type IN (:types) AND nid > :args ORDER BY nid", 0, $limit, array(':types' => $types, ':args' => $args['current_entity']));
  182. while ($row = $result->fetchObject()) {
  183. $entity = entity_load($row->nid);
  184. corresponding_entity_references_processing_entity('update', $entity, $type, TRUE);
  185. // Update our progress information.
  186. $context['sandbox']['progress']++;
  187. $context['sandbox']['current_entity'] = $entity->nid;
  188. $context['message'] = t('Processed @current of @total entitys', array('@current' => $context['sandbox']['progress'], '@total' => $context['sandbox']['max']));
  189. }
  190. // Inform the batch engine that we are not finished,
  191. // and provide an estimation of the completion level we reached.
  192. if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
  193. $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
  194. }
  195. // Put the total into the results section when we're finished so we can show
  196. // it to the admin.
  197. if ($context['finished']) {
  198. $context['results']['count'] = $context['sandbox']['progress'];
  199. }
  200. }
  201. /**
  202. * Batch 'finished' callback.
  203. *
  204. * @see corresponding_entity_references_batch_index_remaining()
  205. */
  206. function corresponding_entity_references_batch_update_existing_finished($success, $results, $operations) {
  207. if ($success) {
  208. $type = 'status';
  209. $message = format_plural($results['count'], '1 entity processed successfully.', '@count entitys processed successfully.');
  210. }
  211. else {
  212. $type = 'error';
  213. // An error occurred.
  214. // $operations contains the operations that remained unprocessed.
  215. $error_operation = reset($operations);
  216. $message = 'An error occurred while processing ' . $error_operation[0] . ' with arguments:' . print_r($error_operation[0], TRUE);
  217. }
  218. drupal_set_message($message, $type);
  219. }
  220. function corresponding_entity_references_ctools_plugin_api($owner, $api) {
  221. if ($owner == 'corresponding_entity_references' && $api == 'default_corresponding_entity_references_presets') {
  222. return array('version' => 1);
  223. }
  224. }