/** * Caches menu items for a given entity and returns an array of the menu items * * @param \StaticPage $root_entity Root entity to fetch the menu items for * * @return array|false */ public static function generateMenuItemsCache(\StaticPage $root_entity) { if (!$root_entity instanceof \StaticPage) { return false; } $static_items = []; $priority = (int) $root_entity->order; if (empty($priority)) { $priority = (int) $root_entity->time_created; } $root_menu_options = ['name' => $root_entity->getGUID(), 'rel' => $root_entity->getGUID(), 'href' => $root_entity->getURL(), 'text' => elgg_format_element('span', [], $root_entity->title), 'priority' => $priority, 'section' => 'static']; if ($root_entity->canEdit()) { $root_menu_options['itemClass'] = ['static-sortable']; } // add main menu items $static_items[$root_entity->getGUID()] = \ElggMenuItem::factory($root_menu_options); // add all sub menu items so they are cacheable $ia = elgg_set_ignore_access(true); $submenu_entities = elgg_get_entities_from_relationship(['type' => 'object', 'subtype' => \StaticPage::SUBTYPE, 'relationship_guid' => $root_entity->getGUID(), 'relationship' => 'subpage_of', 'limit' => false, 'inverse_relationship' => true]); if ($submenu_entities) { foreach ($submenu_entities as $submenu_item) { if (!has_access_to_entity($submenu_item) && !$submenu_item->canEdit()) { continue; } $priority = (int) $submenu_item->order; if (empty($priority)) { $priority = (int) $submenu_item->time_created; } $static_items[$submenu_item->getGUID()] = \ElggMenuItem::factory(['name' => $submenu_item->getGUID(), 'rel' => $submenu_item->getGUID(), 'href' => $submenu_item->getURL(), 'text' => elgg_format_element('span', [], $submenu_item->title), 'priority' => $priority, 'parent_name' => $submenu_item->getContainerGUID(), 'section' => 'static']); } } elgg_set_ignore_access($ia); $file = new \ElggFile(); $file->owner_guid = $root_entity->getGUID(); $file->setFilename('static_menu_item_cache'); $file->open('write'); $file->write(serialize($static_items)); $file->close(); return $static_items; }
$parent = get_entity($parent_guid); elgg_set_ignore_access($ia); if (elgg_instanceof($parent, 'object', 'static')) { if ($parent->container_guid == $owner->getGUID()) { // parent is a top page $subpage_relationship_guid = $parent_guid; } else { // further in the tree, so find out which tree $relations = $parent->getEntitiesFromRelationship(['type' => 'object', 'subtype' => StaticPage::SUBTYPE, 'relationship' => 'subpage_of', 'limit' => 1]); if ($relations) { $subpage_relationship_guid = $relations[0]->getGUID(); } } if ($subpage_relationship_guid) { // remove old tree relationships remove_entity_relationships($entity->getGUID(), 'subpage_of'); // add new tree relationship $entity->addRelationship($subpage_relationship_guid, 'subpage_of'); } } } // check the children for the correct tree if (!$new_entity) { static_check_children_tree($entity, $subpage_relationship_guid); } $ia = elgg_set_ignore_access(true); // save all the content $entity->title = $title; $entity->description = $description; $entity->access_id = $access_id; $entity->container_guid = $parent_guid;