You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Drupal 7中出现T_OBJECT_OPERATOR语法错误,求解决方案

Fixing the Unexpected '->' ParseError in Drupal Rules PHP Eval

Let's break down what's causing this error and how to fix it. That ParseError: syntax error, unexpected '->' might look like a pure syntax issue at first, but it’s almost always triggered because $job_node isn’t a valid object when your code runs—it could be undefined, null, or a non-object value like a string or array.

Here’s a step-by-step solution:

1. Validate $job_node exists and is an object

First, add a check to ensure we only attempt to access properties on a valid node object:

<?php
// Confirm $job_node is a valid object before proceeding
if (isset($job_node) && is_object($job_node)) {
    // Rest of your logic will live inside this block
}
?>

2. Safely access the taxonomy term ID (tid)

Your original field access has two potential failure points:

  • Hardcoding 'und' (undefined language) might not match your site’s language setup (use the node’s dynamic language key instead)
  • The field could be empty, so accessing [0]['tid'] directly will throw errors

Update your code to handle these cases:

<?php
if (isset($job_node) && is_object($job_node)) {
    // Use the node's actual language key (falls back to 'und' if needed)
    $lang = isset($job_node->language) ? $job_node->language : 'und';
    
    // Check if the field has a valid tid before proceeding
    if (isset($job_node->field_job_cv_destination[$lang][0]['tid'])) {
        $tid = $job_node->field_job_cv_destination[$lang][0]['tid'];
        $term = taxonomy_term_load($tid);
        
        // Ensure the term loaded successfully before accessing its name
        if ($term && is_object($term)) {
            $name = $term->name;
            // Add your logic to use $name here
        }
    }
}
?>

3. Why this works

  • The initial $job_node check prevents us from using -> on an invalid value (the root cause of your syntax error)
  • Using $job_node->language instead of 'und' ensures compatibility with both monolingual and multilingual Drupal sites
  • Layered checks for array indexes and the loaded term eliminate undefined index errors and ensure we only access $term->name when $term is valid

Bonus Best Practice

Whenever possible, avoid using PHP eval in Rules. Built-in Rules actions or custom Rules plugins are easier to debug, maintain, and more secure than eval’d code.

内容的提问来源于stack exchange,提问作者Viz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:21:58