src/services/CpNavService.php
public function regenerateNav($layoutId, $generatedNav, $currentNav)
    {
        // Find the extra or missing menu item
        if (count($generatedNav) < count($currentNav)) {
            $order = 0;

            // A menu item exists in the menu, but not in our records - add
            foreach ($currentNav as $value) {
                if (isset($value['url'])) {
                    $handle = str_replace(UrlHelper::url() . '/', '', $value['url']);
                } else {
                    $handle = StringHelper::toKebabCase($value['label']);
                }

                if (!isset($generatedNav[$handle]) && !CpNav::$plugin->navigationService->getByHandle($layoutId, $handle)) {
                    $icon = null;

                    // Check for custom icon (plugins)
                    if (isset($value['icon'])) {
                        $icon = $value['icon'];
                    }

                    // Check for built-in Craft icon
                    if (isset($value['fontIcon'])) {
                        $icon = $value['fontIcon'];
                    }

                    $model = $this->_prepareNavModel([
                        'layoutId' => $layoutId,
                        'handle'   => $handle,
                        'label'    => $value['label'],
                        'order'    => $order,
                        'icon'     => $icon,
                        'url'      => $handle,
                    ]);

                    CpNav::$plugin->navigationService->save($model);
                }

                $order++;
            }
        } else {

            // Create an array of current navigation handles to easy check via in_array
            $currentNavHandles = array_column($currentNav, 'url');

            // A menu item exists in our records, but not in the menu - delete
            foreach ($generatedNav as $key => $value) {
                if (!\in_array($value['handle'], $currentNavHandles, false)) {
                    $navModel = CpNav::$plugin->navigationService->getByHandle($layoutId, $value['handle']);

                    if ($navModel) {
                        CpNav::$plugin->navigationService->delete($navModel);
                    }
                }
            }
        }
    }
public function regenerateNav($layoutId, $generatedNav, $currentNav)
    {
        // Find the extra or missing menu item
        if (count($generatedNav) < count($currentNav)) {
            $order = 0;

            // A menu item exists in the menu, but not in our records - add
            foreach ($currentNav as $value) {
                if (isset($value['url'])) {
                    $handle = str_replace(UrlHelper::url() . '/', '', $value['url']);
                } else {
                    $handle = StringHelper::toKebabCase($value['label']);
                }

                if (!isset($generatedNav[$handle]) && !CpNav::$plugin->navigationService->getByHandle($layoutId, $handle)) {
                    $icon = null;

                    // Check for custom icon (plugins)
                    if (isset($value['icon'])) {
                        $icon = $value['icon'];
                    }

                    // Check for built-in Craft icon
                    if (isset($value['fontIcon'])) {
                        $icon = $value['fontIcon'];
                    }

                    $model = $this->_prepareNavModel([
                        'layoutId' => $layoutId,
                        'handle'   => $handle,
                        'label'    => $value['label'],
                        'order'    => $order,
                        'icon'     => $icon,
                        'url'      => $handle,
                    ]);

                    CpNav::$plugin->navigationService->save($model);
                }

                $order++;
            }
        } else {

            // Create an array of current navigation handles to easy check via in_array
            $currentNavHandles = array_column($currentNav, 'url');

            // A menu item exists in our records, but not in the menu - delete
            foreach ($generatedNav as $key => $value) {
                if (!\in_array($value['handle'], $currentNavHandles, false)) {
                    $navModel = CpNav::$plugin->navigationService->getByHandle($layoutId, $value['handle']);

                    if ($navModel) {
                        CpNav::$plugin->navigationService->delete($navModel);
                    }
                }
            }
        }
    }
public function regenerateNav ( $ layoutId , $ generatedNav , $ currentNav ) { // Find the extra or missing menu item if ( count ( $ generatedNav ) < count ( $ currentNav ) ) { $ order = 0 ; // A menu item exists in the menu, but not in our records - add foreach ( $ currentNav as $ value ) { if ( isset ( $ value [ 'url' ] ) ) { $ handle = str_replace ( UrlHelper :: url ( ) . '/' , '' , $ value [ 'url' ] ) ; } else { $ handle = StringHelper :: toKebabCase ( $ value [ 'label' ] ) ; } if ( ! isset ( $ generatedNav [ $ handle ] ) && ! CpNav :: $ plugin -> navigationService -> getByHandle ( $ layoutId , $ handle ) ) { $ icon = null ; // Check for custom icon (plugins) if ( isset ( $ value [ 'icon' ] ) ) { $ icon = $ value [ 'icon' ] ; } // Check for built-in Craft icon if ( isset ( $ value [ 'fontIcon' ] ) ) { $ icon = $ value [ 'fontIcon' ] ; } $ model = $ this -> _prepareNavModel ( [ 'layoutId' => $ layoutId , 'handle' => $ handle , 'label' => $ value [ 'label' ] , 'order' => $ order , 'icon' => $ icon , 'url' => $ handle , ] ) ; CpNav :: $ plugin -> navigationService -> save ( $ model ) ; } $ order ++ ; } } else { // Create an array of current navigation handles to easy check via in_array $ currentNavHandles = array_column ( $ currentNav , 'url' ) ; // A menu item exists in our records, but not in the menu - delete foreach ( $ generatedNav as $ key => $ value ) { if ( ! \ in_array ( $ value [ 'handle' ] , $ currentNavHandles , false ) ) { $ navModel = CpNav :: $ plugin -> navigationService -> getByHandle ( $ layoutId , $ value [ 'handle' ] ) ; if ( $ navModel ) { CpNav :: $ plugin -> navigationService -> delete ( $ navModel ) ; } } } } }
Creates or deletes records when the menu is updated by plugins

@param integer $layoutId
@param array   $generatedNav
@param array   $currentNav
Creates or deletes records when the menu is updated by plugins