K
K
Kirill2022-04-08 16:30:46
PHP
Kirill, 2022-04-08 16:30:46

How to fix REST plugin json for taxonomy terms?

Greetings! I have no experience in PHP, and I ask for help, I found an example code in the open spaces. But it does not correctly display the tree a little

{
  "11": {
    "14": {
      "tid": "14",
      "name": "child-1",
      "weight": "0"
    },
    "19": {
      "tid": "19",
      "name": "child-2",
      "weight": "1"
    },
    "tid": "11",
    "name": "parent-1",
    "weight": "7"
  },
  "12": {
    "17": {
      "tid": "17",
      "name": "child-1",
      "weight": "0"
    },
    "18": {
      "tid": "18",
      "name": "child-2",
      "weight": "1"
    },
    "tid": "12",
    "name": "parent-2",
    "weight": "6"
  }
 }

rest resource plugin code
$vid = 'digest';
    $terms =\Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree($vid, 0, 1);
    $termDataAll = [];
    foreach ($terms as $term) {
      $termDataAll[$term->tid]["tid"] = $term->tid;
      $termDataAll[$term->tid]["name"] = $term->name;
      $termDataAll[$term->tid]["weight"] = $term->weight;
      $childs =\Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree($vid, $term->tid, 1);
      foreach($childs as $child) {
        $termDataAll[$term->tid][$child->tid]["tid"] = $child->tid;
        $termDataAll[$term->tid][$child->tid]["name"] = $child->name;
        $termDataAll[$term->tid][$child->tid]["weight"] = $child->weight;
      }
    }


I don't understand where these tids "11" come from at the beginning of each block: {... ? how to remove them? and why sheet $termDataAll = []; but in the output it is data {...}?
python skills and logic don't help to understand this php)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2022-04-08
@KirillSPB777

Everything is elementary. In PHP, any array is associative, which corresponds to a JSON object.
But, if the array index starts from zero and contains only strictly consecutive numbers (0, 1, 2, 3, etc.), then for simplicity it is represented as a classic array, including in JSON.

print json_encode(['a', 'b']); // ["a","b"]
print json_encode([0 => 'a', 1 => 'b']); // ["a","b"]
print json_encode([1 => 'a', 2 => 'b']); // {"1":"a","2":"b"}
Your array is filled with indexes $term->tid, which do not form a classical sequence, and therefore are represented by an object.
Your code can be rewritten like this:
$vid = 'digest';
$termDataAll = array_map(
    fn($term) => [
        'tid' => $term->tid,
        'name' => $term->name,
        'weight' => $term->weight,
        'childs' => array_map(
            fn($child) => [
                'tid' => $child->tid,
                'name' => $child->name,
                'weight' => $child->weight
            ],
            \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree($vid, $term->tid, 1)
        )
    ],
    \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree($vid, 0, 1)
);

The result should be JSON like this
[
  [
    "tid": "11",
    "name": "parent-1",
    "weight": "7",
    "childs": [
      ["tid": "14", "name": "child-1", "weight": "0"],
      ["tid": "19", "name": "child-2", "weight": "1"],
    ],
  ], [
    "tid": "12",
    "name": "parent-2",
    "weight": "6",
    "childs": [
      ["tid": "17", "name": "child-1", "weight": "0"],
      ["tid": "18", "name": "child-2", "weight": "1"]
    ]
  ]
]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question