Navigation

  • index
  • next |
  • previous |
  • Sulu 2.4 documentation »
  • Cookbook »

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 content-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}"/>

If the entities of the new link type should be selected via a list in the administration interface, the LinkProvider::getConfiguration method must return the configuration that is used for the list.

<?php

namespace AppBundle\Link;

use Sulu\Bundle\MarkupBundle\Markup\Link\LinkConfigurationBuilder;
use Sulu\Bundle\MarkupBundle\Markup\Link\LinkItem;
use Sulu\Bundle\MarkupBundle\Markup\Link\LinkProviderInterface;

class LinkProvider implements LinkProviderInterface
{
    /**
     * {@inheritdoc}
     */
    public function getConfiguration()
    {
        return LinkConfigurationBuilder::create()
            ->setTitle($this->translator->trans('sulu_page.pages', [], 'admin'))
            ->setResourceKey('...') // the resourceKey of the entity that should be loaded
            ->setListAdapter('column_list')
            ->setDisplayProperties(['title'])
            ->setOverlayTitle($this->translator->trans('sulu_page.single_selection_overlay_title', [], 'admin'))
            ->setEmptyText($this->translator->trans('sulu_page.no_page_selected', [], 'admin'))
            ->setIcon('su-document')
            ->getLinkConfiguration();
    }

    /**
     * {@inheritdoc}
     */
    public function preload(array $hrefs, $locale, $published = true)
    {
        if (0 === count($hrefs)) {
            return [];
        }

        $items = ...; // load items by id
        foreach ($items as $item) {
            $result[] = new LinkItem(...); // create link-item foreach item
        }

        return $result;
    }
}

If the entities of the new link type cannot be selected via a list, the LinkProvider::getConfiguration method of your service must return null and you need to register a custom overlay via the linkTypeRegistry Javascript service:

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

linkTypeRegistry.add('custom_resource_key', CustomLinkTypeOverlay, translate('app.custom_translation_key'));

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
    • How to implement an entity extensible?
    • Migrate existing content data on template change
    • Using Jackrabbit
    • Using Elasticsearch
    • DataProvider 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
    • 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 2.4 documentation »
  • Cookbook »