view.inc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * @file
  4. *
  5. * Plugin to provide a node context. A node context is a node wrapped in a
  6. * context object that can be utilized by anything that accepts contexts.
  7. */
  8. /**
  9. * Plugins are described by creating a $plugin array which will be used
  10. * by the system that includes this file.
  11. */
  12. $plugin = array(
  13. 'title' => t("View"),
  14. 'description' => t('Loads a view result into a context that can then be displayed across a panel or turned into other contexts.'),
  15. 'context' => 'views_content_context_view_create',
  16. 'edit form' => 'views_content_context_view_settings_form',
  17. 'edit form validate' => 'views_content_context_view_settings_form_validate',
  18. 'edit form submit' => 'views_content_context_view_settings_form_submit',
  19. 'defaults' => array('view' => ''),
  20. 'keyword' => 'view',
  21. 'context name' => 'view',
  22. 'get child' => 'views_content_context_view_get_child',
  23. 'get children' => 'views_content_context_view_get_children',
  24. );
  25. function views_content_context_view_get_child($plugin, $parent, $child) {
  26. list($name, $id) = explode('-', $child, 2);
  27. $view = views_get_view($name);
  28. if (!$view) {
  29. return;
  30. }
  31. $view->set_display($id);
  32. if ($view->current_display != $id) {
  33. return;
  34. }
  35. $info = _views_content_get_context_from_display($view, $id, $parent, FALSE);
  36. if ($info) {
  37. return $info;
  38. }
  39. return;
  40. }
  41. function views_content_context_view_get_children($plugin, $parent) {
  42. $types = array(
  43. 'view' => $plugin,
  44. );
  45. // We're keeping the 'view' context around for legacy reasons but
  46. // we want to disable the UI so you can't add it that way anymore.
  47. $types['view']['no ui'] = TRUE;
  48. $views = views_get_applicable_views('returns context');
  49. foreach ($views as $data) {
  50. list($view, $id) = $data;
  51. $info = _views_content_get_context_from_display($view, $id, $parent, FALSE);
  52. if ($info) {
  53. $info['no required context ui'] = TRUE;
  54. $types[$info['name']] = $info;
  55. }
  56. }
  57. return $types;
  58. }
  59. function views_content_context_view_create($empty, $data = NULL, $conf = FALSE, $plugin = array()) {
  60. $context = new ctools_context('view');
  61. $context->plugin = 'view';
  62. if ($empty) {
  63. return $context;
  64. }
  65. if ($conf) {
  66. if (is_array($data) && !empty($data['view'])) {
  67. // This code is left in for backward compatibility. Will not be used
  68. // with child plugins.
  69. list($name, $display_id) = explode(':', $data['view'], 2);
  70. $data = views_get_view($name);
  71. if ($data) {
  72. $data->set_display($display_id);
  73. }
  74. }
  75. else if (!empty($plugin['view name'])) {
  76. $data = views_get_view($plugin['view name']);
  77. $data->set_display($plugin['view display id']);
  78. }
  79. }
  80. if (is_object($data) && $data->current_display != 'default') {
  81. // We don't store the loaded view as we don't want the view object
  82. // cached. However, in order to extract it you can use:
  83. // @code
  84. // $output = views_content_context_get_output($context);
  85. // $view = $output['view'];
  86. // @endcode
  87. $context->data = array(
  88. 'name' => $data->name,
  89. 'display' => $data->current_display,
  90. 'args' => $data->args,
  91. );
  92. // At runtime, this can get populated. Once it is populated this
  93. // object should not be cached.
  94. $context->view = NULL;
  95. $context->title = $data->get_title();
  96. $context->argument = $data->name . ':' . $data->current_display;
  97. $context->restrictions['base'] = array($data->base_table);
  98. return $context;
  99. }
  100. }
  101. function views_content_context_view_settings_form($form, &$form_state) {
  102. $conf = $form_state['conf'];
  103. $views = views_get_applicable_views('returns context');
  104. foreach ($views as $data) {
  105. list($view, $id) = $data;
  106. $title = views_content_get_display_title($view, $id, 'admin_title');
  107. $options[$view->name . ':' . $id] = $title;
  108. }
  109. if (!empty($options)) {
  110. natcasesort($options);
  111. $form['view'] = array(
  112. '#type' => 'select',
  113. '#options' => $options,
  114. '#title' => t('View'),
  115. );
  116. }
  117. else {
  118. $form['view'] = array(
  119. '#value' => '<p>' . t('There are currently no views with Context displays enabled. You should go to the view administration and add a Context display to use a view as a context.') . '</p>',
  120. );
  121. }
  122. return $form;
  123. }
  124. /**
  125. * Validate a node.
  126. */
  127. function views_content_context_view_settings_form_validate($form, &$form_state) {
  128. if (empty($form_state['values']['view'])) {
  129. form_error($form['view'], t('You must select a view.'));
  130. }
  131. }
  132. /**
  133. * Provide a list of ways that this context can be converted to a string.
  134. */
  135. function views_content_context_view_convert_list() {
  136. $list = array(
  137. );
  138. return $list;
  139. }
  140. /**
  141. * Convert a context into a string.
  142. */
  143. function views_content_context_view_convert($context, $type) {
  144. switch ($type) {
  145. }
  146. }