Breadcrumb ideas to update them automatically

Hi!

i am trying to add breadcrumbs Breadcrumb Component - Vaadin Add-on Directory. I have a custom Annotation HeaderTitle which i tried to use as a breadcrumb.

Currently i am cunfused and need some input.

the NavigationObserver looks like if it is too early, then the dom is not rendered

Layout:

 @Override
    public void beforeEnter(BeforeEnterEvent event) {
        if (!hasAccess(event.getNavigationTarget())) {
            event.rerouteTo(AccessDeniedView.class);
        }

        setBreadcrumbFromLocation(event.getLocation());
        Class<?> viewClass = event.getNavigationTarget();
        HeaderTitle pageTitleAnnotation = viewClass.getAnnotation(HeaderTitle.class);
        if (pageTitleAnnotation != null) {
            dynamicSpan.setText(pageTitleAnnotation.value());
        } else {
            dynamicSpan.setText(""); // Fallback-Titel
        }
    }

    private boolean hasAccess(Class<?> navigationTarget) {
        return securityUtils.isAccessGrantedToView(navigationTarget);
    }


    public void setBreadcrumbFromLocation(Location location) {
        breadcrumbs.removeAll();

        // Startpunkt
        breadcrumbs.add(new Breadcrumb("Home", "/"));

        StringBuilder pathBuilder = new StringBuilder();
        List<String> segments = location.getSegments();

        for (int i = 0; i < segments.size(); i++) {
            String segment = segments.get(i);
            pathBuilder.append("/").append(segment);

            String label = mapSegmentToLabel(segment);
            String href = pathBuilder.toString();

            // Letztes Element: kein Link
            if (i == segments.size() - 1) {
                breadcrumbs.add(new Breadcrumb(label));
            } else {
                breadcrumbs.add(new Breadcrumb(label, href));
            }
        }
    }

Thats way to difficult, i think there is a easier way to get it done. If someone already implemented this addon, i would really aprreciate some input how you have handled it.

I would assume afterNavigation is the correct lifecycle callback for this, as that is called in the end of navigation cycle, beforeEnter is premature for this.

2 Likes

Yeah indeed, that was an easy one :man_facepalming: :grin:, thank you!