Extensible link system
- Home
- Creating forms
- Extensible link system
LinkSpec
SproutCMS features a way to register arbitrary types of content which can be linked to from within any kind of record. Each type of content has its own link specification, LinkSpec
, which defines an interface for configuring the link, and what the resulting URL to the content will be.
For example, the LinkSpecPage
class defines a link to a page.
There are three methods which each LinkSpec needs to implement:
getEditForm - This provides the interface for selecting the item to link to. In the case of LinkSpecPage
, this provides a dropdown which selects the page ID.
isValid - This checks that the chosen item is valid.
getUrl - This converts the reference to the chosen item (usually an ID) to a URL used to access it.
To add an arbitrary new kind of content which can be linked to, you simply create a helper which extends LinkSpec, then register that helper as a selectable link specification.
The following example registers blog posts as a type of content which can be linked to:
modules/Blogs/sprout_load.php
<?php use Sprout\Helpers\Register; Register::linkspec('\\SproutModules\\Karmabunny\\Blogs\\Helpers\\LinkSpecBlog', 'Blog Post');
RteLibrary
SproutCMS also provides a means of linking to arbitrary content from within a rich text field. Each type of content has its own RteLibrary
implementation.
Each new RteLibrary
needs to implement two methods: search
, and browse
.
Both of these just return an array containing RteLibContainer
and/or RteLibObject
elements.
An RteLibContainer
represents a group (e.g. a folder, a category, etc.) while an RteLibObject
represents an item of content to link to (e.g. a page, blog post, etc.).
Each RteLibrary
type needs to be registered as well, e.g.:
sprout/sprout_load.php
<?php use Sprout\Helpers\Register; Register::rteLibrary('\\Sprout\\Helpers\\RteLibraryPages');