Developers

Custom Link Types

Rich-text cells let editors link to entries, categories and assets, alongside standard URLs. A custom link type adds your own element type to that list, such as products from a module or another plugin. Editors can then pick those elements from the link menu, and rendered tables always use each element’s current URL.

A link type is an existing element type class. Table Maker doesn’t need a separate class, but the element type must meet a few requirements.

  • Its refHandle() method returns a handle made of letters, numbers and underscores. Links are saved with this handle and the element’s ID, so keep it stable.
  • It’s registered with Craft through Elements::EVENT_REGISTER_ELEMENT_TYPES, so Craft can resolve its reference tags.
  • Its elements return a URL from getUrl(). If hasUris() returns true, the element selector only shows elements that have a URI.
  • Its sources() method returns the sources editors should pick from. Table Maker offers every source the current user can see in an element selector modal.

Table Maker finds link types through the registerRichTextLinkTypes event. Register your element type from your module or plugin’s init() method.

use craft\events\RegisterComponentTypesEvent;
use modules\sitemodule\elements\Product;
use verbb\tablemaker\fields\TableMakerField;
use yii\base\Event;

Event::on(TableMakerField::class, TableMakerField::EVENT_REGISTER_RICH_TEXT_LINK_TYPES, function(RegisterComponentTypesEvent $event) {
    $event->types[] = Product::class;
});

The element type then appears in each Table Maker field’s Rich Text Link Types setting. Fields set to All offer it straight away.

If you later stop registering the element type, its saved links keep pointing to the URL saved when each link was added, but no longer follow URI changes.