Search code examples
phpxmlmodulecontrollerdrupal-8

Trying to get property of non-object when grabbing fields from Term Entity reference in Drupal 8


I'm creating an XML feed of a content type Biography, which has Entity Reference terms fields. I am loading the node, loading the Paragraph fields, then using Term::load to load the term object within the paragraph field that is a term by term ID number. However, when I try to grab the fields from the term itself, I can't do so without throwing "Trying to get property of non-object" on the lines where I use ->name->value . The fields successfully get grabbed and rendered, the errors just come with them. Trying to use ->getName() method breaks the feed altogether.

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);

}

This renders successfully

<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>

But I get PHP errors that "Trying to get property of non-object" for the lines with ->name->value I tried using ->getName() method, but that immediately throws a 500 error. Using isset to check that there's a value didn't work either for making getName() work.

$degree = '';
if(isset(Term::load($degreeID)->name->value)) {
  $degree = $degreeObject->name->value;
}

Works, but errors remain

$degree = '';
if(isset(Term::load($degreeID)->getName())) {
  $degree = $degreeObject->getName();
}

Throws 500 error but also gets rid of error


Solution

  • Check the target_id isset(). Modify your code as below

      $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 : '';
    

    No need to load paragraph and Term each time you can get them with entity reference.