cer_entity_settings.module 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. /**
  3. * Implements hook_views_api().
  4. */
  5. function cer_entity_settings_views_api() {
  6. return array('api' => 3);
  7. }
  8. /**
  9. * Implements hook_query_TAG_alter().
  10. */
  11. function cer_entity_settings_query_cer_presets_alter(QueryAlterableInterface $query) {
  12. $entity = $query->getMetaData('entity');
  13. $instance = field_info_instance($entity->type(), 'cer_settings', $entity->getBundle());
  14. if ($instance) {
  15. $IDs = array();
  16. foreach ($entity->cer_settings as $preset) {
  17. $IDs[] = $preset->getIdentifier();
  18. }
  19. // If no presets were selected, guarantee an empty result set by selecting
  20. // the non-existent preset ID 0.
  21. if (empty($IDs)) {
  22. $IDs[] = 0;
  23. }
  24. $query->getMetaData('entity_field_query')->entityCondition('entity_id', $IDs, 'IN');
  25. if (! $entity->cer_store_settings->value()) {
  26. $queue = &drupal_static('cer_entity_settings_exit', array());
  27. $queue[ $entity->type() ][ $entity->getIdentifier() ] = $entity;
  28. }
  29. }
  30. }
  31. /**
  32. * Implements hook_exit().
  33. */
  34. function cer_entity_settings_exit() {
  35. $queue = drupal_static(__FUNCTION__, array());
  36. foreach ($queue as $entity_type => $entities) {
  37. foreach ($entities as $entity) {
  38. // Clear out the stored settings.
  39. $entity->cer_settings->set(NULL);
  40. $entity = $entity->value();
  41. $entity->cer_processed = TRUE;
  42. entity_save($entity_type, $entity);
  43. }
  44. }
  45. }
  46. /**
  47. * Implements hook_cron().
  48. */
  49. function cer_entity_settings_cron($field = NULL) {
  50. // Delete defunct instances of the given field. An instance is considered
  51. // defunct if there are no presets which refer the entity type and bundle
  52. // on which it's instantiated.
  53. if (isset($field)) {
  54. $view = views_get_view('cer_endpoint_in_use');
  55. $field = field_info_field($field);
  56. // Loop through every instance of the field, executing the view for each one.
  57. foreach ($field['bundles'] as $entity_type => $bundles) {
  58. foreach ($bundles as $bundle) {
  59. $view->set_exposed_input(array(
  60. 'left' => "{$entity_type}:{$bundle}:",
  61. 'right' => "{$entity_type}:{$bundle}:",
  62. ));
  63. $view->execute();
  64. if (empty($view->result)) {
  65. $instance = field_info_instance($entity_type, $field['field_name'], $bundle);
  66. if ($instance) {
  67. // Do NOT delete the field if this is the last instance of it. We
  68. // clean up our own fields during uninstall.
  69. field_delete_instance($instance, FALSE);
  70. }
  71. }
  72. }
  73. }
  74. }
  75. else {
  76. call_user_func(__FUNCTION__, 'cer_settings');
  77. call_user_func(__FUNCTION__, 'cer_store_settings');
  78. }
  79. }
  80. /**
  81. * Implements hook_ctools_plugin_directory().
  82. */
  83. function cer_entity_settings_ctools_plugin_directory($owner, $plugin_type) {
  84. return "plugins/{$owner}/{$plugin_type}";
  85. }
  86. /**
  87. * Implements hook_cer_insert().
  88. */
  89. function cer_entity_settings_cer_insert(CerPreset $preset) {
  90. cer_entity_settings_cer_update($preset);
  91. }
  92. /**
  93. * Implements hook_cer_update().
  94. */
  95. function cer_entity_settings_cer_update(CerPreset $preset) {
  96. _cer_entity_settings_create_instance($preset->wrapper->cer_left->chain->value());
  97. _cer_entity_settings_create_instance($preset->wrapper->cer_right->chain->value());
  98. }
  99. /**
  100. * Instantiates the cer_settings and cer_store_settings fields at the head of
  101. * the given chain, if they aren't already there.
  102. */
  103. function _cer_entity_settings_create_instance(FieldChain $chain) {
  104. $field = $chain->current();
  105. $instance = field_info_instance($field->entityType, 'cer_settings', $field->bundle);
  106. if (empty($instance)) {
  107. $instance = array(
  108. 'bundle' => $field->bundle,
  109. 'default_value' => NULL,
  110. 'deleted' => 0,
  111. 'description' => '',
  112. 'display' => array(
  113. 'default' => array(
  114. 'label' => 'above',
  115. 'module' => 'entityreference',
  116. 'settings' => array(
  117. 'link' => FALSE,
  118. ),
  119. 'type' => 'entityreference_label',
  120. 'weight' => 1,
  121. ),
  122. ),
  123. 'entity_type' => $field->entityType,
  124. 'field_name' => 'cer_settings',
  125. 'label' => t('Synchronize this @entity_type with...', array(
  126. '@entity_type' => $field->entityTypeLabel,
  127. )),
  128. 'required' => 0,
  129. 'settings' => array(
  130. ),
  131. 'widget' => array(
  132. 'active' => 1,
  133. 'module' => 'options',
  134. 'settings' => array(
  135. 'match_operator' => 'CONTAINS',
  136. 'path' => '',
  137. 'size' => 60,
  138. ),
  139. 'type' => 'options_buttons',
  140. 'weight' => 1,
  141. ),
  142. );
  143. field_create_instance($instance);
  144. }
  145. $instance = field_info_instance($field->entityType, 'cer_store_settings', $field->bundle);
  146. if (empty($instance)) {
  147. $instance = array(
  148. 'bundle' => $field->bundle,
  149. 'default_value' => array(
  150. 0 => array(
  151. 'value' => 1,
  152. ),
  153. ),
  154. 'deleted' => 0,
  155. 'description' => '',
  156. 'display' => array(
  157. 'default' => array(
  158. 'label' => 'above',
  159. 'module' => 'list',
  160. 'settings' => array(),
  161. 'type' => 'list_default',
  162. 'weight' => 2,
  163. ),
  164. 'teaser' => array(
  165. 'label' => 'above',
  166. 'settings' => array(),
  167. 'type' => 'hidden',
  168. 'weight' => 0,
  169. ),
  170. ),
  171. 'entity_type' => $field->entityType,
  172. 'field_name' => 'cer_store_settings',
  173. 'label' => t('Remember these settings'),
  174. 'required' => 0,
  175. 'settings' => array(
  176. ),
  177. 'widget' => array(
  178. 'active' => 1,
  179. 'module' => 'options',
  180. 'settings' => array(
  181. 'display_label' => 1,
  182. ),
  183. 'type' => 'options_onoff',
  184. 'weight' => 2,
  185. ),
  186. );
  187. field_create_instance($instance);
  188. }
  189. }