首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Drupal 8中从术语实体引用抓取字段时尝试获取非对象的属性

在Drupal 8中从术语实体引用抓取字段时尝试获取非对象的属性
EN

Stack Overflow用户
提问于 2018-04-25 19:36:54
回答 1查看 579关注 0票数 1

我正在创建一个内容类型传记的XML提要,其中包含实体引用术语字段。我正在加载节点,加载段落字段,然后使用Term::load在段落字段中加载term对象,这是一个按术语ID编号的术语。但是,当我试图从术语本身获取字段时,我必须在使用->name->value的行上抛出“尝试获取非对象的属性”。字段成功地被抓取和渲染,错误也随之而来。尝试使用->getName()方法会完全破坏提要。

代码语言:javascript
复制
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);

}

这将成功地呈现

代码语言:javascript
复制
<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()工作时也不起作用。

代码语言:javascript
复制
$degree = '';
if(isset(Term::load($degreeID)->name->value)) {
  $degree = $degreeObject->name->value;
}

工作,但错误仍然存在

代码语言:javascript
复制
$degree = '';
if(isset(Term::load($degreeID)->getName())) {
  $degree = $degreeObject->getName();
}

抛出500错误,但也消除错误

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-26 09:01:35

检查target_id isset()。按以下方式修改代码

代码语言:javascript
复制
  $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 : '';

每次您可以使用引用获得它们时,不需要加载段和术语

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50030083

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档