statikbe/craft-cta-field

Call to action & link fields made easy

1.3.1 2019-08-21 13:59 UTC

This package is auto-updated.

Last update: 2020-03-21 15:07:12 UTC


README

Requirements

This plugin requires Craft CMS 3.0.0 or later.

Installation

To install the plugin, follow these instructions.

  1. Open your terminal and go to your Craft project:

     cd /path/to/project
    
  2. Then tell Composer to load the plugin:

     composer require statikbe/craft-cta-field
    
  3. In the Control Panel, go to Settings → Plugins and click the “Install” button for Link Field.

Templating

Link fields on your models will return an instance of cta\models\Link. Rendering a link field directly within a template will return the url the field is pointing to.

<a href="{{ item.myLinkField }}">Link</a>

You can use the following accessors to get the different properties of the link:

{{ item.myLinkField.getElement() }}
{{ item.myLinkField.getTarget() }}
{{ item.myLinkField.getText() }}
{{ item.myLinkField.getUrl() }}
{{ item.myLinkField.hasElement() }}
{{ item.myLinkField.isEmpty() }}

Use the getLink utility function to render a full html link:

{{ item.myLinkField.getLink() }}

You can pass the desired content of the link as a string, e.g.

{{ entry.linkField.getLink('Imprint') }}

You may also pass an array of attributes. When doing this you can override the default attributes href and target. The special attribute text will be used as the link content.

{{ entry.linkField.getLink({
  class: 'my-link-class',
  target: '_blank',
  text: 'Imprint',
}) }}

Custom classes

Add custum classes to the dropdown menu.

  1. Add cta.php to /config
  2. Declare your classes:
<?php

return [
    'classes' => [
        'btn'                   => 'Primary',
        'btn btn--secondary'    => 'Secondary'
    ]
];

Credits

Heavily inspired by sebastian-lenz/craft-linkfield.