从 Drupal 8 中的术语实体引用中获取字段时尝试获取非对象的 属性
Trying to get property of non-object when grabbing fields from Term Entity reference in Drupal 8
我正在创建内容类型传记的 XML 提要,其中包含实体参考术语字段。我正在加载节点,加载段落字段,然后使用 Term::load 加载段落字段中的术语对象,该字段是按术语 ID 编号的术语。但是,当我尝试从术语本身获取字段时,如果不在我使用 ->name->value 的行上抛出 "Trying to get property of non-object" 就无法做到这一点。这些字段成功地被抓取和渲染,错误随之而来。尝试使用 ->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>
但是对于带有 ->name->value 的行,我得到 PHP 错误 "Trying to get property of non-object"
我尝试使用 ->getName() 方法,但会立即引发 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 错误但也消除了错误
检查 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 : '';
无需每次加载段落和术语,您可以通过entity
引用获取它们。
我正在创建内容类型传记的 XML 提要,其中包含实体参考术语字段。我正在加载节点,加载段落字段,然后使用 Term::load 加载段落字段中的术语对象,该字段是按术语 ID 编号的术语。但是,当我尝试从术语本身获取字段时,如果不在我使用 ->name->value 的行上抛出 "Trying to get property of non-object" 就无法做到这一点。这些字段成功地被抓取和渲染,错误随之而来。尝试使用 ->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>
但是对于带有 ->name->value 的行,我得到 PHP 错误 "Trying to get property of non-object" 我尝试使用 ->getName() 方法,但会立即引发 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 错误但也消除了错误
检查 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 : '';
无需每次加载段落和术语,您可以通过entity
引用获取它们。