Function create_function() is deprecated Drupal 7

When updating the php version to say 7.2 - 7.3, on Drupal 7, all kinds of errors of various modules can periodically pop up. This is due to the fact that the module code may contain obsolete functions and variables that in newer versions of php either no longer work or will soon stop working. And even if the code is still working, but errors glare at the eyes of users, and turning off their output is not a good thing. Therefore, you have to periodically manually edit the code. Sometimes the simplest things are replacement = ''; on = []; or writing the names of variables / arrays in single quotes, which was not previously.

However, sometimes there are more complicated options, such as the "Function create_function() is deprecated" error referring to the files of the views_php module.

Fortunately, similar questions surfaced on Drupal.org and a solution was found, in the form of applying a patch, specifically for this case (views_php module).

Link to patch - link.

For people who do not know how to install patches or perverts who love to pick with pens, I post the text of the patch, in which, in general, everything is simple - first it is indicated that the file in which editing / replacement is in progress, then where the minus symbol is - this line is deleted, where the plus symbol - this line is vice versa is added to the file.

diff -ur /home/rgo_main2013/home/rgo_main2013/patches/views_php/views_php/plugins/views/views_php_handler_area.inc /home/rgo_main2013/public_html/sites/all/modules/contrib/views_php/plugins/views/views_php_handler_area.inc
--- a/plugins/views/views_php_handler_area.inc	2015-11-12 15:51:38.000000000 +0300
+++ b/plugins/views/views_php_handler_area.inc	2018-12-07 14:25:25.151928000 +0300
@@ -34,9 +34,9 @@
    * Implements views_handler_area#render().
    */
   function render($empty = FALSE) {
-    // Ecexute output PHP code.
+    // Execute output PHP code.
     if ((!$empty || !empty($this->options['empty'])) && !empty($this->options['php_output'])) {
-      $function = create_function('$view, $handler, $results', ' ?>' . $this->options['php_output'] . '<?php ');
+      $function = views_php_create_function('$view, $handler, $results', ' ?>' . $this->options['php_output'] . '<?php ');
       ob_start();
       $function($this->view, $this, $this->view->result);
       return ob_get_clean();
diff -ur /home/rgo_main2013/home/rgo_main2013/patches/views_php/views_php/plugins/views/views_php_handler_field.inc /home/rgo_main2013/public_html/sites/all/modules/contrib/views_php/plugins/views/views_php_handler_field.inc
--- a/plugins/views/views_php_handler_field.inc	2015-11-12 15:51:38.000000000 +0300
+++ b/plugins/views/views_php_handler_field.inc	2018-12-07 14:38:47.793261000 +0300
@@ -112,9 +112,9 @@
    * @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'] . ';');
+      $function = views_php_create_function('$view, $handler, &$static', $this->options['php_setup'] . ';');
       ob_start();
       $function($this->view, $this, $this->php_static_variable);
       ob_end_clean();
@@ -126,9 +126,9 @@
    * @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 = views_php_create_function('$view, $handler, &$static, $row, $data', $this->options['php_value'] . ';');
       ob_start();
       foreach ($this->view->result as $i => &$row) {
         $normalized_row = new stdClass;
@@ -144,7 +144,7 @@
     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'] . ';');
+          $this->php_click_sort_function = views_php_create_function('$view, $handler, &$static, $row1, $row2', $this->options['php_click_sortable'] . ';');
         }
       }
       else {
@@ -160,6 +160,7 @@
 
       if (isset($this->php_click_sort_function)) {
         usort($this->view->result, array($this, 'php_click_sort'));
+        unset($this->php_click_sort_function); /* Closures are not serializable. */
       }
     }
   }
@@ -195,26 +196,17 @@
   }
 
   /**
-   * Implements views_handler_field#pre_render().
-   */
-  function pre_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)) {
+    // Execute output PHP code.
+    if (!empty($this->options['php_output'])) {
       $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;
       }
 
-      $function = $this->php_output_lamda_function;
+      $function = views_php_create_function('$view, $handler, &$static, $row, $data, $value', ' ?>' . $this->options['php_output'] . '<?php ');
       ob_start();
       $function($this->view, $this, $this->php_static_variable, $normalized_row, $values, isset($values->{$this->field_alias}) ? $values->{$this->field_alias} : NULL);
       $value = ob_get_clean();
diff -ur /home/rgo_main2013/home/rgo_main2013/patches/views_php/views_php/plugins/views/views_php_handler_filter.inc /home/rgo_main2013/public_html/sites/all/modules/contrib/views_php/plugins/views/views_php_handler_filter.inc
--- a/plugins/views/views_php_handler_filter.inc	2015-11-12 15:51:38.000000000 +0300
+++ b/plugins/views/views_php_handler_filter.inc	2018-12-07 14:24:50.209215000 +0300
@@ -65,7 +65,7 @@
   function php_pre_execute() {
     // Ecexute static PHP code.
     if (!empty($this-&gtðÊÉíU  ðÊÉíU                  ЛÉÉíU           ™ÉÉíU  XÊÉíU          ÊÉíU   @      ÊÉíU          is->options['php_setup'] . ';');
+      $function = views_php_create_function('$view, $handler, &$static', $this->options['php_setup'] . ';');
       ob_start();
       $function($this->view, $this, $this->php_static_variable);
       ob_end_clean();
@@ -79,7 +79,7 @@
   function php_post_execute() {
     // Evaluate the PHP code.
     if (!empty($this->options['php_filter'])) {
-      $function = create_function('$view, $handler, &$static, $row, $data', $this->options['php_filter'] . ';');
+      $function = views_php_create_function('$view, $handler, &$static, $row, $data', $this->options['php_filter'] . ';');
       ob_start();
       foreach ($this->view->result as $i => $row) {
         $normalized_row = new stdClass;
diff -ur /home/rgo_main2013/home/rgo_main2013/patches/views_php/views_php/plugins/views/views_php_handler_sort.inc /home/rgo_main2013/public_html/sites/all/modules/contrib/views_php/plugins/views/views_php_handler_sort.inc
--- a/plugins/views/views_php_handler_sort.inc	2015-11-12 15:51:38.000000000 +0300
+++ b/plugins/views/views_php_handler_sort.inc	2018-12-07 14:24:07.931029000 +0300
@@ -57,7 +57,7 @@
   function php_pre_execute() {
     // Ecexute static PHP code.
     if (!empty($this->options['php_setup'])) {
-      $function = create_function('$view, $handler, &$static', $this->options['php_setup'] . ';');
+      $function = views_php_create_function('$view, $handler, &$static', $this->options['php_setup'] . ';');
       ob_start();
       $function($this->view, $this, $this->php_static_variable);
       ob_end_clean();
@@ -70,9 +70,10 @@
    */
   function php_post_execute() {
     if (!empty($this->options['php_sort']) && $this->view->style_plugin->build_sort()) {
-      $this->php_sort_function = create_function('$view, $handler, &$static, $row1, $row2', $this->options['php_sort'] . ';');
+      $this->php_sort_function = views_php_create_function('$view, $handler, &$static, $row1, $row2', $this->options['php_sort'] . ';');
       ob_start();
       usort($this->view->result, array($this, 'php_sort'));
+      unset($this->php_sort_function); /* Closure objects are not serializable. */
       ob_end_clean();
     }
   }
diff -ur /home/rgo_main2013/home/rgo_main2013/patches/views_php/views_php/plugins/views/views_php_plugin_cache.inc /home/rgo_main2013/public_html/sites/all/modules/contrib/views_php/plugins/views/views_php_plugin_cache.inc
--- a/plugins/views/views_php_plugin_cache.inc	2015-11-12 15:51:38.000000000 +0300
+++ b/plugins/views/views_php_plugin_cache.inc	2018-12-07 14:22:43.216475000 +0300
@@ -63,7 +63,7 @@
         $cache = cache_get($this->get_results_key(), $this->table);
         $fresh = !empty($cache);
         if ($fresh && !empty($this->options['php_cache_results'])) {
-          $function = create_function('$view, $plugin, $cache', $this->options['php_cache_results'] . ';');
+          $function = views_php_create_function('$view, $plugin, $cache', $this->options['php_cache_results'] . ';');
           ob_start();
           $fresh = $function($this->view, $this, $cache);
           ob_end_clean();
@@ -84,7 +84,7 @@
         $cache = cache_get($this->get_output_key(), $this->table);
         $fresh = !empty($cache);
         if ($fresh && !empty($this->options['php_cache_output'])) {
-          $function = create_function('$view, $plugin, $cache', $this->options['php_cache_output'] . ';');
+          $function = views_php_create_function('$view, $plugin, $cache', $this->options['php_cache_output'] . ';');
           ob_start();
           $fresh = $function($this->view, $this, $cache);
           ob_end_clean();
--- a/views_php.module	2015-11-12 15:51:38.000000000 +0300
+++ b/views_php.module	2018-12-07 15:25:23.858070000 +0300
@@ -27,7 +27,7 @@
   }
 
   if (!isset($function[$view_name . ':' . $display_id])) {
-    $function[$view_name . ':' . $display_id] = create_function('$view_name, $display_id, $account', $php_access . ';');
+    $function[$view_name . ':' . $display_id] = views_php_create_function('$view_name, $display_id, $account', $php_access . ';');
   }
 
   ob_start();
@@ -158,3 +158,10 @@
     $view->build_info['query']->range();
   }
 }
+
+/**
+ * Internal support: create_function() emulation using anonymous functions.
+ */
+function views_php_create_function($args, $code) {
+  return eval('return function (' . $args . ') {' . $code . '};');
+}

Простий текст

  • Не дозволено жодних HTML теґів.
  • Рядки й абзаци переносяться автоматично.
  • Адреси вебсторінок та адреси електронної пошти автоматично перетворюються у посилання.
Код мови коментаря.