cer.cer.inc 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /**
  3. * @file
  4. * Contains CER hook implementations.
  5. */
  6. /**
  7. * Implements hook_cer_fields().
  8. */
  9. function cer_cer_fields() {
  10. $fields = array();
  11. if (module_exists('taxonomy')) {
  12. $fields = array_merge($fields, _cer_collect_fields_of_type('taxonomy_term_reference', 'CerTaxonomyTermReferenceField'));
  13. }
  14. if (module_exists('entityreference')) {
  15. $fields = array_merge($fields, _cer_collect_fields_of_type('entityreference', 'CerEntityReferenceField'));
  16. }
  17. if (module_exists('node_reference')) {
  18. $fields = array_merge($fields, _cer_collect_fields_of_type('node_reference', 'CerNodeReferenceField'));
  19. }
  20. if (module_exists('user_reference')) {
  21. $fields = array_merge($fields, _cer_collect_fields_of_type('user_reference', 'CerUserReferenceField'));
  22. }
  23. if (module_exists('field_collection')) {
  24. $fields = array_merge($fields, _cer_collect_fields_of_type('field_collection', 'CerFieldCollectionField'));
  25. }
  26. return $fields;
  27. }
  28. /**
  29. * Implmements hook_cer_fields_alter().
  30. */
  31. function cer_cer_fields_alter(array &$fields) {
  32. foreach (array_keys($fields) as $identifier) {
  33. list ($entity_type, $bundle, $field) = explode(':', $identifier);
  34. if ($entity_type == 'field_collection_item') {
  35. $result = db_query("SELECT entity_type, bundle, field_name FROM {field_config_instance} WHERE field_name = :field_collection", array(':field_collection' => $bundle));
  36. foreach ($result as $r) {
  37. $fields[$identifier]['parents'][] = "{$r->entity_type}:{$r->bundle}:{$r->field_name}";
  38. }
  39. $fields[$identifier]['require parent'] = TRUE;
  40. }
  41. }
  42. }
  43. function _cer_collect_fields_of_type($field_type, $class) {
  44. $fields = array();
  45. $result = db_query("SELECT fci.entity_type, fci.bundle, fci.field_name FROM {field_config_instance} fci INNER JOIN {field_config} fc ON fc.id = fci.field_id WHERE fc.type = :type AND fc.deleted = 0 AND fci.deleted = 0", array(':type' => $field_type));
  46. foreach ($result as $r) {
  47. $fields["{$r->entity_type}:{$r->bundle}:{$r->field_name}"]['class'] = $class;
  48. }
  49. return $fields;
  50. }