Guest User

Untitled

a guest
Nov 20th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. // Attempt to fetch the entity that is being viewed via a backtrace to the
  2. // field_attach_view($entity_type, $entity) function and parameters §if found.
  3. $backtrace = debug_backtrace();
  4. foreach ($backtrace as $caller) {
  5. if ($caller['function'] == 'field_attach_view') {
  6. $entity_type = $caller['args'][0];
  7. $entity = $caller['args'][2];
  8. // do stuff with entity
  9. break;
  10. }
  11. }
  12.  
  13. function($entity, $entity_type)
  14.  
  15. function MODULE_entity_load($entities, $entity_type) {
  16. foreach ($entities as $entity) {
  17. $entity->entity_type = $entity_type;
  18. }
  19. }
  20.  
  21. function entity_by_path($path) {
  22. $router_item = menu_get_item($path);
  23.  
  24. $ent_sug_array = array();
  25. foreach ($router_item['map'] as $key => $value) {
  26. if(is_object($value) && isset($value->type)) {
  27. $ent_sug_array[$key] = $value;
  28. }
  29. }
  30.  
  31. $ent_sug = false;
  32. if(count($ent_sug_array) > 1) {
  33. $lf_sug = false;
  34. if(isset($router_item['load_functions'])) {
  35. if(count($router_item['load_functions']) == 1) {
  36. reset($router_item['load_functions']);
  37. $lf_sug = key($router_item['load_functions']);
  38. }
  39. }
  40. if($lf_sug !== false) {
  41. $ent_sug = is_object($router_item['map'][$lg_sug] && isset($router_item['map'][$lg_sug]->type)) ? $router_item['map'][$lg_sug] : $ent_sug;
  42. }
  43. } elseif(count($ent_sug_array) == 1) {
  44. $ent_sug = reset($ent_sug_array);
  45. }
  46.  
  47. if(!empty($ent_sug)) {
  48. $bundle = $ent_sug->type;
  49. $ent_type = method_exists($ent_sug, 'entityType') ? $ent_sug->entityType() : false; //eck case
  50. if(empty($ent_type)) {
  51. $aclass = get_class($ent_sug);
  52. if($aclass == 'stdClass') {
  53. //node, user, core entity case
  54. if(isset($router_item['load_functions'])) {
  55. $lf = reset($router_item['load_functions']);
  56. $ei = entity_get_info();
  57. foreach ($ei as $key => $value) {
  58. if(empty('load hook'))
  59. continue;
  60. if($lf == $value['load hook']) {
  61. $ent_type = $key;
  62. break;
  63. }
  64. }
  65. }
  66. }else{
  67. //custom entity case
  68. $ent_type = $aclass;
  69. }
  70. }
  71.  
  72. return array('entity'=>$ent_sug, 'type'=>$ent_type, 'bundle'=>$bundle);
  73. }
  74.  
  75. return false;
  76. }
  77.  
  78. $item = menu_get_item($_GET['internal_path']);
  79. if (empty($item['access']) || empty($item['page_arguments'][0]) || empty($item['load_functions'])) {
  80. return FALSE;
  81. }
  82.  
  83. $entity = $item['page_arguments'][0];
  84.  
  85. // Get entity-type.
  86. $load_function = reset($item['load_functions']);
  87. $entity_types = entity_get_info();
  88. $entity_type = '';
  89. foreach ($entity_types as $type => $type_definition) {
  90. if ($load_function == $type_definition['load hook']) {
  91. // When the load_function from menu_get_item matches "hook load" of the
  92. // entity type definition, then the entity_type was found.
  93. $entity_type = $type;
  94. break;
  95. }
  96. }
  97. if (!$entity_type) {
  98. return FALSE;
  99. }
Add Comment
Please, Sign In to add comment