-3

在 drupal 中,此代码会引发无法捕获的 php 错误:

/*
* @param $variables
*   An associative array containing:
*   - element: An associative array containing the properties of the element.
*     Properties used: #title, #title_display, #description, #id, #required,
*     #children, #type, #name.
*
* @ingroup themeable
*/
function theme_form_element($variables) {
  //print_r($variables); die("daa`");
  $element = &$variables['element'];
  // This is also used in the installer, pre-database setup.
  $t = get_t();

  // This function is invoked as theme wrapper, but the rendered form element
  // may not necessarily have been processed by form_builder().

  $element += array(
    '#title_display' => 'before',
  );
...

drupal 函数的最后一行抛出错误。

$variables['element']进入函数时是一个数组。当$element引用分配给它时,它(在 内$variables)从 type更改Array为 type &Array。该变量$element将自身报告为 type Array

以下行在使用而不是 += 运算符时不会引发相同的错误:

$element = $element + array();
$new = array_merge($element, array());  

@iputonmyrobeandwizardhat 要求提供 sscce,我试图在下面提供。但是,该代码不会单独导致与在 drupal 的渲染过程中使用时抛出的相同错误。谁能推荐一种隔离错误的方法?

<?php
function theme_form_element($variables) { 
  $element = &$variables['element'];         
  var_dump($variables);                //['element'] becomes type &array

  //not fatal
  $element2 = $element + array( '#title' => 'before'); 

  //not fatal
  $element3 = array_merge($element, array( '#title' => 'before'));   

  $element += array( '#title_display' => 'before');  
    var_dump($element);
}

$variables_array = array
(
'element' => array
    (
        '#type' => 'textfield',
        '#title' => 'First Name:',
        '#default_value' => '',
        '#size' => 60,
        '#maxlength' => 64,
        '#description' => 'First name',
        '#required' => 1,
        '#input' => 1,
        '#autocomplete_path' => '',
        '#process' => Array ('ajax_process_form'),

        '#theme' => 'textfield',
        '#theme_wrappers' => Array('form_element'),

        '#defaults_loaded' => 1,
        '#tree' => '',
        '#parents' => Array('first_name'),

        '#array_parents' => Array('first_name'),

        '#weight' => 0,
        '#processed' => 1,
        '#attributes' => Array(),

        '#title_display' => 'before',
        '#id' => 'edit-first-name',
        '#name' => 'first_name',
        '#value' => '',
        '#ajax_processed' => '',
        '#sorted' => 1,
        '#children' => '<input type="text" id="edit-first-name" name="first_name" value="" size="60" maxl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iIjY2hpbGRyZW4iO3M6MTIxOiI8aW5wdXQgdHlwZT0idGV4dCIgaWQ9ImVkaXQtZmlyc3QtbmFtZSIgbmFtZT0iZmlyc3RfbmFtZSIgdmFsdWU9IiIgc2l6ZT0iNjAiIG1heGxlbmd0aD0iNjQiIGNsYXNzPSJmb3JtLXRleHQgcmVxdWlyZWQiIC8+Ijt9fQ==
4

1 回答 1

1

+= 适用于数组和引用。

$arr = array(1);
$e = &$arr;
$e += array(1,2);

print_r($e);//Array ( [0] => 1 [1] => 2 )

http://codepad.org/0M5ChPgt

于 2012-12-27T17:13:44.727 回答