entity_from_schema.inc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * @file
  4. * Plugin to provide an relationship handler for an entity from a field.
  5. */
  6. /**
  7. * Plugins are described by creating a $plugin array which will be used
  8. * by the system that includes this file.
  9. */
  10. $plugin = array(
  11. 'title' => t('Entity'),
  12. 'description' => t('Creates an entity context from a foreign key on a field.'),
  13. 'context' => 'ctools_entity_from_schema_context',
  14. 'get child' => 'ctools_entity_from_schema_get_child',
  15. 'get children' => 'ctools_entity_from_schema_get_children',
  16. );
  17. function ctools_entity_from_schema_get_child($plugin, $parent, $child) {
  18. $plugins = ctools_entity_from_schema_get_children($plugin, $parent);
  19. return $plugins[$parent . ':' . $child];
  20. }
  21. function ctools_entity_from_schema_get_children($parent_plugin, $parent) {
  22. $entities = entity_get_info();
  23. $plugins = array();
  24. foreach (module_implements('entity_info') as $module) {
  25. module_load_install($module);
  26. $schemas = drupal_get_schema();
  27. foreach ($entities as $from_entity => $from_entity_info) {
  28. if (empty($from_entity_info['base table'])) {
  29. continue;
  30. }
  31. $table = $from_entity_info['base table'];
  32. if (isset($schemas[$table]['foreign keys'])) {
  33. foreach ($schemas[$table]['foreign keys'] as $relationship => $info) {
  34. foreach ($entities as $to_entity => $to_entity_info) {
  35. if (empty($info['table'])) {
  36. continue;
  37. }
  38. if (isset($to_entity_info['base table']) && $info['table'] == $to_entity_info['base table'] && in_array($to_entity_info['entity keys']['id'], $info['columns'])) {
  39. $this_col = ctools_entity_from_schema_columns_filter($info['columns'], $to_entity_info['entity keys']['id']);
  40. // Set up our t() replacements as we reuse these.
  41. $replacements = array(
  42. '@relationship' => $relationship,
  43. '@base_table' => $table,
  44. '@to_entity' => $to_entity_info['label'],
  45. '@from_entity' => $from_entity_info['label'],
  46. );
  47. $name = $this_col . '-' . $from_entity . '-' . $to_entity;
  48. $plugin_id = $parent . ':' . $name;
  49. $plugin = $parent_plugin;
  50. $plugin['title'] = t('@to_entity from @from_entity (on @base_table.@relationship)', $replacements);
  51. $plugin['keyword'] = $to_entity;
  52. $plugin['context name'] = $name;
  53. $plugin['name'] = $plugin_id;
  54. $plugin['description'] = t('Builds a relationship from a @from_entity to a @to_entity using the @base_table.@relationship field.', $replacements);
  55. $plugin['required context'] = new ctools_context_required($from_entity_info['label'], $from_entity);
  56. $plugin_entities = array('to' => $to_entity_info, 'from' => $from_entity_info);
  57. $plugin_entities = array('to' => array($to_entity => $to_entity_info), 'from' => array($from_entity => $from_entity_info));
  58. drupal_alter('ctools_entity_context', $plugin, $plugin_entities, $plugin_id);
  59. $plugins[$plugin_id] = $plugin;
  60. // Add the relation in the reverse direction.
  61. $name = $this_col . '-' . $to_entity . '-' . $from_entity;
  62. $plugin_id = $parent . ':' . $name;
  63. $plugin = $parent_plugin;
  64. $plugin['title'] = t('@from_entity from @to_entity (on @base_table.@relationship)', $replacements);
  65. $plugin['keyword'] = $to_entity;
  66. $plugin['context name'] = $name;
  67. $plugin['name'] = $plugin_id;
  68. $plugin['description'] = t('Builds a relationship from a @to_entity to a @from_entity using the @base_table.@relationship field.', $replacements);
  69. $plugin['required context'] = new ctools_context_required($to_entity_info['label'], $to_entity);
  70. $plugin_entities = array('to' => $from_entity_info, 'from' => $to_entity_info);
  71. $plugin_entities = array('to' => array($from_entity => $from_entity_info), 'from' => array($to_entity => $to_entity_info));
  72. drupal_alter('ctools_entity_context', $plugin, $plugin_entities, $plugin_id);
  73. $plugins[$plugin_id] = $plugin;
  74. }
  75. }
  76. }
  77. }
  78. }
  79. }
  80. drupal_alter('ctools_entity_contexts', $plugins);
  81. return $plugins;
  82. }
  83. function ctools_entity_from_schema_columns_filter($columns, $value) {
  84. foreach ($columns as $this_col => $that_col) {
  85. if ($value == $that_col) {
  86. return $this_col;
  87. }
  88. }
  89. }
  90. /**
  91. * Return a new context based on an existing context.
  92. */
  93. function ctools_entity_from_schema_context($context, $conf) {
  94. $plugin = $conf['name'];
  95. list($plugin, $plugin_name) = explode(':', $plugin);
  96. list($this_col, $from_entity, $to_entity) = explode('-', $plugin_name);
  97. // If unset it wants a generic, unfilled context, which is just NULL.
  98. $entity_info = entity_get_info($from_entity);
  99. if (empty($context->data) || !isset($context->data->{$entity_info['entity keys']['id']})) {
  100. return ctools_context_create_empty('entity:' . $to_entity, NULL);
  101. }
  102. if (isset($context->data->{$entity_info['entity keys']['id']})) {
  103. // Load the entity.
  104. $id = $context->data->{$entity_info['entity keys']['id']};
  105. $entity = entity_load($from_entity, array($id));
  106. $entity = $entity[$id];
  107. if (isset($entity->$this_col)) {
  108. $to_entity_id = $entity->$this_col;
  109. // Send it to ctools.
  110. return ctools_context_create('entity:' . $to_entity, $to_entity_id);
  111. }
  112. }
  113. }