Maybe this has to do with an internal Drupal thing or something I don't fully understand. Looking at the output JSON, I'm wondering why the "below" key returns an object instead of an array?

Comments

mattferderer created an issue. See original summary.

tpzurdo’s picture

I am also having troubles with this object which is not showing several objects. It is showing only the first one.

fabianderijk’s picture

Assigned: Unassigned » fabianderijk

That's a good question. I will take a look at this soon.

xavier.masson’s picture

Hi guys, it's cause in getMenuItems() method src/Plugin/rest/resource/RestMenuItemsResource.php line 240 the array key is a string instead integer, when the serializer convert it in JSON the array is converted to object.

If you change :

      $items[$item_name] = [
        'key'      => $item_name,
        'title'    => $org_link->getTitle(),
        'uri'      => $uri,
        'alias'    => ltrim($alias, '/'),
        'external' => $external,
        'absolute' => $absolute,
        'weight'   => $org_link->getWeight(),
        'expanded' => $org_link->isExpanded(),
        'enabled'  => $org_link->isEnabled(),
        'uuid'     => $uuid,
        'options'     => $options,
      ];

to :

      $items[] = [
        'key'      => $item_name,
        'title'    => $org_link->getTitle(),
        'uri'      => $uri,
        'alias'    => ltrim($alias, '/'),
        'external' => $external,
        'absolute' => $absolute,
        'weight'   => $org_link->getWeight(),
        'expanded' => $org_link->isExpanded(),
        'enabled'  => $org_link->isEnabled(),
        'uuid'     => $uuid,
        'options'     => $options,
      ];

After the serialization, it's a array instead object.

xavier.masson’s picture

StatusFileSize
new653 bytes

  • fabianderijk committed 862e846 on 8.x-1.x authored by hait
    Issue #2893100 by hait: Question: Below as an array instead of object?
    
fabianderijk’s picture

Status: Active » Needs review

I've applied the patch. @mattferderer and @tpzurdo could you check if the dev version fixes the problem?

xavier.masson’s picture

StatusFileSize
new2.23 KB

I've patch too quickly, i've miss to change 'below' implementation.

+      end($items);
+      $item_key = key($items);
+
       if (!empty($item_value['below'])) {
-        $items[$item_name]['below'] = [];
-        $this->getMenuItems($item_value['below'], $items[$item_name]['below']);
+        $items[$item_key]['below'] = [];
+        $this->getMenuItems($item_value['below'], $items[$item_key]['below']);
       }

  • fabianderijk committed 82211a2 on 8.x-1.x authored by hait
    Issue #2893100 by hait, fabianderijk: Question: Below as an array...
fabianderijk’s picture

I've just pushed the last addition.

fabianderijk’s picture

Status: Needs review » Fixed
fabianderijk’s picture

This is fixed in the upcoming 8.x-1.5 release.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.