2
votes

I'm setting up a controller module in Drupal 8 to create an XML feed of terms with fields in a specific vocabulary. I've been able to do some initial setup to loop through each of the terms, but I'm not sure how to get the field values within the term.

My controller:

<?php
namespace Drupal\af_services_xml\Controller;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\Response;
use Drupal\Core\Entity\Term;
class FruitController extends ControllerBase {
  /**
   * Function (Public): Returns content to my route.
   */
  public function fruits_xml() {
    $response = new Response();
    $response->headers->set('Content-Type', 'text/xml');
    // Setup general nid query.
    $query = \Drupal::entityQuery('taxonomy_term');
    $query->condition('vid', 'fruits');
    $result = array_values($query->execute());
    for ($i = 0; $i < count($result); $i++) {
      $result[$i] = self::xmlOutput($result[$i]);
    }
    // Prepend XML wrapper.
    array_unshift($result, '<?xml version="1.0" encoding="utf-8"?><fruitbasket>');
    // Append XML closure.
    $result[] = '</fruitbasket>';
    $response->setContent(implode('', $result));
    return $response;
  }

  private function xmlOutput($tid) {
    $tid = '<fruit whereValue="' . $tid . '">
              <name>title</name>
              <description>description</description>
              <numberofseeds>field_seed_count</numberofseeds>
            </fruit>';
    return $tid;
  }
}

You can see, I'm able to grab the term id itself. This is the current output of my feed (i have six fruit terms in my vocabulary)

<fruitbasket>
  <fruit whereValue="87">
    <name>title</name>
    <description>description</description>
    <numberofseeds>field_seed_count</numberofseeds>
  </fruit>
  <fruit whereValue="90">
    <name>title</name>
    <description>description</description>
    <numberofseeds>field_seed_count</numberofseeds>
  </fruit>
  <fruit whereValue="86">
    <name>title</name>
    <description>description</description>
    <numberofseeds>field_seed_count</numberofseeds>
  </fruit>
</fruitbasket>

Of course, most of this output is hard-coded. I want to use a get method (maybe either get() or getFields() ?), define each individual field as a variable, and place that variable into my XML markup. I'm not exactly sure how to go about doing this, however.

Some pseudocode: $seedCount = $tid->get(field_seed_count); and then using $seedCount in my $tid = declaration, between the <numberofseeds> tags.

Any suggestions?

UPDATE: SOLVED

This almost worked great! I had to update the loop since foreach isn't using $i. I also loaded the Term in my xmlOutput function

$tids = array_values($query->execute());
   foreach ($tids as $key=>$value) {
   $result[$key] = self::xmlOutput($value);
}

private function xmlOutput($input) {
  $term = Term::load($input);
  $tid = '<tid>' . $term->id() . '</tid>';
  return $tid;
}
1

1 Answers

1
votes

You need to load the terms like below

$tids = $query->execute();
$terms = \Drupal\taxonomy\Entity\Term::loadMultiple($tids);

Then,

foreach ($terms as $term) {
  $result[$i] = self::xmlOutput($term);
}
private function xmlOutput($term) {
  $tid = '<fruit whereValue="' . $term->id() . '">
          <name>.' $term->name->value '.</name>
          <description>.' $term->description->value '.</description>
          <numberofseeds>.' $term->field_seed_count->value '.</numberofseeds>
        </fruit>';
  return $tid;
}