view_from_argument.inc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * @file
  4. * Plugin to provide an relationship handler for a view by argument input settings.
  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('View From Argument'),
  12. 'description' => t('Creates a view context from argument input settings.'),
  13. 'context' => 'views_content_view_from_argument_context',
  14. 'get child' => 'views_content_view_from_argument_get_child',
  15. 'get children' => 'views_content_view_from_argument_get_children',
  16. );
  17. function views_content_view_from_argument_get_child($plugin, $parent, $child) {
  18. list($name, $id) = explode('-', $child, 2);
  19. $view = views_get_view($name);
  20. if (!$view) {
  21. return;
  22. }
  23. $view->set_display($id);
  24. if ($view->current_display != $id) {
  25. return;
  26. }
  27. $info = _views_content_get_context_from_display($view, $id, $parent, TRUE);
  28. if ($info) {
  29. return $info;
  30. }
  31. return;
  32. }
  33. function views_content_view_from_argument_get_children($plugin, $parent) {
  34. $types = array();
  35. $views = views_get_applicable_views('returns context');
  36. foreach ($views as $data) {
  37. list($view, $id) = $data;
  38. $info = _views_content_get_context_from_display($view, $id, $parent, TRUE);
  39. if ($info) {
  40. $types[$info['name']] = $info;
  41. }
  42. }
  43. return $types;
  44. }
  45. /**
  46. * Return a new context based on an existing context.
  47. */
  48. function views_content_view_from_argument_context($contexts, $conf) {
  49. $name = $conf['name'];
  50. list($plugin, $view_data) = explode(':', $name);
  51. list($view_name, $display_id) = explode('-', $view_data);
  52. $keys = array_keys($conf['context']);
  53. if (empty($contexts[$keys[0]]->data)) {
  54. return ctools_context_create_empty('view', NULL);
  55. }
  56. // Load our view up.
  57. $data = views_get_view($view_name);
  58. if ($data) {
  59. // Set the display.
  60. $data->set_display($display_id);
  61. $context_keys = array_keys($contexts);
  62. foreach ($data->display_handler->get_argument_input() as $id => $argument) {
  63. if ($argument['type'] == 'context') {
  64. $key = array_shift($context_keys);
  65. if (isset($contexts[$key])) {
  66. if (strpos($argument['context'], '.')) {
  67. list($context, $converter) = explode('.', $argument['context'], 2);
  68. $args[] = ctools_context_convert_context($contexts[$key], $converter, array('sanitize' => FALSE));
  69. }
  70. else {
  71. $args[] = $contexts[$key]->argument;
  72. }
  73. }
  74. }
  75. }
  76. // Remove any trailing NULL arguments as these are non-args:
  77. while (count($args) && end($args) === NULL) {
  78. array_pop($args);
  79. }
  80. $data->set_arguments($args);
  81. if ($path = $data->display_handler->get_option('inherit_panels_path')) {
  82. $data->override_path = $_GET['q'];
  83. }
  84. }
  85. if (isset($contexts[$keys[0]]->data)) {
  86. return ctools_context_create('view', $data);
  87. }
  88. }