本文整理汇总了PHP中Knp\Menu\ItemInterface::reorderChildren方法的典型用法代码示例。如果您正苦于以下问题:PHP ItemInterface::reorderChildren方法的具体用法?PHP ItemInterface::reorderChildren怎么用?PHP ItemInterface::reorderChildren使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Knp\Menu\ItemInterface
的用法示例。
在下文中一共展示了ItemInterface::reorderChildren方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: sort
/**
* Reorder menu based on position attribute
*
* @param ItemInterface $menu
*/
protected function sort(ItemInterface $menu)
{
if ($menu->hasChildren() && $menu->getDisplayChildren()) {
$orderedChildren = [];
$unorderedChildren = [];
$hasOrdering = false;
$children = $menu->getChildren();
foreach ($children as &$child) {
if ($child->hasChildren() && $child->getDisplayChildren()) {
$this->sort($child);
}
$position = $child->getExtra('position');
if ($position !== null) {
$orderedChildren[$child->getName()] = (int) $position;
$hasOrdering = true;
} else {
$unorderedChildren[] = $child->getName();
}
}
if ($hasOrdering) {
asort($orderedChildren);
$menu->reorderChildren(array_merge(array_keys($orderedChildren), $unorderedChildren));
}
}
}
示例2: moveChildToPosition
/**
* Moves child to specified position. Rearange other children accordingly.
*
* @param ItemInterface $item
* @param ItemInterface $child Child to move.
* @param integer $position Position to move child to.
*/
public function moveChildToPosition(ItemInterface $item, ItemInterface $child, $position)
{
$name = $child->getName();
$order = array_keys($item->getChildren());
$oldPosition = array_search($name, $order);
unset($order[$oldPosition]);
$order = array_values($order);
array_splice($order, $position, 0, $name);
$item->reorderChildren($order);
}
示例3: reorderMenu
/**
* Reorder the menu
*
* @param \Knp\Menu\ItemInterface $menu
*
* @return MenuItem
*/
private function reorderMenu($menu)
{
$menuItems = $menu->getChildren();
usort($menuItems, array($this, 'menuSort'));
$newMenuOrder = array();
foreach ($menuItems as $menuItem) {
$newMenuOrder[] = $menuItem->getName();
}
$menu->reorderChildren($newMenuOrder);
return $menu;
}
示例4: reorderMenuItems
/**
* Reorderd the items in the menu based on the extra data
*
* @param ItemInterface $menu
*/
protected function reorderMenuItems(ItemInterface $menu)
{
$menuOrderArray = array();
$addLast = array();
$alreadyTaken = array();
foreach ($menu->getChildren() as $menuItem) {
if ($menuItem->hasChildren()) {
$this->reorderMenuItems($menuItem);
}
$orderNumber = $menuItem->getExtra('orderNumber');
if ($orderNumber != null) {
if (!isset($menuOrderArray[$orderNumber])) {
$menuOrderArray[$orderNumber] = $menuItem->getName();
} else {
$alreadyTaken[$orderNumber] = $menuItem->getName();
}
} else {
$addLast[] = $menuItem->getName();
}
}
ksort($menuOrderArray);
if (!empty($alreadyTaken)) {
foreach ($alreadyTaken as $key => $value) {
$keysArray = array_keys($menuOrderArray);
$position = array_search($key, $keysArray);
if ($position === false) {
continue;
}
$menuOrderArray = array_merge(array_slice($menuOrderArray, 0, $position), array($value), array_slice($menuOrderArray, $position));
}
}
ksort($menuOrderArray);
if (!empty($addLast)) {
foreach ($addLast as $value) {
$menuOrderArray[] = $value;
}
}
if (!empty($menuOrderArray)) {
$menu->reorderChildren($menuOrderArray);
}
}
示例5: reorderMenuItems
/**
* Reorder Menus items
*
* @param ItemInterface $menu
*/
protected function reorderMenuItems(ItemInterface $menu)
{
$menuOrderArray = array();
$addLast = array();
$alreadyTaken = array();
foreach ($menu->getChildren() as $key => $menuItem) {
if ($menuItem->hasChildren()) {
$this->reorderMenuItems($menuItem);
}
$orderNumber = $menuItem->getExtra('orderNumber');
if ($orderNumber != null) {
if (!isset($menuOrderArray[$orderNumber])) {
$menuOrderArray[$orderNumber] = $menuItem->getName();
} else {
$alreadyTaken[$orderNumber] = $menuItem->getName();
// $alreadyTaken[] = array('orderNumber' => $orderNumber, 'name' => $menuItem->getName());
}
} else {
$addLast[] = $menuItem->getName();
}
}
// sort them after first pass
ksort($menuOrderArray);
// handle position duplicates
if (count($alreadyTaken)) {
foreach ($alreadyTaken as $key => $value) {
// the ever shifting target
$keysArray = array_keys($menuOrderArray);
$position = array_search($key, $keysArray);
if ($position === false) {
continue;
}
$menuOrderArray = array_merge(array_slice($menuOrderArray, 0, $position), array($value), array_slice($menuOrderArray, $position));
}
}
// sort them after second pass
ksort($menuOrderArray);
// add items without ordernumber to the end
if (count($addLast)) {
foreach ($addLast as $key => $value) {
$menuOrderArray[] = $value;
}
}
if (count($menuOrderArray)) {
$menu->reorderChildren($menuOrderArray);
}
}