Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add api endpoint for providing a breadcrumb #70

Open
alexander-schranz opened this issue Feb 8, 2021 · 2 comments
Open

Add api endpoint for providing a breadcrumb #70

alexander-schranz opened this issue Feb 8, 2021 · 2 comments
Labels
Feature New functionality not yet included in the Bundle
Milestone

Comments

@alexander-schranz
Copy link
Member

alexander-schranz commented Feb 8, 2021

Like the sulu_breadcrumb twig extension it should be possible to provide a breadcrumb over an endpoint. It should work similar to the navigation context api. An example api endpint url could be /api/breadcrumb/<uuid>.

Another possibility could be that we add the breadcrumb directly to the json response example:

/my-page-url.json?with-breadcrumb=true

Would return the breadcrumb on the root level:

{
      "id": "<uuid>",
      "breadcrumb": [{
               "uuid": "uuid",
               "title": "Homepage",
               "url": "/",
      }],
      "content": {},
      "view": {},
}

Update:

We want to provide the breadcrumb as a flag in the bundle and directly then via the website controller and an extension pint point in #100. So we avoid an additional request for this kind of data. I'm not yet sure if the extension point need to be in the structureresolver or in the headless controller. I already added a comment to the exist PR to update the interface for our usecase with the breadcrumb.

@alexander-schranz alexander-schranz added the Feature New functionality not yet included in the Bundle label Feb 8, 2021
@niklasnatter
Copy link
Contributor

niklasnatter commented Aug 20, 2021

We recently added the breadcrumbs of a page directly to the .json response in one of our projects. To do this, we used the following custom controller:

<?php

namespace App\Controller\Website;

use Sulu\Bundle\HeadlessBundle\Controller\HeadlessWebsiteController as SuluHeadlessWebsiteController;
use Sulu\Bundle\WebsiteBundle\Navigation\NavigationMapperInterface;
use Sulu\Component\Content\Compat\StructureInterface;

class HeadlessWebsiteController extends SuluHeadlessWebsiteController
{
    /**
     * @var NavigationMapperInterface
     */
    private $navigationMapper;

    public function __construct(
        NavigationMapperInterface $navigationMapper
    ) {
        $this->navigationMapper = $navigationMapper;
    }

    protected function resolveStructure(StructureInterface $structure): array
    {
        $data = parent::resolveStructure($structure);
        $data['breadcrumbs'] = $this->getBreadcrumbs($structure);

        return $data;
    }

    /**
     * @return array<array{
     *     id: string,
     *     title: string,
     *     url: string,
     * }>
     */
    private function getBreadcrumbs(StructureInterface $structure): array
    {
        $breadcrumbs = [];

        foreach ($this->navigationMapper->getBreadcrumb(
            $structure->getUuid(),
            $structure->getWebspaceKey(),
            $structure->getLanguageCode()
        ) as $item) {
            $breadcrumbs[] = [
                'uuid' => $item->getUuid(),
                'title' => $item->getTitle(),
                'url' => $item->getUrl(),
            ];
        }

        return $breadcrumbs;
    }
}

And register that controller in our templates:

    <controller>App\Controller\Website\HeadlessWebsiteController::indexAction</controller>

@alexander-schranz
Copy link
Member Author

The problem with using getBreadCrumb directly in the HeadlessWebsiteController that it will not work with Articles in case of articles we need to get the breadcrumb not by the article uuid instead of the page tree parent page uuid.

As discussed with @chirimoya we should enable or disable the breadcrumb via sulu_headless option currently.

As we have different cases Page and Article Breadcrumb we maybe adopt the community pull request here: #100

But instead of the current implementation which has a extension->getKey() the provider should just be able to return all data and overwrite existing props e.g.:

return ['breadcrumb' => $this->getBreadCrumb()];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature New functionality not yet included in the Bundle
Projects
None yet
Development

No branches or pull requests

2 participants