cer.admin.inc 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <?php
  2. /**
  3. * @file
  4. * Administrative functionality, separated for performance purposes.
  5. */
  6. /**
  7. * The settings form.
  8. */
  9. function cer_settings_form($form = array(), &$form_state) {
  10. $channels = array();
  11. foreach (_cer_get_fields() as $field) {
  12. // A field that hasn't been instantiated yet will not have a 'bundles' key, which
  13. // means it's useless to us, so skip over it.
  14. if (! isset($field['bundles'])) {
  15. continue;
  16. }
  17. foreach ($field['bundles'] as $entity_type => $bundles) {
  18. foreach ($bundles as $bundle) {
  19. $instance = field_info_instance($entity_type, $field['field_name'], $bundle);
  20. if ($instance) {
  21. $channels = array_merge($channels, _cer_find_channels($instance));
  22. }
  23. }
  24. }
  25. }
  26. if (empty($channels)) {
  27. drupal_set_message(t('There are no entity reference fields that can correspond.'), 'warning');
  28. }
  29. else {
  30. $mapping = array();
  31. foreach ($channels as $count => $key) {
  32. $formatted_key = str_replace(' ', '*', $key);
  33. $mapping[$count] = $formatted_key;
  34. $form['values']["enabled_{$count}"] = array(
  35. '#type' => 'checkbox',
  36. '#default_value' => cer_preset_enabled($formatted_key),
  37. '#title' => filter_xss(theme('cer_label', array('key' => $key))),
  38. );
  39. }
  40. // We are using a hidden field to submit the configuration because on
  41. // some systems the checkbox name length is limited, and using
  42. // the key would cause the length to be too long. (Issue #558612)
  43. $form['mapping'] = array(
  44. '#type' => 'hidden',
  45. '#value' => serialize($mapping),
  46. );
  47. $form['submit'] = array(
  48. '#type' => 'submit',
  49. '#value' => t('Save'),
  50. );
  51. }
  52. return $form;
  53. }
  54. /**
  55. * Submit function for settings form.
  56. */
  57. function cer_settings_form_submit($form, $form_state) {
  58. ctools_include('export');
  59. $query_values = array();
  60. $mapping = unserialize($form_state['values']['mapping']);
  61. foreach ($form_state['values'] as $key => $value) {
  62. $keys = explode('_', $key);
  63. if ($keys[0] == 'enabled') {
  64. $query_values[$mapping[$keys[1]]] = $value;
  65. }
  66. }
  67. // load all existing presets
  68. $presets = ctools_export_crud_load_all('cer');
  69. foreach ($query_values as $key => $value) {
  70. // get preset object (create new one if it doesn't exist already).
  71. $preset = empty($presets[$key]) ? ctools_export_crud_new('cer') : $presets[$key];
  72. // set and save value
  73. if (empty($preset->entity_types_content_fields)) {
  74. $preset->entity_types_content_fields = $key;
  75. }
  76. $preset->enabled = $value;
  77. ctools_export_crud_save('cer', $preset);
  78. // remove from list of presets, so we know which ones are still used
  79. if (isset($presets[$key])) {
  80. unset($presets[$key]);
  81. }
  82. }
  83. drupal_set_message(t('The configuration has been saved.'));
  84. }
  85. /**
  86. * Allows batch updating of existing entities.
  87. */
  88. function cer_update_form($form = array(), &$form_state) {
  89. $form['type'] = array(
  90. '#type' => 'select',
  91. '#title' => t('Entity type'),
  92. '#required' => TRUE,
  93. '#options' => array(),
  94. '#description' => t('Select the entity type that you want to update.'),
  95. );
  96. foreach (entity_get_info() as $type => $class) {
  97. $form['type']['#options'][$type] = $class['label'];
  98. }
  99. $form['submit'] = array(
  100. '#type' => 'submit',
  101. '#value' => t('Submit'),
  102. );
  103. return $form;
  104. }
  105. /**
  106. * The update form.
  107. * Allows updating of current entitys.
  108. */
  109. function cer_update_form_submit($form, &$form_state) {
  110. $batch = array(
  111. 'finished' => 'cer_batch_update_existing_finished',
  112. 'title' => t('Processing'),
  113. 'init_message' => t('Preparing to update corresponding entity references for existing entities...'),
  114. 'progress_message' => t('Processing entities...'),
  115. 'error_message' => t('Corresponding entity references - existing entity update has encountered an error.'),
  116. );
  117. $entity_type = $form_state['values']['type'];
  118. $query = new EntityFieldQuery();
  119. $query->entityCondition('entity_type', $entity_type);
  120. $result = $query->execute();
  121. if (isset($result[$entity_type])) {
  122. foreach ($result[$entity_type] as $entity_id => $stub) {
  123. $batch['operations'][] = array('cer_processing_entity', array('update', $entity_id, $entity_type));
  124. }
  125. }
  126. batch_set($batch);
  127. }
  128. /**
  129. * The purpose of this function is to answer this question: I am a field instance. Which other
  130. * fields reference the entity that owns me? And of those instances, which ones can I reference?
  131. * The answer is returned as an array of CER keys: "entity1 bundle1 field1 entity2 bundle2 field2".
  132. *
  133. * @param array $instance
  134. * Field instance info, as returned by field_info_instance().
  135. *
  136. * @return array
  137. */
  138. function _cer_find_channels($instance) {
  139. $channels = array();
  140. $my_id = $instance['entity_type'] . ' ' . $instance['bundle'] . ' ' . $instance['field_name'];
  141. $my_info = field_info_field($instance['field_name']);
  142. $my_targets = _cer_get_target_bundles($my_info);
  143. $my_target_type = $my_info['settings']['target_type'];
  144. $referrers = _cer_find_referrers($instance['entity_type'], $instance['bundle']);
  145. foreach ($referrers as $referrer) {
  146. if (isset($referrer['bundles'][$my_target_type])) {
  147. if (empty($my_targets)) {
  148. $bundles = $referrer['bundles'][$my_target_type];
  149. }
  150. else {
  151. $bundles = array_intersect($referrer['bundles'][$my_target_type], $my_targets);
  152. }
  153. foreach ($bundles as $bundle) {
  154. $channels[] = "{$my_id} {$my_target_type} {$bundle} " . $referrer['field_name'];
  155. }
  156. }
  157. }
  158. return $channels;
  159. }
  160. /**
  161. * Find all fields that can reference the given entity type and bundle.
  162. *
  163. * @param $entity_type
  164. * The entity type that can be referenced.
  165. * @param $bundle
  166. * The bundle that can be referenced.
  167. *
  168. * @return array
  169. */
  170. function _cer_find_referrers($entity_type, $bundle) {
  171. $referrers = array();
  172. foreach (_cer_get_fields() as $field) {
  173. if ($field['settings']['target_type'] == $entity_type) {
  174. $target_bundles = _cer_get_target_bundles($field);
  175. if (empty($target_bundles) || in_array($bundle, $target_bundles)) {
  176. $referrers[] = $field;
  177. }
  178. }
  179. }
  180. return $referrers;
  181. }
  182. /**
  183. * Find all bundles reference-able by a given field. If all bundles are reference-able,
  184. * an empty array is returned.
  185. *
  186. * @param $field
  187. * Field info array as returned by field_info_field().
  188. *
  189. * @return array
  190. */
  191. function _cer_get_target_bundles($field) {
  192. $target_bundles = array();
  193. // If the reference field is using a view, load the view and see if it's filtering by the entity
  194. // type's bundle filter. If it is, the filter values are the target bundles. Otherwise,
  195. // assume that all bundles can be referenced.
  196. //
  197. // @todo Support contextual filters?
  198. //
  199. // NOTE: Selection handlers (i.e., $field['settings']['handler']) are plugins owned by
  200. // Entity Reference. There is no method defined to get an array of referenceable
  201. // bundles, but hopefully, if CER gains enough traction in the community, such a
  202. // method can be added to the EntityReference_SelectionHandler interface. This
  203. // function could then be deprecated, which would be a more flexible, future-proof
  204. // method of finding a field's target bundles.
  205. //
  206. if ($field['settings']['handler'] == 'views') {
  207. $view_name = $field['settings']['handler_settings']['view']['view_name'];
  208. $view = views_get_view($view_name);
  209. if ($view) {
  210. $view->set_display($field['settings']['handler_settings']['view']['display_name']);
  211. $info = entity_get_info($field['settings']['target_type']);
  212. if ($info['entity keys']['bundle'] && $handler = $view->display_handler->get_handler('filter', $info['entity keys']['bundle'])) {
  213. $target_bundles = $handler->value;
  214. }
  215. }
  216. else {
  217. drupal_set_message(t('Could not get target bundles for %field (failed to load view %view).', array('%view' => $view_name, '%field' => $field['field_name'])), 'error');
  218. }
  219. }
  220. elseif (isset($field['settings']['handler_settings']['target_bundles'])) {
  221. $target_bundles = $field['settings']['handler_settings']['target_bundles'];
  222. }
  223. else {
  224. $info = entity_get_info($field['settings']['target_type']);
  225. $target_bundles = array_keys($info['bundles']);
  226. }
  227. return $target_bundles;
  228. }
  229. /**
  230. * Get the Field API definitions for all entity reference fields.
  231. *
  232. * @return array
  233. */
  234. function _cer_get_fields() {
  235. static $fields;
  236. if (!isset($fields)) {
  237. $fields = array_map('field_info_field', db_select('field_config', 'fc')->fields('fc', array('field_name'))->condition('type', 'entityreference')->execute()->fetchCol());
  238. }
  239. return $fields;
  240. }