Navigation

  • index
  • next |
  • previous |
  • Sulu 3.x documentation »
  • Cookbook »
  • Provider for custom link type

Provider for custom link type¶

LinkProvider services are used to resolve data for different types of internal links. The services are used in different parts of the system, including the Link property type (see Link), the internal link plugin of the CKEditor and the <sulu-link> tag inside of twig-templates (see sulu-link).

The LinkProvider::preload method is responsible for resolving an array of LinkItem instances for the given arguments. Each LinkItem consists of the following properties:

  • id

  • title

  • url

  • published

Example¶

To register a LinkProvider service for a custom link type, you create a service that implements the LinkProviderInterface and tag it with the respective resource key: <tag name="sulu.link.provider" alias="{resourceKey}"/>

The LinkProvider::getConfigurationBuilder method must return a LinkConfigurationBuilder that defines how entities of this link type are selected in the administration interface.

<?php

namespace App\Link;

use Sulu\Bundle\MarkupBundle\Markup\Link\LinkConfigurationBuilder;
use Sulu\Bundle\MarkupBundle\Markup\Link\LinkItem;
use Sulu\Bundle\MarkupBundle\Markup\Link\LinkProviderInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

class LinkProvider implements LinkProviderInterface
{
    public function __construct(
        private TranslatorInterface $translator,
    ) {
    }

    public function getConfigurationBuilder(): LinkConfigurationBuilder
    {
        return LinkConfigurationBuilder::create()
            ->setTitle($this->translator->trans('app.custom_link_type', [], 'admin'))
            ->setResourceKey('custom_resource_key') // the resourceKey of the entity that should be loaded
            ->setListAdapter('column_list')
            ->setDisplayProperties(['title'])
            ->setOverlayTitle($this->translator->trans('app.custom_link_overlay_title', [], 'admin'))
            ->setEmptyText($this->translator->trans('app.no_custom_link_selected', [], 'admin'))
            ->setIcon('su-document');
    }

    public function preload(array $hrefs, string $locale, bool $published = true): iterable
    {
        if (0 === \count($hrefs)) {
            return [];
        }

        $items = ...; // load items by id
        $result = [];
        foreach ($items as $item) {
            $result[] = new LinkItem($item->getId(), $item->getTitle(), $item->getUrl(), $item->isPublished());
        }

        return $result;
    }
}

Register the service and tag it with your resource key in config/services.yaml:

App\Link\CustomLinkProvider:
    tags:
        - { name: sulu.link.provider, alias: custom_resource_key }

Custom target options¶

By default, link targets _blank, _self, _parent and _top are available. Use setTargets() on the builder to restrict or replace the available options:

->setTargets(['_blank', '_self'])

The admin UI translates each target value using the key pattern sulu_admin.link_target{value} — for example _blank becomes sulu_admin.link_target_blank.

To add a new target or override an existing label, add the key to your project’s admin translation file (e.g. translations/admin.en.json):

{
    "sulu_admin.link_target_blank": "Open in new tab",
    "sulu_admin.link_target_self": "Open in same tab",
    "sulu_admin.link_target_popup": "Open in popup"
}

Then register the new value in setTargets():

->setTargets(['_blank', '_self', '_popup'])

Custom overlay via JavaScript¶

If the entities of the new link type cannot be selected via a standard list, you can replace the default list overlay with a custom React component. Register your component in the linkOverlayRegistry before the admin bundle initialises its link types. The registry picks it up automatically when it processes the PHP-provided internalLinkTypes configuration:

import linkOverlayRegistry from 'sulu-admin-bundle/containers/Link/registries/linkOverlayRegistry';

linkOverlayRegistry.add('custom_resource_key', CustomLinkTypeOverlay);

Logo

Table of Contents

  • The Book
  • Cookbook
    • Caching with Varnish
    • Maintenance Mode
    • Securing your application
    • Pass additional data to your template using a custom controller
    • Custom error page
    • Extend Entities
    • Extending DimensionContent
    • How to implement an entity extensible?
    • Elasticsearch
    • SmartContentProvider for SmartContent
    • Filter pages by a property using a custom SmartContent DataProvider
    • System-Collections
    • Adding new Webspace
    • How to manage analytics?
    • How to change the default locale provider?
    • Using default snippets for managing page-independent data
    • How to Override Sulu’s default Directory Structure
    • How to deactivate the RequestAnalyzer?
    • How to use the RequestAnalyzer with ESI requests?
    • Server Configuration
    • How to add localizations with the localization provider?
    • Provider for custom link type
    • Provider for XML-Sitemap
    • PropertyResolver and ResourceLoader
    • Improve Sitemap Speed
    • HTTPS
    • Generating thumbnails for video files with ffmpeg
    • Generating thumbnails for pdf files with ghostscript
    • Store Media in an external Storage
    • Using Webpack Encore for your website assets
    • Adding tabs to Sulu’s Admin UI
    • Display values from a service in a select
    • Provide Sulu twig template attributes in your controller
    • User context caching
    • Building the administration interface frontend application
    • Running Sulu in a Multi-Server Setup
  • Reference
  • Bundles
  • Developer Guide
  • Upgrades

Related Topics

  • Documentation overview
    • Cookbook
      • Previous:
        How to add localizations with the localization provider?
      • Next:
        Provider for XML-Sitemap

This Page

  • Show Source

Quick search

Navigation

  • index
  • next |
  • previous |
  • Sulu 3.x documentation »
  • Cookbook »
  • Provider for custom link type