cer.module 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. <?php
  2. /**
  3. * Implements hook_entity_info().
  4. */
  5. function cer_entity_info() {
  6. $info = array(
  7. 'cer' => array(
  8. 'label' => t('CER Preset'),
  9. 'entity class' => 'CerPreset',
  10. 'controller class' => 'CerPresetController',
  11. 'base table' => 'cer_preset',
  12. 'label callback' => 'entity_class_label',
  13. 'module' => 'cer',
  14. 'fieldable' => TRUE,
  15. 'entity keys' => array(
  16. 'id' => 'pid',
  17. 'name' => 'identifier', // This is the automatically generated export key.
  18. 'label' => 'identifier', // The identifier will be the default label.
  19. ),
  20. 'admin ui' => array(
  21. 'path' => 'admin/config/content/cer',
  22. 'file' => 'cer.admin.inc',
  23. 'controller class' => 'CerUIController',
  24. ),
  25. 'exportable' => TRUE,
  26. 'features controller class' => 'CerPresetFeaturesController',
  27. 'access callback' => 'cer_access',
  28. ),
  29. );
  30. if (variable_get('cer_enable_field_ui', FALSE)) {
  31. $info['cer']['bundles']['cer']['admin']['path'] = $info['cer']['admin ui']['path'];
  32. }
  33. return $info;
  34. }
  35. /**
  36. * Access callback for Entity UI.
  37. */
  38. function cer_access($op, $preset = NULL, $account = NULL) {
  39. return user_access('administer cer settings', $account);
  40. }
  41. /**
  42. * Implements hook_menu().
  43. */
  44. function cer_menu() {
  45. $info = cer_entity_info();
  46. $prefix = $info['cer']['admin ui']['path'];
  47. return array(
  48. "{$prefix}/update" => array(
  49. 'title' => 'Bulk Update',
  50. 'page callback' => 'drupal_get_form',
  51. 'page arguments' => array('cer_bulk_update_form'),
  52. 'access arguments' => array('administer cer settings'),
  53. 'file' => 'cer.admin.inc',
  54. 'type' => MENU_LOCAL_TASK,
  55. ),
  56. );
  57. return $items;
  58. }
  59. /**
  60. * Implements hook_permission().
  61. */
  62. function cer_permission() {
  63. return array(
  64. 'administer cer settings' => array(
  65. 'title' => t('Administer corresponding references'),
  66. )
  67. );
  68. }
  69. /**
  70. * Implements hook_field_delete_instance().
  71. */
  72. function cer_field_delete_instance(array $instance) {
  73. // Delete every CER preset which refers to the deleted field instance.
  74. $filter = $instance['entity_type'] . ':' . $instance['bundle'] . ':' . $instance['field_name'];
  75. $baseQuery = new EntityFieldQuery();
  76. $baseQuery->entityCondition('entity_type', 'cer');
  77. $baseQuery->fieldCondition('cer_enabled', 'value', TRUE);
  78. $query = clone $baseQuery;
  79. $query->fieldCondition('cer_left', 'path', $filter, 'CONTAINS');
  80. $result = $query->execute();
  81. if (isset($result['cer'])) {
  82. foreach (entity_load('cer', array_keys($result['cer'])) as $preset) {
  83. $preset->delete();
  84. }
  85. }
  86. $query = clone $baseQuery;
  87. $query->fieldCondition('cer_right', 'path', $filter, 'CONTAINS');
  88. $result = $query->execute();
  89. if (isset($result['cer'])) {
  90. foreach (entity_load('cer', array_keys($result['cer'])) as $preset) {
  91. $preset->delete();
  92. }
  93. }
  94. }
  95. /**
  96. * Implements hook_node_insert().
  97. */
  98. function cer_node_insert(StdClass $node) {
  99. // Write access grants *before* doing CER stuff in order to prevent a race condition.
  100. // This tricky bug can easily rear its ugly head if you have an Entity Reference field,
  101. // referencing nodes, and a node access module enabled.
  102. //
  103. // Entity Reference's bundled selection handlers will use either EntityFieldQuery or
  104. // Views, both of which are affected by node access grants (and rightfully so).
  105. // However, when creating a node, core invokes hook_node_save() *before* it writes the
  106. // grants to the database, which can cause EntityFieldQuery (or Views, unless
  107. // configured to disable SQL rewriting) to return no results if the user isn't the
  108. // superuser. Since CER asks the field backend to validate the reference, this can
  109. // cause the reference to not be validated, and the cross-reference to fail.
  110. //
  111. // Really, this is a core issue and not a CER issue. Core should be invoking
  112. // hook_node_save() AFTER it writes access info. But we can work around it by writing
  113. // the access info, doing our own processing, and then clearing the access info
  114. // so node_save() can write it cleanly. And that's what this does.
  115. //
  116. // Hear that, core devs? Fix it fix it fix it!
  117. //
  118. node_access_acquire_grants($node);
  119. cer_processing_entity('insert', $node, 'node');
  120. db_delete('node_access')->condition('nid', $node->nid)->execute();
  121. }
  122. /**
  123. * Implements hook_entity_insert().
  124. */
  125. function cer_entity_insert($entity, $type) {
  126. if (! function_exists("cer_{$type}_insert")) {
  127. cer_processing_entity('insert', $entity, $type);
  128. }
  129. }
  130. /**
  131. * Implements hook_entity_update().
  132. */
  133. function cer_entity_update($entity, $type) {
  134. if (! function_exists("cer_{$type}_update")) {
  135. cer_processing_entity('update', $entity, $type);
  136. }
  137. }
  138. /**
  139. * Implements hook_entity_delete().
  140. */
  141. function cer_entity_delete($entity, $type) {
  142. if (! function_exists("cer_{$type}_delete")) {
  143. cer_processing_entity('delete', $entity, $type);
  144. }
  145. }
  146. /**
  147. * Process a entity's corresponding entity references.
  148. *
  149. * @param string $op
  150. * The operation being performed on the entity (insert, update, or delete).
  151. *
  152. * @param object $entity
  153. * The entity object (optionally wrapped), or its ID.
  154. *
  155. * @param string $entity_type
  156. * The entity type. If $entity is wrapped, this can be NULL since the entity
  157. * type is known by the wrapper.
  158. *
  159. * @param array $context
  160. * Either the Batch API context (since this is the callback function used
  161. * during bulk update) or NULL if we're not in a batch job.
  162. */
  163. function cer_processing_entity($op, $entity, $entity_type = NULL, array &$context = NULL) {
  164. // Don't do anything if the MAINTENANCE_MODE flag is set. This isn't the same thing
  165. // as user-facing maintenance mode, but rather is set when running, say, update.php
  166. // or another relatively low-level operation. This was added to prevent CER from
  167. // running while updating from 1.x or 2.x, since classes may not yet be registered
  168. // yet and we don't want to cause fatal errors during update.
  169. if (defined('MAINTENANCE_MODE')) {
  170. return;
  171. }
  172. // Don't process anything that hasn't got the cer data structure: this provides an
  173. // opportunity for other modules to opt their entities out of CER processing.
  174. if ($entity instanceof EntityStructureWrapper) {
  175. try {
  176. // If there is no such a property an exception will be thrown.
  177. $entity->getPropertyInfo('cer');
  178. }
  179. catch (EntityMetadataWrapperException $e) {
  180. return;
  181. }
  182. }
  183. if ($entity instanceof EntityDrupalWrapper) {
  184. // Under certain circumstances, the cer struct may not be known to Entity
  185. // API. So check for that before doing any actual processing. If it's not
  186. // known yet, rebuild the property info cache and re-instantiate the
  187. // wrapper with all the latest property definitions.
  188. try {
  189. $entity->getPropertyInfo('cer');
  190. }
  191. catch (EntityMetadataWrapperException $e) {
  192. entity_property_info_cache_clear();
  193. $entity = new EntityDrupalWrapper($entity->type(), $entity->value());
  194. }
  195. $finder = new CerPresetFinder($entity);
  196. foreach ($finder->execute() as $preset) {
  197. $handler = new CerPresetHandler($preset, $entity);
  198. $handler->$op();
  199. }
  200. if ($context) {
  201. if (! isset($context['results']['count'])) {
  202. $context['results']['count'] = 0;
  203. }
  204. $context['results']['count']++;
  205. }
  206. }
  207. elseif ($entity_type) {
  208. if (is_numeric($entity)) {
  209. $entity = entity_object_load($entity, $entity_type);
  210. }
  211. if (is_object($entity) && empty($entity->cer_processed)) {
  212. cer_processing_entity($op, new EntityDrupalWrapper($entity_type, $entity), NULL, $context);
  213. }
  214. }
  215. }
  216. /**
  217. * Batch 'finished' callback.
  218. */
  219. function cer_batch_update_existing_finished($success, $results, $operations) {
  220. if ($success) {
  221. $message = format_plural($results['count'], '1 entity processed.', '@count entities processed.');
  222. if (isset($results['errors'])) {
  223. $type = 'warning';
  224. foreach ($results['errors'] as $e) {
  225. drupal_set_message($e->getMessage(), 'error');
  226. }
  227. }
  228. else {
  229. $type = 'status';
  230. }
  231. drupal_set_message($message, $type);
  232. }
  233. else {
  234. // An error occurred. $operations contains the operations that remained unprocessed.
  235. $error_operation = reset($operations);
  236. $message = 'An error occurred while processing ' . $error_operation[0] . ' with arguments:' . print_r($error_operation[0], TRUE);
  237. drupal_set_message($message, 'error');
  238. }
  239. }
  240. /**
  241. * Implements hook_hook_info().
  242. *
  243. * @see cer.api.php for info about what these hooks do.
  244. */
  245. function cer_hook_info() {
  246. return array(
  247. 'cer_fields' => array(
  248. 'group' => 'cer',
  249. ),
  250. 'cer_fields_alter' => array(
  251. 'group' => 'cer',
  252. ),
  253. // This is not used except when rebuilding legacy presets. It's here to
  254. // ensure that MODULE.cer.inc is auto-loaded when loading legacy presets
  255. // from code. (@see cer_update_7005())
  256. 'cer_default_presets' => array(
  257. 'group' => 'cer',
  258. ),
  259. );
  260. }
  261. /**
  262. * Returns options for the cer_weight field.
  263. */
  264. function cer_weight_options() {
  265. $options = array();
  266. for ($i = -50; $i <= 50; $i++) {
  267. $options[$i] = $i;
  268. }
  269. return $options;
  270. }
  271. /**
  272. * Implements hook_features_ignore().
  273. */
  274. function cer_features_ignore($component) {
  275. $ignores = array();
  276. if ($component == 'cer') {
  277. $ignores['wrapper'] = 0;
  278. }
  279. return $ignores;
  280. }
  281. /**
  282. * Implements hook_features_override_ignore().
  283. */
  284. function cer_features_override_ignore($component) {
  285. return cer_features_ignore($component);
  286. }
  287. /**
  288. * Implements hook_entity_property_info().
  289. */
  290. function cer_entity_property_info() {
  291. $properties = array();
  292. foreach (entity_get_info() as $entity_type => $entity_info) {
  293. // Expose a 'cer' struct on every entity type so that we can get special
  294. // entity-specific information during processing. This stuff is wrapped in
  295. // a struct to avoid namespace collisions, which can be disastrous.
  296. //
  297. // @see Issue #2223467
  298. //
  299. $properties[$entity_type]['properties']['cer'] = array(
  300. 'label' => t('CER'),
  301. 'description' => t('Information about the entity, used internally by CER.'),
  302. 'type' => 'struct',
  303. 'getter callback' => 'cer_get_cer_struct',
  304. 'computed' => TRUE,
  305. 'property info' => array(
  306. // lineage is a chain string, in the format used by {cer}.a and {cer}.b.
  307. // e.g., node:article:field_related_pages.
  308. 'lineage' => array(
  309. 'label' => t('Context'),
  310. 'description' => t("The entity's lineage, represented as a string."),
  311. 'type' => 'text',
  312. 'getter callback' => 'cer_get_entity_lineage',
  313. 'computed' => TRUE,
  314. ),
  315. // The depth of the entity. The default callback will just return 1, since most
  316. // entities don't live inside other entities (field collections are the main
  317. // exception).
  318. 'depth' => array(
  319. 'label' => t('Depth'),
  320. 'description' => t("How deeply the entity is embedded."),
  321. 'type' => 'integer',
  322. 'getter callback' => 'cer_get_entity_depth',
  323. 'computed' => TRUE,
  324. ),
  325. // The default callback returns the original entity because, as with the depth
  326. // property, most entities don't live inside other entities.
  327. 'owner' => array(
  328. 'label' => t('Owner'),
  329. 'description' => t('The top-level entity under which this one is embedded.'),
  330. 'type' => 'entity',
  331. 'getter callback' => 'cer_get_entity_owner',
  332. 'computed' => TRUE,
  333. ),
  334. // A wrapper around $entity->original that returns the current entity if there is
  335. // no original version available (i.e., during bulk update).
  336. 'original' => array(
  337. 'label' => t('Original'),
  338. 'description' => t('The original entity (before update), or the current entity if an update has not occurred.'),
  339. 'type' => 'entity',
  340. 'getter callback' => 'cer_get_entity_original',
  341. 'computed' => TRUE,
  342. ),
  343. ),
  344. );
  345. }
  346. return $properties;
  347. }
  348. /**
  349. * Implements hook_entity_property_info_alter().
  350. */
  351. function cer_entity_property_info_alter(array &$info) {
  352. // Add a 'chain' property to the cer_left and cer_right fields so we can
  353. // easily get the field's value represented as a CerFieldChain.
  354. $info['cer']['bundles']['cer']['properties']['cer_left']['property info'] =
  355. $info['cer']['bundles']['cer']['properties']['cer_right']['property info'] = array(
  356. 'chain' => array(
  357. 'label' => t('Chain'),
  358. 'description' => t('A CER field chain to the field instance.'),
  359. 'type' => 'struct',
  360. 'getter callback' => 'cer_unpack_field_chain',
  361. 'computed' => TRUE,
  362. ),
  363. );
  364. // Field collections are special. Because they live inside other entities (to
  365. // potentially infinite levels of recursion), their CER property callbacks must be
  366. // able to recurse upwards through the chain of embedding.
  367. if (module_exists('field_collection')) {
  368. $struct = &$info['field_collection_item']['properties']['cer']['property info'];
  369. $struct['lineage']['getter callback'] = 'cer_get_field_collection_lineage';
  370. $struct['lineage']['raw getter callback'] = 'cer_get_field_collection_lineage_array';
  371. $struct['depth']['getter callback'] = 'cer_get_field_collection_depth';
  372. $struct['owner']['getter callback'] = 'cer_get_field_collection_owner';
  373. }
  374. }
  375. // Include property callback functions
  376. module_load_include('inc', 'cer', 'cer.properties');
  377. if (module_exists('field_collection')) {
  378. module_load_include('inc', 'cer', 'cer.properties.field_collection');
  379. }