cer.admin.inc 7.7 KB

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