src/Forms/TreeDropdownField.php
protected function flattenChildrenArray($children, $parentTitles = [])
    {
        $output = [];

        foreach ($children as $child) {
            $childTitles = array_merge($parentTitles, [$child['title']]);
            $grandChildren = $child['children'];
            $contextString = implode('/', $parentTitles);

            $child['contextString'] = ($contextString !== '') ? $contextString . '/' : '';
            unset($child['children']);

            if (!$this->search || in_array($child['id'], $this->realSearchIds)) {
                $output[] = $child;
            }
            $output = array_merge($output, $this->flattenChildrenArray($grandChildren, $childTitles));
        }

        return $output;
    }
protected function flattenChildrenArray($children, $parentTitles = [])
    {
        $output = [];

        foreach ($children as $child) {
            $childTitles = array_merge($parentTitles, [$child['title']]);
            $grandChildren = $child['children'];
            $contextString = implode('/', $parentTitles);

            $child['contextString'] = ($contextString !== '') ? $contextString . '/' : '';
            unset($child['children']);

            if (!$this->search || in_array($child['id'], $this->realSearchIds)) {
                $output[] = $child;
            }
            $output = array_merge($output, $this->flattenChildrenArray($grandChildren, $childTitles));
        }

        return $output;
    }
protected function flattenChildrenArray ( $ children , $ parentTitles = [ ] ) { $ output = [ ] ; foreach ( $ children as $ child ) { $ childTitles = array_merge ( $ parentTitles , [ $ child [ 'title' ] ] ) ; $ grandChildren = $ child [ 'children' ] ; $ contextString = implode ( '/' , $ parentTitles ) ; $ child [ 'contextString' ] = ( $ contextString !== '' ) ? $ contextString . '/' : '' ; unset ( $ child [ 'children' ] ) ; if ( ! $ this -> search || in_array ( $ child [ 'id' ] , $ this -> realSearchIds ) ) { $ output [ ] = $ child ; } $ output = array_merge ( $ output , $ this -> flattenChildrenArray ( $ grandChildren , $ childTitles ) ) ; } return $ output ; }
Flattens a given list of children array items, so the data is no longer
structured in a hierarchy

NOTE: uses {@link TreeDropdownField::$realSearchIds} to filter items by if there is a search

@param array $children - the list of children, which could contain their own children
@param array $parentTitles - a list of parent titles, which we use to construct the contextString
@return array - flattened list of children
Flattens a given list of children array items so the data is no longer structured in a hierarchy