Skip to content

Instantly share code, notes, and snippets.

@damiankloip
Created February 13, 2013 12:23
Show Gist options
  • Save damiankloip/4944226 to your computer and use it in GitHub Desktop.
Save damiankloip/4944226 to your computer and use it in GitHub Desktop.
diff --git a/lib/Drupal/views_field_view/Plugin/views/field/View.php b/lib/Drupal/views_field_view/Plugin/views/field/View.php
index 567667d..e3b0833 100644
--- a/lib/Drupal/views_field_view/Plugin/views/field/View.php
+++ b/lib/Drupal/views_field_view/Plugin/views/field/View.php
@@ -23,6 +23,34 @@ use Drupal\views\Plugin\views\field\FieldPluginBase;
class View extends FieldPluginBase {
/**
+ * Keeps track of which views are currently being run.
+ *
+ * @var array
+ */
+ protected static $running = array();
+
+ /**
+ * The child view to use when rendering the field.
+ *
+ * @var \Drupal\views\ViewExecutable
+ */
+ protected $childView;
+
+ /**
+ * Whether access to the current view display is allowed.
+ *
+ * @var bool
+ */
+ protected $access;
+
+ /**
+ * Recursion flag.
+ *
+ * @var bool
+ */
+ protected $recursion = FALSE;
+
+ /**
* Overrides \Drupal\views\Plugin\views\field\FieldPluginBase::click_sortable().
*/
public function click_sortable() {
@@ -180,69 +208,76 @@ class View extends FieldPluginBase {
}
/**
+ * Overrides \Drupal\views\Plugin\views\field\FieldPluginBase::pre_render().
+ */
+ public function pre_render(&$values) {
+ // Protect against the evil / recursion.
+ // Set the variable for yourself, this is not for the normal "user".
+ if (!empty($this->options['view']) && (empty(static::$running[$this->options['view']][$this->options['display']]) || variable_get('views_field_view_evil', FALSE))) {
+ $this->childView = views_get_view($this->options['view']);
+ $this->access = $this->childView->access($this->options['display']);
+ }
+ else {
+ $this->recursion = TRUE;
+ }
+ }
+
+ /**
* Overrides \Drupal\views\Plugin\views\field\FieldPluginBase::render().
*/
public function render($values) {
$output = NULL;
- static $running = array();
- // Protect against the evil / recursion.
- // Set the variable for yourself, this is not for the normal "user".
- if (empty($running[$this->options['view']][$this->options['display']]) || variable_get('views_field_view_evil', FALSE)) {
- if (!empty($this->options['view'])) {
- $running[$this->options['view']][$this->options['display']] = TRUE;
- $args = array();
-
- // Only perform this loop if there are actually arguments present.
- if (!empty($this->options['arguments'])) {
- // Create array of tokens.
- foreach ($this->splitTokens($this->options['arguments']) as $token) {
- $args[] = $this->getTokenValue($token, $values, $this->view);
- }
+ if (!empty($this->childView) && $this->access) {
+ static::$running[$this->options['view']][$this->options['display']] = TRUE;
+
+ $args = array();
+ // Only perform this loop if there are actually arguments present.
+ if (!empty($this->options['arguments'])) {
+ // Create array of tokens.
+ foreach ($this->splitTokens($this->options['arguments']) as $token) {
+ $args[] = $this->getTokenValue($token, $values, $this->view);
}
+ }
- // get view etc… and execute.
- $view = views_get_view($this->options['view']);
-
- // Only execute and render the view if the user has access.
- if ($view->access($this->options['display'])) {
- $view->setDisplay($this->options['display']);
-
- if ($view->display_handler->isPagerEnabled()) {
- // Check whether the pager IDs should be rewritten.
- $view->initQuery();
- // Find a proper start value for the ascening pager IDs.
- $start = 0;
- $pager = $view->display_handler->getOption('pager');
- if (isset($this->query->pager->options['id'])) {
- $start = (int) $this->query->pager->options['id'];
- }
-
- // Set the pager ID before initializing the pager, so
- // views_plugin_pager::set_current_page works as expected, which is
- // called from view::init_pager()
- $pager['options']['id'] = $start + 1 + $this->view->row_index;
- $view->display_handler->setOption('pager', $pager);
- $view->initPager();
- }
+ $view = $this->childView;
- $view->preExecute($args);
- $view->execute();
+ $view->setDisplay($this->options['display']);
- // If there are no results and hide_empty is set.
- if (empty($view->result) && $this->options['hide_empty']) {
- $output = '';
- }
- // Else just call render on the view object.
- else {
- $output = $view->render();
- }
+ if ($view->display_handler->isPagerEnabled()) {
+ // Check whether the pager IDs should be rewritten.
+ $view->initQuery();
+ // Find a proper start value for the ascening pager IDs.
+ $start = 0;
+ $pager = $view->display_handler->getOption('pager');
+ if (isset($this->query->pager->options['id'])) {
+ $start = (int) $this->query->pager->options['id'];
}
- $running[$this->options['view']][$this->options['display']] = FALSE;
+ // Set the pager ID before initializing the pager, so
+ // views_plugin_pager::set_current_page works as expected, which is
+ // called from view::init_pager()
+ $pager['options']['id'] = $start + 1 + $this->view->row_index;
+ $view->display_handler->setOption('pager', $pager);
+ $view->initPager();
}
+
+ $view->preExecute($args);
+ $view->execute();
+
+ // If there are no results and hide_empty is set.
+ if (empty($view->result) && $this->options['hide_empty']) {
+ $output = '';
+ }
+ // Else just call render on the view object.
+ else {
+ $view->initStyle();
+ $output = $view->render();
+ }
+
+ static::$running[$this->options['view']][$this->options['display']] = FALSE;
}
- else {
+ elseif ($this->recursion) {
$output = t('Recursion, stop!');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment