/** * Setup categories tree using Elgg menu * * @param string $hook Equals 'register' * @param string $type Equals 'menu:categories' * @param array $return An array of category menu items * @param array $params Additional parameters passed to elgg_view_menu() * 'entity' Entity * 'depth' Depth of the tree * 'limit' Number of subcategories to display * 'icons' Show icons * 'collapse' Collapse by default * * @return array */ public function setupCategoriesMenu($hook, $type, $return, $params) { elgg_require_js('framework/categories/menu'); $entity = elgg_extract('entity', $params, elgg_get_site_entity()); // container group or site or category if (!$entity instanceof ElggEntity) { return $return; } unset($params['name']); $defaults = array('depth' => false, 'icons' => true, 'collapse' => true, 'limit' => false, 'badge' => true); $params = array_merge($defaults, $params); $root_nodes = array($entity->guid); $tree = new TreeNode($entity, null, null, $params); $nodes = $tree->toArray($params['depth']); if (!$entity instanceof ElggSite) { if (!elgg_in_context('categories-manage')) { if ($entity instanceof ElggGroup && $this->config->allowsGroupCategories() || $entity instanceof ElggObject) { // Add site wide categories if we are in a group context with group categories enabled // or we are in an object context $site = elgg_get_site_entity(); $root_nodes[] = $site->guid; $site_tree = new TreeNode($site, null, null, $params); $site_nodes = $site_tree->toArray($params['depth']); $nodes = array_merge($site_nodes, $nodes); } } } if (!empty($nodes)) { foreach ($nodes as $node_opts) { $node_guid = elgg_extract('node_guid', $node_opts); $parent_guid = elgg_extract('parent_guid', $node_opts); $node = get_entity($node_guid); $parent = get_entity($parent_guid); if (!$node instanceof ElggEntity || in_array($node->guid, $root_nodes)) { continue; } $has_children = elgg_extract('has_children', $node_opts); $item_params = array_merge($params, array('entity' => $node, 'has_children' => $has_children)); $return[] = ElggMenuItem::factory(array('name' => "category:{$node->guid}", 'parent_name' => $parent && !in_array($parent->guid, $root_nodes) ? "category:{$parent->guid}" : null, 'text' => elgg_view('framework/categories/node', $item_params), 'href' => false, 'priority' => $node->priority ? (int) $node->priority : 999, 'data' => $item_params)); } } if (elgg_in_context('categories-manage')) { // Adds an empty subcategory form $return[] = ElggMenuItem::factory(array('name' => 'placeholder', 'text' => elgg_view('framework/categories/placeholder', array('container' => $entity->guid)), 'href' => false, 'priority' => 1000, 'data-guid' => $node->guid)); } return $return; }
<?php use hypeJunction\Categories\TreeNode; $container_guid = get_input('container_guid'); $container = get_entity($container_guid); if (!$container) { $container = elgg_get_site_entity(); } $options = get_input('options', array('limit' => false)); $tree = new TreeNode($container, null, null, $options); $nodes = array(); $children = $tree->getChildren(); foreach ($children as $child) { $category = $child->getEntity(); if (!$category) { continue; } $nodes[] = array('id' => $category->guid, 'text' => $category->getDisplayName(), 'icon' => $category->icontime ? $category->getIconURL('tiny') : false, 'children' => $child->hasChildren(), 'li_attr' => array(), 'a_attr' => array('href' => $category->getURL())); } echo json_encode($nodes);