Git Product home page Git Product logo

linkit's Introduction

Linkit

Linkit plugin for Craft 4

One link field to rule them all...

This plugin adds a custom fieldtype which enables support for linking to email addresses, telephone numbers, URL's, Craft element types and more.

New for Craft 4

  • GraphQL Support
  • Eager Loading Support

Requirements

This plugin requires Craft CMS 4.0.0 or later.

Installation

Plugin Store

Log into your control panel, hit up the 'Plugin Store', search for this plugin and install.

Composer

Open terminal, go to your Craft project folder and use composer to load this plugin. Once loaded you can install via the Craft Control Panel, go to Settings → Plugins, locate the plugin and hit “Install”.

cd /path/to/project
composer require presseddigital/linkit

Configuring Linkit

Once installed, create a new field and choose the Linkit fieldtype. You'll then have the option of configuring what link type will be available to this field.

The following link types are available:

Basic

  1. Email Address
  2. Telephone Number
  3. URL

Social

  1. Twitter
  2. Facebook
  3. Instagram
  4. LinkedIn

Elements

  1. Entry
  2. Category
  3. User
  4. Asset
  5. Product

Each link type has additional option to allow further customisation. For example, the User link type allows you to set a default path...

Linkit

You can also customise the dropdown labels that appear on the field.

Linkit

Using Linkit

Template Variables (Basic Use)

Output the custom field to get a ready built html link...

{{ entry.linkItField | raw }}

or in full...

{{ entry.linkItField.link }} or {{ entry.linkItField.getLink() }}

Create a customised html link...

{% set attributes = {
    title: 'Custom Title',
    target: '_self',
    class: 'my-class',
    "data-custom": 'custom-data-attribute'
} %}
{{ entry.linkItField.link(attributes) }}

Template Variables (Advanced Use)

Each Linkit field returns a Linkit model with the following tags...

{{ entry.linkItField.url }} or {{ entry.linkItField.getUrl() }}
{{ entry.linkItField.text }} or {{ entry.linkItField.getText() }}

{{ entry.linkItField.type }}
{{ entry.linkItField.typeHandle }}

{{ entry.linkItField.hasElement }}
{{ entry.linkItField.available }} or {{ entry.linkItField.isAvailable() }}

{{ entry.linkItField.target }}
{{ entry.linkItField.targetString }}
{{ entry.linkItField.linkAttributes }}
{{ entry.linkItField.customText }}

If your link is an element link you also have access to the following...

{{ entry.linkItField.element }} or {{ entry.linkItField.getElement() }}

or via the specific element types...

{{ entry.linkItField.entry }} or {{ entry.linkItField.getEntry() }}
{{ entry.linkItField.asset }} or {{ entry.linkItField.getAsset() }}
{{ entry.linkItField.category }} or {{ entry.linkItField.getCategory() }}
{{ entry.linkItField.user }} or {{ entry.linkItField.getUser() }}
{{ entry.linkItField.product }} or {{ entry.linkItField.getProduct() }}

Example Usage

If you have created a field called 'linkItField' with a User link type like so...

Linkit

{{ entry.linkItField.link }}

would output <a href="/profile/USERNAME">Visit Profile</a> which is the default user path that is created in the setting and the user would be available at...

{{ entry.linkItField.user }}

GraphQL

Linkit supports querying subfields with GraphQl, the following fields are available, take a look at the Craft GraphQL explorer to have a play with the return values:

myLinkitField {
  label
  type
  typeHandle
  available
  link
  url
  text
  target
  element {
    id
    title
  }
}

A Note On Element Status

To match first party element fieldtypes, Linkit now links to and allows users to select disabled elements when using element link types, we have added a new method to allow you to determine if a link is available (enabled) for the current site, as such the following will return a boolean value

{{ entry.linkItField.avialable }} or {{ entry.linkItField.isAvialable() }}

you can use this to work out if you should display a link

{% if entry.linkItField.available %}
    {{ entry.linkItField.link | raw }}
{% endif %}

You can still access the linked element and any other attributes should you need to access a disabled element

{# So long as it exists the link is always returned irrelevant of status #}
{% set element = entry.linkItField.element %}
{% if element %}
    {{ element.title }} - {{ element.url }}
{% endif %}

Custom Link Types

You can easily create your own link types,

Full documenation is coming soon, for now, take a look at the models folder. Each link type is a seperate model, just extend Link or ElementLink, depending on your needs, and everything will be set up for you.

Hook up the requirements and register custom link types in your plugin (or modules) init()...

<?php

...

use presseddigital\linkit\Linkit;
use presseddigital\linkit\events\RegisterLinkTypesEvent;
use presseddigital\linkit\services\LinkitService;

use developer\plugin\models\CustomType;

...

public function init()
{
    parent::init();

    Event::on(LinkitService::class, LinkitService::EVENT_REGISTER_LINKIT_FIELD_TYPES, function (RegisterLinkTypesEvent $event) {
        $event->types[] = new CustomType();
    });

}

If you think they are super useful and you agree we can look to add them to the core plugin for everyone to use.

Brought to you by Pressed Digital

linkit's People

Contributors

angrybrad avatar ben-callaway avatar myleshyson avatar samhibberd avatar shornuk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

linkit's Issues

When using Phone / Mail, text includes mailto:/tel: prefix

Hi, I just used the phone and mail fields for the first time. I encounter the issue that when outputting them this way:

<td><a href="{{ row.fieldHandle.url }}">{{ row.fieldHandle.text }}</a></td>

the result looks like this

<td><a href="tel:+49 (0) 611 304 021">tel:+49 (0) 611 304 021</a></td>

image

Am I getting something wrong, or is this a bug?

Displaying title of related entry; conflict with custom text

I think I'm seeing a couple issues if I have an entries link that allows the user to override the link text.

For example:
Linkit field in a Matrix field called theLink
I link to a related entry titled The Related Entry.
The default link text is "Learn More"
I override the link text to say "Check It out"

Using this:

{% for item in entry.matrix %}
  {% <h2>{{ item.theLink.title }}</h2> 'title' throws error, so I'm using 'text' instead %}
  <h2>{{ item.theLink.text }}</h2>
  <a href="{{ item.theLink.url }}">{{ item.theLink.text }}</a>
{% endfor %}

I'm trying to display both the related entry's title, and custom link text that is not the same as the related entry title. My desired output is:

The Related Entry
Learn More

If I were to add custom link text, the desired output should be:

The Related Entry
Check it out

As it is, if I don't override the link text, I get this:

The Related Entry
The Related Entry

If I do override the link text, I get:

Check It Out
Check It Out

By default text seems to pull the related entry's title ("The Related Entry"), though I was expecting it to display the custom text for the link. Something like item.theLink.title isn't available.

I guess I'm not clear on how to get the info for the linked entry (the title if nothing else), and still allow for the default custom text and an override for that text.

Also...is text an alias of customText?

How to display conditionally?

I've just started using LinkIt with Craft 3. In Craft 2, I could conditionally show the linkit field using {% if entry.linkItField|length %}. In Craft 3, the button default text is showing even if an entry / element hasn't been selected in the control panel. Is this a bug, or has the way to check for length changed? I need to show the linkit field only when a user has selected something to link to.

Error: Couldn't install fruitstudios/linkit: Unable to determine the base path

So, I can't seem to install this plugin. I've tried several times. I've also tested other plugins to see if they trigger this error (they don't).

Error: Couldn't install fruitstudios/linkit: Unable to determine the base path

Output: Loading composer repositories with package information
Updating dependencies
Package operations: 1 install, 0 updates, 0 removals

  • Installing fruitstudios/linkit (1.0.3): Loading from cache
  • Removing fruitstudios/linkit (1.0.3)

Disabling an entry doesn't disable related linkit fields

My expectation is that Linkit fields (that use Entry, Category, etc.) should be disabled when a related entry is disabled. I think this is how it worked in Linkit for Craft 2 anyway.

This should also work based on each locale's entry status for localized sites.

Tested on two Craft 3 installs. One that is localized. Both produce the same results. The Linkit field still displays on the front-end when the entry is disabled.

Multi-Site

Hi, I'm just giving Link-It a test-run on a multi-site Craft3 Installation. I'm using it in a Matrix Field. The Problem is, that it only displays Entries from the Main Site, not the selected Site.

So if you select another Site which has different Enries, the relevant Entries are not selectable.

C2->C3 upgrade error: Undefined index

Hey guys,

Just upgrading a site from Craft v2 to v3, and I can't seem to install your plugin after doing a composer require (latest v1.0.8, cleared cache, etc). It seems to failing on the data migration step...

When I look in the web.log file, I can see the following exception...

2018-05-23 14:24:43 [127.0.0.1][46][-][error][craft\db\MigrationManager::migrateUp] Failed to apply Install (time: 0.002s). Output:
    > delete from {{%plugins}} ... done (time: 0.000s)
Exception: Undefined index: types (/Users/michael/Sites/v-web/vendor/fruitstudios/linkit/src/migrations/Install.php:80)
#0 /Users/michael/Sites/v-web/vendor/craftcms/cms/src/web/ErrorHandler.php(84): yii\base\ErrorHandler->handleError(8, 'Undefined index...', '/Users/michael....', 80)
#1 /Users/michael/Sites/v-web/vendor/fruitstudios/linkit/src/migrations/Install.php(80): craft\web\ErrorHandler->handleError(8, 'Undefined index...', '/Users/michael....', 80, Array)
#2 /Users/michael/Sites/v-web/vendor/fruitstudios/linkit/src/migrations/Install.php(54): fruitstudios\linkit\migrations\Install->_migrateFieldSettings(Array)
#3 /Users/michael/Sites/v-web/vendor/fruitstudios/linkit/src/migrations/Install.php(22): fruitstudios\linkit\migrations\Install->_upgradeFromCraft2()
#4 /Users/michael/Sites/v-web/vendor/craftcms/cms/src/db/Migration.php(56): fruitstudios\linkit\migrations\Install->safeUp()
#5 /Users/michael/Sites/v-web/vendor/craftcms/cms/src/db/MigrationManager.php(243): craft\db\Migration->up(true)
#6 /Users/michael/Sites/v-web/vendor/craftcms/cms/src/base/Plugin.php(122): craft\db\MigrationManager->migrateUp(Object(fruitstudios\linkit\migrations\Install))
#7 /Users/michael/Sites/v-web/vendor/craftcms/cms/src/services/Plugins.php(472): craft\base\Plugin->install()
#8 /Users/michael/Sites/v-web/vendor/craftcms/cms/src/controllers/PluginsController.php(48): craft\services\Plugins->installPlugin('linkit')
#9 [internal function]: craft\controllers\PluginsController->actionInstallPlugin()
#10 /Users/michael/Sites/v-web/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#11 /Users/michael/Sites/v-web/vendor/yiisoft/yii2/base/Controller.php(157): yii\base\InlineAction->runWithParams(Array)
#12 /Users/michael/Sites/v-web/vendor/craftcms/cms/src/web/Controller.php(103): yii\base\Controller->runAction('install-plugin', Array)
#13 /Users/michael/Sites/v-web/vendor/yiisoft/yii2/base/Module.php(528): craft\web\Controller->runAction('install-plugin', Array)
#14 /Users/michael/Sites/v-web/vendor/craftcms/cms/src/web/Application.php(273): yii\base\Module->runAction('plugins/install...', Array)
#15 /Users/michael/Sites/v-web/vendor/craftcms/cms/src/web/Application.php(521): craft\web\Application->runAction('plugins/install...', Array)
#16 /Users/michael/Sites/v-web/vendor/craftcms/cms/src/web/Application.php(257): craft\web\Application->_processActionRequest(Object(craft\web\Request))
#17 /Users/michael/Sites/v-web/vendor/yiisoft/yii2/base/Application.php(386): craft\web\Application->handleRequest(Object(craft\web\Request))
#18 /Users/michael/Sites/v-web/web/index.php(21): yii\base\Application->run()
#19 /Users/michael/.composer/vendor/laravel/valet/server.php(147): require('/Users/michael....')
#20 {main}

2018-05-23 14:24:43 [127.0.0.1][46][-][trace][yii\db\Transaction::rollBack] Roll back transaction

Any ideas on how to work around this?

Link class in the database?

Should I store link class: in database, will it be quicker to grab. Could then do a getLinkType() function if needed.

Error on install after 3.1 update

When installing & activating the plugin on a 3.1 site, we get the following error on the plugin overview screen:

Invalid plugin – craft\errors\InvalidPluginException
No plugin exists with the handle "fruit-link-it".

This happens during an update from 2.7.4 to 3.1.3.
Tried running the entire migration/update again with the plugin installed but same error.

Craft setup fails when you have one or more linkit fields and uses project.yaml

To reproduce:

  1. composer create-project craftcms/craft craft3_fresh
  2. cd craft3_fresh
  3. Create database
  4. ./craft setup
  5. composer require fruitstudios/linkit
  6. ./craft install/plugin linkit
  7. Create a linkit field
  8. Drop database
  9. Run ./craft setup again
    ...
> applying existing project config ... done
Exception: No primary site exists (/home/nettum/Sites/temp/craft3_fresh/vendor/craftcms/cms/src/services/Sites.php:496)
...
Exception 'craft\errors\MigrationException' with message 'An error occurred while executing the "craft\migrations\Install migration: No primary site exists

This seems to happen because of $projectConfig->muteEvents = true; in the Install.php migration.
If I set this to false or remove it all together things seems to work.

project.yaml:

dateModified: 1551862082
email:
  fromEmail: [email protected]
  fromName: 'craft3 fresh'
  transportType: craft\mail\transportadapters\Sendmail
fieldGroups:
  07fe101c-b8a2-4d7d-888a-a329255d0283:
    name: Common
fields:
  7162b828-f22d-490c-ac7f-bd8a28e63b40:
    contentColumnType: text
    fieldGroup: 07fe101c-b8a2-4d7d-888a-a329255d0283
    handle: myLink
    instructions: ''
    name: 'My Link'
    searchable: true
    settings:
      allowCustomText: ''
      allowTarget: ''
      defaultText: ''
      selectLinkText: ''
      types:
        fruitstudios\linkit\models\Asset:
          customLabel: ''
          customSelectionLabel: ''
          enabled: ''
          sources: '*'
        fruitstudios\linkit\models\Category:
          customLabel: ''
          customSelectionLabel: ''
          enabled: ''
          sources: '*'
        fruitstudios\linkit\models\Email:
          customLabel: ''
          customPlaceholder: ''
          enabled: ''
        fruitstudios\linkit\models\Entry:
          customLabel: ''
          customSelectionLabel: ''
          enabled: '1'
          sources: '*'
        fruitstudios\linkit\models\Facebook:
          customLabel: ''
          customPlaceholder: ''
          enabled: ''
        fruitstudios\linkit\models\Instagram:
          customLabel: ''
          customPlaceholder: ''
          enabled: ''
        fruitstudios\linkit\models\LinkedIn:
          customLabel: ''
          customPlaceholder: ''
          enabled: ''
        fruitstudios\linkit\models\Phone:
          customLabel: ''
          customPlaceholder: ''
          enabled: ''
        fruitstudios\linkit\models\Twitter:
          customLabel: ''
          customPlaceholder: ''
          enabled: ''
        fruitstudios\linkit\models\Url:
          allowAlias: '1'
          allowHash: '1'
          allowMailto: '1'
          allowPaths: '1'
          customLabel: ''
          customPlaceholder: ''
          enabled: '1'
        fruitstudios\linkit\models\User:
          customLabel: ''
          customSelectionLabel: ''
          enabled: ''
          sources: '*'
          userPath: ''
    translationKeyFormat: null
    translationMethod: none
    type: fruitstudios\linkit\fields\LinkitField
plugins:
  linkit:
    edition: standard
    enabled: true
    schemaVersion: 1.0.8
siteGroups:
  3a3339d0-b1f5-4243-a52f-a8d017eb5470:
    name: 'craft3 fresh'
sites:
  da69fd41-8b78-4ea7-a321-c2ffe849b42e:
    baseUrl: $DEFAULT_SITE_URL
    handle: default
    hasUrls: true
    language: en-US
    name: 'craft3 fresh'
    primary: true
    siteGroup: 3a3339d0-b1f5-4243-a52f-a8d017eb5470
    sortOrder: 1
system:
  edition: solo
  live: true
  name: 'craft3 fresh'
  schemaVersion: 3.1.25
  timeZone: America/Los_Angeles
users:
  allowPublicRegistration: false
  defaultGroup: null
  photoSubpath: ''
  photoVolumeUid: null
  requireEmailVerification: true

ReflectionException Class custom does not exist

I'm guessing this has something to do with existing data from Craft 2. Is there any way to migrate old data to the Craft 3 plugin?

in /var/www/vendor/fruitstudios/linkit/src/fields/LinkitField.php at line 105 -
fruitstudios\linkit\fields\LinkitField::_getLinkTypeModelByType('custom')

Ease up on URL validation (iTunes store links)

One thing we've noticed is that custom URL fields don't accept certain URLs, if they're a bit unusual. For instance, links to the Apple App Store:

itms-apps://itunes.apple.com/us/app...

Is a valid link, and works, but cannot be saved in a Linkit field.

I would think either turning off URL validation, accepting these sort of links, or being a bit more lenient on URL structures would all be valid options.

Thanks!

Element link types display as the non translated version after save

When selecting an entry when editing content in a locale, the translated title of the entry is shown. After saving a Linkit field, the default locale's version is displayed.

I compared this to how Craft handles entry fields and locales show the translated version after save, as expected.

Before save (after selecting an entry):
screen shot 2018-04-27 at 10 13 50 am

After save:
screen shot 2018-04-27 at 10 14 12 am

Cannot install in Craft 3.1 beta

Hi,

Attempting to install the plugin in Craft 3.1.0 beta 5.1 results in the following error: Couldn’t install plugin. (super helpful)

I took a jaunt through the logs to see if there was an error that jumped out at me, but didn't spot anything in particular. I'd be happy to send along the log, if that'd be helpful.

entry.fieldhandle.type returns a long string

I'm trying to show a different icon depending on whether the user has selected "URL" or "Asset" for the linkit type. If I call {{ entry.fieldhandle.type }} in the template, I'd expect to get url or asset, but I'm getting this:

fruitstudios\linkit\models\Url
fruitstudios\linkit\models\Asset

Feature Request: Add ability to manage Entry and Category sources outside of the field itself

It's difficult keeping LinkIt field source options in sync with each other with several LinkIt fields used throughout various Super Tables and Matrix fields. If a new entries section gets added and you want users to be able to link to it, you have to edit every single LinkIt field's options.

Having the ability to create source groups and manage through a settings panel would be super helpful.

Default text doesnt seem to out put

for some reason the default text on our links werent outputting. We had it say learn more by default on a matrix field but instead of saying that it was the title of the element it was linking to when i ouput

{{block.ctaLink.text}}

but loving the plugin so far, solves so many problems

"All" option

I find I use Link It quite a lot when it comes to making buttons or navigation in Craft.

Previously on Craft 2 there wasn't as many options, but now theres quite a lot.

It feels quite a long task each time creating these fields and switching on pretty much every element.

It would be good if you could just tick 'All' and it switches all options on. That would speed up development a lot.

Or, if theres a way of creating 'Templates' for field settings that you could just apply to the field. E.g. I may want a template called 'Social' which only has Facebook, Twitter and Instagram option ticked for example.

Updating to Craft 3.1.x removes Asset and Entry sources

Updating a site to 3.1.5 results in LinkIt losing all its checked options for Entry and Assets under a linkit field. Fields that previous had "Entry" enabled as an option with some checkboxes turned on are now all unchecked on all fields - both as normal LinkIt fields, and LinkIt fields inside Matrix fields.

LinkIt 1.1.6
Craft 3.1.5

Crashes Solspace’s Calendar plugin

Steps to replicate

  1. Add LinkIt field to calendar fields
  2. Load calendar view (i.e. month, week, or day)
  3. Result: Infinite recursion
Plugin Version
Solspace Calendar 2.0.6
LinkIt 1.1.3

Do you have any thoughts on this?

Link Settings

  • Pass LinkType settings to a link so available to use when building the link
  • Also make sure that the defaultText is passed and available to the link model

Assigning Value to Link Attributes from another Field

How do we assign a value from another field to the attributes? For example, I'm wanting to use the value from a field called linkStyle to set on the link's class name {{ block.linkStyle }}

      {% if block.type == 'link' %}

        {% set attributes = {
          class: 'white bg-black {{ block.linkStyle }}',
        } %}
        {{ block.linkit.link(attributes) }}

      {% endif %}

Add config file for setting default placeholder values

It's becoming pretty tiring having to change the default support@craftcms and http://craftcms.com placeholder values for every single new Linkit field I create.

Would love to have the ability to define these default values in a linkit.php config file or in a CP setting.

Make a LinkIt field available as a Table Column header?

Love the plugin, and glad to be able to contribute to development with the Plug-in Store.

I'd like to be able to add a LinkIt field as a Table Column header for my section entry lists in the control panel. Currently it doesn't seem that the LinkIt field appears at all as an option. Maybe I'm mistaken or I've done something wrong in setting up the field.

  1. It would be great to be able to choose it at all.
  2. It would be nice if the display were something like "No link" if no link, or "Link" hyperlinked to the resource followed by the resource type in parentheses, i.e. Link (URL) or Link (Entry) or Link (PDF) ... etc

This would be a nice addition if it isn't already there waiting for me to discover it. There are probably lots of complexities involved here so sorry if I'm asking for something ridiculously difficult.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.