You are here

public function View::render in Views Field View 8

Renders the field.

Parameters

\Drupal\views\ResultRow $values: The values retrieved from a single row of a view's query result.

Return value

string|\Drupal\Component\Render\MarkupInterface The rendered output. If the output is safe it will be wrapped in an object that implements MarkupInterface. If it is empty or unsafe it will be a string.

Overrides FieldPluginBase::render

File

src/Plugin/views/field/View.php, line 213

Class

View
Plugin annotation @ViewsField("view");

Namespace

Drupal\views_field_view\Plugin\views\field

Code

public function render(ResultRow $values) {
  $output = NULL;
  static $running = [];

  // 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']]) || $this->config
    ->get('evil')) {
    if (!empty($this->options['view'])) {
      $running[$this->options['view']][$this->options['display']] = TRUE;
      $args = [];

      // 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 and execute.
      $view = Views::getView($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 ascending 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
          ->preExecute($args);
        $view
          ->execute();

        // If there are no results and hide_empty is set.
        if (empty($view->result) && $this->options['hide_empty']) {
          $output = '';
        }
        else {
          $output = $view
            ->render();
        }
      }
      $running[$this->options['view']][$this->options['display']] = FALSE;
    }
  }
  else {
    $output = $this
      ->t('Recursion, stop!');
  }
  if (!empty($output)) {

    // Add the rendered output back to the $values object
    // so it is available in $view->result objects.
    $values->{'views_field_view_' . $this->options['id']} = $output;
  }
  return $output;
}