cer.install 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /**
  3. * @file
  4. * Install file providing corresponding entity reference schema.
  5. */
  6. /**
  7. * Implements hook_schema().
  8. */
  9. function cer_schema() {
  10. $schema['cer'] = array(
  11. 'description' => t('Saves the content types and entity reference fields for which the corresponding entity reference is enabled'),
  12. 'fields' => array(
  13. 'entity_types_content_fields' => array('type' => 'varchar', 'length' => 200, 'not null' => TRUE, 'default' => ''),
  14. 'enabled' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
  15. ),
  16. 'primary key' => array('entity_types_content_fields'),
  17. 'export' => array(
  18. 'key' => 'entity_types_content_fields',
  19. 'status' => 'enabled',
  20. 'primary key' => 'entity_types_content_fields',
  21. 'key name' => 'Corresponding entity reference',
  22. 'identifier' => 'cnr_obj',
  23. 'api' => array(
  24. 'api' => 'default_cer_presets',
  25. 'owner' => 'cer',
  26. 'minimum_version' => 1,
  27. 'current_version' => 1,
  28. ),
  29. ),
  30. );
  31. return $schema;
  32. }
  33. /**
  34. * Rename table to shorten module name.
  35. */
  36. function cer_update_7001() {
  37. db_rename_table('corresponding_entity_references', 'cer');
  38. }
  39. /**
  40. * Disable presets which refer to fields that don't exist. (Issue #2122531)
  41. */
  42. function cer_update_7002() {
  43. $presets = db_query('SELECT entity_types_content_fields FROM {cer} WHERE 1')->fetchCol();
  44. foreach ($presets as $preset) {
  45. $keys = explode('*', $preset);
  46. $left = field_info_instance($keys[0], $keys[2], $keys[1]);
  47. $right = field_info_instance($keys[3], $keys[5], $keys[4]);
  48. if (empty($left) || empty($right)) {
  49. db_query('UPDATE {cer} SET enabled = 0 WHERE entity_types_content_fields = :preset', array(':preset' => $preset));
  50. drupal_set_message(t('CER preset %preset was disabled because it uses non-existent fields.', array('%preset' => $preset)), 'warning');
  51. }
  52. }
  53. }