You are here

public function ViewsPhp::phpPostExecute in Views PHP 8

Same name in this branch
  1. 8 src/Plugin/views/filter/ViewsPhp.php \Drupal\views_php\Plugin\views\filter\ViewsPhp::phpPostExecute()
  2. 8 src/Plugin/views/sort/ViewsPhp.php \Drupal\views_php\Plugin\views\sort\ViewsPhp::phpPostExecute()
  3. 8 src/Plugin/views/field/ViewsPhp.php \Drupal\views_php\Plugin\views\field\ViewsPhp::phpPostExecute()

See also

views_php_views_post_execute()

File

src/Plugin/views/field/ViewsPhp.php, line 179
Contains \Drupal\views_php\Plugin\views\field\ViewsPhp.

Class

ViewsPhp
A handler to provide a field that is constructed by the administrator using PHP.

Namespace

Drupal\views_php\Plugin\views\field

Code

public function phpPostExecute(&$values) {

  // Execute value PHP code.
  if (!empty($this->options['php_value'])) {
    $function = create_function('$view, $handler, &$static, $row', $this->options['php_value'] . ';');
    ob_start();
    foreach ($this->view->result as $i => &$row) {
      $normalized_row = new ViewsPhpNormalizedRow();
      foreach ($this->view->display_handler
        ->getHandlers('field') as $field => $handler) {

        // Do not add our own field. Also, do not add other fields that have no data yet. This occurs because
        // the value code is evaluated in hook_views_post_execute(), but field data is made available in hook_views_pre_render(),
        // which is called after hook_views_post_execute().
        if ((empty($handler->aliases) || empty($handler->aliases['entity_type'])) && $handler->field_alias != $this->field_alias) {
          $normalized_row->{$field} = isset($row->{$handler->field_alias}) ? $row->{$handler->field_alias} : NULL;
        }
      }
      $row->{$this->field_alias} = $function($this->view, $this, $this->php_static_variable, $normalized_row);
    }
    ob_end_clean();
  }

  // If we're sorting, do the actual sorting then fix the results as per the pager info.
  if (!empty($this->options['use_php_click_sortable']) && !empty($this->php_click_sort_order)) {
    if ($this->options['use_php_click_sortable'] == self::CLICK_SORT_PHP) {
      if (!empty($this->options['php_click_sortable'])) {
        $this->php_click_sort_function = create_function('$view, $handler, &$static, $row1, $row2', $this->options['php_click_sortable'] . ';');
      }
    }
    else {
      $predefined = array(
        self::CLICK_SORT_NUMERIC => array(
          $this,
          'phpClickSortNumeric',
        ),
        self::CLICK_SORT_ALPHA => 'strcasecmp',
        self::CLICK_SORT_ALPHA_CASE => 'strcmp',
        self::CLICK_SORT_NAT => 'strnatcasecmp',
        self::CLICK_SORT_NAT_CASE => 'strnatcmp',
      );
      $this->php_click_sort_function = $predefined[$this->options['use_php_click_sortable']];
    }
    if (isset($this->php_click_sort_function)) {
      usort($this->view->result, array(
        $this,
        'phpClickSort',
      ));
    }
  }
}