Browse Source

updated and patched views_php to fixe the pager bug
https://www.drupal.org/node/2484407

Bachir Soussi Chiadmi 9 years ago
parent
commit
442d89c948

+ 23 - 18
sites/all/modules/contrib/views/views_php/plugins/views/views_php_handler_field.inc

@@ -44,7 +44,7 @@ class views_php_handler_field extends views_handler_field {
     $form += views_php_form_element($this,
       FALSE,
       array('php_value', t('Value code'), t('Code to construct the value of this field.'), FALSE),
-      array('$view', '$handler', '$static', '$row', '$data')
+      array('$view', '$handler', '$static', '$row')
     );
     $form += views_php_form_element($this,
       array('use_php_click_sortable', t('Enable click sort'), t('If checked, you can use PHP code to enable click sort on the field.')),
@@ -112,7 +112,7 @@ class views_php_handler_field extends views_handler_field {
    * @see self::php_post_execute()
    */
   function php_pre_execute() {
-    // Ecexute static PHP code.
+    // Execute static PHP code.
     if (!empty($this->options['php_setup'])) {
       $function = create_function('$view, $handler, &$static', $this->options['php_setup'] . ';');
       ob_start();
@@ -126,16 +126,21 @@ class views_php_handler_field extends views_handler_field {
    * @see views_php_views_post_execute()
    */
   function php_post_execute() {
-    // Ecexute value PHP code.
+    // Execute value PHP code.
     if (!empty($this->options['php_value'])) {
-      $function = create_function('$view, $handler, &$static, $row, $data', $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 stdClass;
         foreach ($this->view->display_handler->get_handlers('field') as $field => $handler) {
-          $normalized_row->$field = isset($row->{$handler->field_alias}) ? $row->{$handler->field_alias} : NULL;
+          // 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, $row);
+        $row->{$this->field_alias} = $function($this->view, $this, $this->php_static_variable, $normalized_row);
       }
       ob_end_clean();
     }
@@ -195,23 +200,23 @@ class views_php_handler_field extends views_handler_field {
   }
 
   /**
-   * Implements views_handler_field#pre_render().
+   * Implements views_handler_field#render().
    */
-  function pre_render(&$values) {
+  function render($values) {
     if (!empty($this->options['php_output'])) {
       $this->php_output_lamda_function = create_function('$view, $handler, &$static, $row, $data, $value', ' ?>' . $this->options['php_output'] . '<?php ');
-    }
-  }
 
-  /**
-   * Implements views_handler_field#render().
-   */
-  function render($values) {
-    // Ecexute output PHP code.
-    if (!empty($this->options['php_output']) && isset($this->php_output_lamda_function)) {
       $normalized_row = new stdClass;
-      foreach ($this->view->display_handler->get_handlers('field') as $field => $handler) {
-        $normalized_row->$field = isset($values->{$handler->field_alias}) ? $values->{$handler->field_alias} : NULL;
+      if (empty($this->view->style_plugin->rendered_fields)) {
+        foreach ($this->view->field as $id => $field) {
+          if ($field->field_alias != $this->field_alias) {
+            $normalized_row->$id = $field->get_value($values);
+          }
+        }
+      } else {
+        foreach ($this->view->style_plugin->rendered_fields{$this->view->row_index} as $field => $value) {
+          $normalized_row->$field = $value;
+        }
       }
 
       $function = $this->php_output_lamda_function;

+ 9 - 7
sites/all/modules/contrib/views/views_php/plugins/views/views_php_handler_filter.inc

@@ -74,19 +74,21 @@ class views_php_handler_filter extends views_handler_filter {
 
   /**
    *
-   * @see views_php_views_post_execute()
+   * @see views_php_plugin_pager::render()
    */
-  function php_post_execute() {
+  function php_pre_render() {
     // Evaluate the PHP code.
     if (!empty($this->options['php_filter'])) {
       $function = create_function('$view, $handler, &$static, $row, $data', $this->options['php_filter'] . ';');
       ob_start();
-      foreach ($this->view->result as $i => $row) {
-        $normalized_row = new stdClass;
-        foreach ($this->view->display_handler->get_handlers('field') as $field => $handler) {
-          $normalized_row->$field = isset($row->{$handler->field_alias}) ? $row->{$handler->field_alias} : NULL;
+
+      $normalized_row = new stdClass;
+      foreach ($this->view->result as $i => $result) {
+        foreach ($this->view->field as $id => $field) {
+          $normalized_row->$id = $this->view->field[$id]->theme($result);
         }
-        if ($function($this->view, $this, $this->php_static_variable, $normalized_row, $row)) {
+
+        if ($function($this->view, $this, $this->php_static_variable, $normalized_row, $result)) {
           unset($this->view->result[$i]);
         }
       }

+ 10 - 9
sites/all/modules/contrib/views/views_php/plugins/views/views_php_plugin_pager.inc

@@ -36,18 +36,19 @@ class views_php_plugin_pager extends views_php_plugin_wrapper  {
         }
       }
     }
-
-    $this->wrapped->total_items = count($this->wrapped->view->result);
-    $this->wrapped->update_page_info();
-
-    $item_per_page = $this->wrapped->get_items_per_page();
-    if ($item_per_page > 0) {
-      $offset = $this->wrapped->get_current_page() * $item_per_page + $this->wrapped->get_offset();
-      $this->wrapped->view->result = array_slice($this->wrapped->view->result, $offset, $item_per_page);
-    }
     $this->wrapped->post_execute($result);
   }
 
+  public function pre_render() {
+    foreach (array(/*'argument',*/ 'field', 'filter', 'sort', /*'relationship'*/) as $type) {
+      foreach ($this->wrapped->view->$type as $id => $handler) {
+        if (is_callable(array($handler, 'php_pre_render'))) {
+          $handler->php_pre_render();
+        }
+      }
+    }
+    $this->php_unwrap();
+  }
 
   /**
    * Execute the count query, which will be done just prior to the query

+ 3 - 1
sites/all/modules/contrib/views/views_php/plugins/views/views_php_plugin_query.inc

@@ -16,6 +16,8 @@ class views_php_plugin_query extends views_php_plugin_wrapper {
 
     $this->wrapped->execute($view);
 
-    $pager->php_unwrap();
+    // We cannot unwrap here because we need to run the pre_render functions.
+    // See: views_php_plugin_pager::pre_render()
+    //$pager->php_unwrap();
   }
 }

+ 24 - 0
sites/all/modules/contrib/views/views_php/views_php-2484407-10.patch

@@ -0,0 +1,24 @@
+diff --git a/plugins/views/views_php_plugin_pager.inc b/plugins/views/views_php_plugin_pager.inc
+index bb7d2d9..5ffa123 100644
+--- a/plugins/views/views_php_plugin_pager.inc
++++ b/plugins/views/views_php_plugin_pager.inc
+@@ -47,19 +47,7 @@
+         }
+       }
+     }
+-    $this->update_wrapped_pager();
+     $this->php_unwrap();
+-  }
+-
+-  protected function update_wrapped_pager() {
+-    $this->wrapped->total_items = count($this->wrapped->view->result);
+-    $this->wrapped->update_page_info();
+-
+-    $item_per_page = $this->wrapped->get_items_per_page();
+-    if ($item_per_page > 0) {
+-      $offset = $this->wrapped->get_current_page() * $item_per_page + $this->wrapped->get_offset();
+-      $this->wrapped->view->result = array_slice($this->wrapped->view->result, $offset, $item_per_page);
+-    }
+   }
+ 
+   /**

+ 3 - 3
sites/all/modules/contrib/views/views_php/views_php.info

@@ -15,9 +15,9 @@ files[] = plugins/views/views_php_plugin_pager.inc
 files[] = plugins/views/views_php_plugin_query.inc
 files[] = plugins/views/views_php_plugin_wrapper.inc
 
-; Information added by Drupal.org packaging script on 2014-05-08
-version = "7.x-1.0-alpha1+2-dev"
+; Information added by Drupal.org packaging script on 2015-04-21
+version = "7.x-2.x-dev"
 core = "7.x"
 project = "views_php"
-datestamp = "1399543127"
+datestamp = "1429645983"
 

+ 12 - 3
sites/all/modules/contrib/views/views_php/views_php.module

@@ -47,8 +47,8 @@ function views_php_form_element($handler, $checkbox = FALSE, $input, $variables
       '$handler' => t('The handler object.'),
       '$plugin'  => t('The plugin object.'),
       '$static'  => t('A variable that can be used to store reusable data per row.'),
-      '$row'     => t('Contains the retrieved record from the database (e.g. $data->nid).'),
-      '$data'    => t('Contains the retrieved record from the database (e.g. $data->nid).'),
+      '$row'     => t('Contains the retrieved record from the database per row.'),
+      '$data'    => t('Contains the retrieved record from the database for all rows.'),
       '$results' => t('Array containing the view\'s result.'),
       '$cache'   => t('The cache object.'),
     );
@@ -102,7 +102,16 @@ function views_php_form_element($handler, $checkbox = FALSE, $input, $variables
     $items[] = l($variable_name, '', array('fragment' => $container[$name]['#id'], 'external' => TRUE)) . ': ' . $description;
 
     if (strpos($variable_name, '$row') === 0) {
+      $php_value = ($input[0] == 'php_value') ? true : false;
+
       foreach ($handler->view->display_handler->get_handlers('field') as $field => $field_handler) {
+        // Do not add fields that will not have data when evaluating the value code. 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 ($php_value && $field_handler->table != $field_handler->view->base_table) {
+          continue;
+        }
+
         $items[] = l($variable_name . '->' . $field, '', array('fragment' => $container[$name]['#id'], 'external' => TRUE)) . ': ' . $field_handler->ui_name();
       }
     }
@@ -142,7 +151,7 @@ function views_php_views_pre_execute($view) {
 /**
  * Implements hook_views_post_execute().
  */
-function views_php_views_post_execute($view) {
+function views_php_views_post_render($view) {
   // Restore original query plugin if it was wrapped.
   if ($view->query instanceof views_php_plugin_wrapper) {
     $view->query->php_unwrap();