Xml 在Drupal8中从术语实体引用获取字段时,尝试获取非对象的属性
我正在创建一个内容类型传记的XML提要,其中包含实体引用术语字段。我正在加载节点,加载段落字段,然后使用Term::load加载段落字段中的Term对象,该字段是一个逐项ID号。然而,当我试图从术语本身获取字段时,如果不在我使用->名称->值的行中抛出“尝试获取非对象的属性”,我就无法做到这一点。字段成功地被抓取和渲染,错误也随之出现。尝试使用->getName()方法会完全中断提要Xml 在Drupal8中从术语实体引用获取字段时,尝试获取非对象的属性,xml,module,controller,drupal-8,Xml,Module,Controller,Drupal 8,我正在创建一个内容类型传记的XML提要,其中包含实体引用术语字段。我正在加载节点,加载段落字段,然后使用Term::load加载段落字段中的Term对象,该字段是一个逐项ID号。然而,当我试图从术语本身获取字段时,如果不在我使用->名称->值的行中抛出“尝试获取非对象的属性”,我就无法做到这一点。字段成功地被抓取和渲染,错误也随之出现。尝试使用->getName()方法会完全中断提要 private function xmlOutput($input) { $node = Node::loa
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);
工作正常,但错误依然存在
$degree = '';
if(isset(Term::load($degreeID)->getName())) {
$degree = $degreeObject->getName();
}
抛出500个错误,但也会清除错误检查目标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 : '';
无需每次使用实体
引用获取段落和术语时都加载它们
$degree = '';
if(isset(Term::load($degreeID)->getName())) {
$degree = $degreeObject->getName();
}
$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 : '';