我正在创建一个内容类型传记的XML提要,其中包含实体引用术语字段。我正在加载节点,加载段落字段,然后使用Term::load在段落字段中加载term对象,这是一个按术语ID编号的术语。但是,当我试图从术语本身获取字段时,我必须在使用->name->value的行上抛出“尝试获取非对象的属性”。字段成功地被抓取和渲染,错误也随之而来。尝试使用->getName()方法会完全破坏提要。
private function xmlOutput($input) {
$node = Node::load($input);
$educationArray = $node->get('field_group_education');
$educationOutput = [];
foreach ($educationArray as $key => $value) {
$educationGroupID = $node->get('field_group_education')[$key]->target_id;
$educationGroup = Paragraph::load($educationGroupID);
$honors = $educationGroup->get('field_academic_honors')->value;
$degreeID = $educationGroup->get('field_degrees')->target_id;
$degree = Term::load($degreeID)->name->value;
$educationID = $educationGroup->get('field_education')->target_id;
$schoolName = Term::load($educationID)->name->value;
$yearGraduated = $educationGroup->get('field_year_graduated')->value;
$educationRender = '<professionalEducation>
<order>' . $key . '</order>
<school whereField="Name" whereValue="' . $schoolName . '" autoCreate="true"/>
<educationType whereField="Name" whereValue="' . $degree . '" autoCreate="true"/>
<yearGraduated>' . $yearGraduated . '</yearGraduated>
<degree whereField="Name" whereValue="' . $degree . '" autoCreate="true"/> .
<honors>' . $honors . '</honors>
</professionalEducation>';
array_push($educationOutput, $educationRender);
}
$compiled = [
'<education>',
implode($educationOutput),
'</education>',
];
return implode('', $compiled);}
这将成功地呈现
<education>
<professionalEducation>
<order>0</order>
<school whereField="Name" whereValue="education term 1"
autoCreate="true"/>
<educationType whereField="Name" whereValue="AA" autoCreate="true"/>
<yearGraduated>1990</yearGraduated>
<degree whereField="Name" whereValue="AA" autoCreate="true"/>
<honors>Honors 1</honors>
</professionalEducation>
<professionalEducation>
<order>1</order>
<school whereField="Name" whereValue="education term 2" autoCreate="true"/>
<educationType whereField="Name" whereValue="AB" autoCreate="true"/>
<yearGraduated>2000</yearGraduated>
<degree whereField="Name" whereValue="AB" autoCreate="true"/>
<honors>honors 2</honors>
</professionalEducation>
</education>但是,对于使用->getName()方法尝试获取->name->value的行,我得到了“试图获取非对象属性”的PHP错误,但这会立即引发500个错误。使用isset检查是否有一个值在使getName()工作时也不起作用。
$degree = '';
if(isset(Term::load($degreeID)->name->value)) {
$degree = $degreeObject->name->value;
}工作,但错误仍然存在
$degree = '';
if(isset(Term::load($degreeID)->getName())) {
$degree = $degreeObject->getName();
}抛出500错误,但也消除错误
发布于 2018-04-26 09:01:35
检查target_id isset()。按以下方式修改代码
$educationGroup = $value->entity;
$honors = isset($educationGroup->field_academic_honors->target_id) ? $educationGroup->field_academic_honors->value : '';
$degree = isset($educationGroup->field_degrees->target_id) ? $educationGroup->field_degrees->entity->name->value : '';
$schoolName = isset($educationGroup->field_education->target_id) ? $educationGroup->field_education->entity->name->value : '';
$yearGraduated = isset($educationGroup->field_year_graduated->target_id) ? $educationGroup->field_year_graduated->value : '';每次您可以使用引用获得它们时,不需要加载段和术语。
https://stackoverflow.com/questions/50030083
复制相似问题