Multi-purpose link plugin for Craft CMS
One link field to replace them all, a multi-purpose link fieldtype for Craft CMS.
Linkit for Craft 3 is now available in the Plugin Store (Linkit on GitHub)
See upgrade details below.
This plugin adds a fieldtype which links to all sorts of stuff, Link It can currently link to:
Link It supports Matrix fields and allows you to:
To install FruitLinkIt, follow these steps:
fruitlinkit
directory into your craft/plugins
directoryFruitLinkIt requires Craft 2.5, for pre 2.5 support see releases.
Always ensure you have tested any new releases in a development environment.
Upgrading from a version prior to 2.3.0 please take extra care to backup and test in a development environment. FruitLinkIt will run a migration that automatically updates any existing field settings and content after which it will remove the old version.
NB. Existing templates will not break but you will get a load of deprecator errors, take a look at the readme file for updated usage guides.
Add a new Link It field and configure it. Easy.
Just output the custom field to get a ready built html link
{{ entry.linkItField }}
or in full
{{ entry.linkItField.htmlLink }} or {{ entry.linkItField.getHtmlLink() }}
Customised html link
{% set attributes = {
title: 'Custom Title',
target: '_self',
class: 'my-class',
"data-custom": 'custom-data-attribute'
} %}
{{ entry.linkItField.htmlLink(attributes) }}
Each Link it field returns a LinkIt model with the following attributes / methods available
{{ entry.linkItField.type }} (email, custom, tel, entry, category or asset)
{{ entry.linkItField.target }}
{{ entry.linkItField.url }} or {{ entry.linkItField.getUrl() }}
{{ entry.linkItField.text }} or {{ entry.linkItField.getText() }}
If your link is an element link (asset, entry, category) you also have access to the following:
{{ entry.linkItField.element }} or {{ entry.linkItField.getElement() }}
or specific element types
{{ entry.linkItField.entry }} or {{ entry.linkItField.getEntry() }}
{{ entry.linkItField.asset }} or {{ entry.linkItField.getAsset() }}
{{ entry.linkItField.category }} or {{ entry.linkItField.getCategory() }}
{{ linkItField.linkText }} use {{ linkItField.text }} instead.
{{ linkItField.link }} use {{ linkItField }} or {{ linkItField.htmlLink }} instead.
{{ linkItField.email }} use {{ linkItField.url }} instead.
{{ linkItField.custom }} use {{ linkItField.url }} instead.
{{ linkItField.tel }} use {{ linkItField.url }} instead.
There are two hooks that allow plugins to add their own Element Types to Link It:
linkit_registerElementTypes
Registers the ElementType(s) with LinkIt and provides all the data needed for the field settings.
It should return an array of element types you want to register and should be in the following format:
/**
* @return array
*/
public function linkit_registerElementTypes()
{
return array(
'my_element' => array(
'name' => Craft::t('My Element'),
'pluralName' => Craft::t('My Elements'),
'selectionLabelDefault' => Craft::t('Select an element'),
'emptyInputErrorMessage' => Craft::t('Please select an element'),
'elementType' => 'MyPlugin_MyElement',
'sources' => array(
array(
'label' => Craft::t('All of my elements'),
'value' => '*'
),
array(
'label' => Craft::t('Element source 1'),
'value' => 'myElementGroup:1'
),
array(
'label' => Craft::t('Element source 2'),
'value' => 'myElementGroup:2'
)
)
)
),
'my_other_element' => array(
...
);
}
linkit_getElementData
Fetches the element data for a given $type
and $id
:
/**
* @param string $type
* @param int $id
* @return array|false
*/
public function linkit_getElementData($type, $id)
{
// Return false if there is no `$type` or `$id` set
if (!$type || !$id) {
return false;
}
switch ($type) {
case 'my_element':
$myElement = craft()->myPlugin_myElements->getMyElementById($id);
if ($myElement) {
return array(
'url' => $myElement->getUrl(),
'text' => $myElement->title,
'element' => $myElement
);
} else {
// There was no element, so be sure to return false
return false;
}
break;
case 'my_other_element':
...
break;
// We don’t want to return anything for unsupported types
default:
return false;
break;
}
}
Some things to do, and ideas for potential features:
Complete rewrite for Craft 2.5.x, same functionality with some UI tweaks and
Amongst other stuff :)
Brought to you by Fruit Studios
Copyright 2014 Fruit Studios Ltd