Link Plugin¶
djangocms-cascade ships with its own link plugin. This is because other plugins from the Cascade eco-system, such as the BootstrapButtonPlugin, the BootstrapImagePlugin or the BootstrapPicturePlugin also require the functionality to set links to internal- and external URLs. Since we do not want to duplicate the linking functionality for each of these plugins, it has been moved into its own base class. Therefore we will use the terminology TextLinkPlugin when referring to text-based links.
The de-facto plugin for links, djangocms-link can’t be used as a base class for these plugins,
hence an alternative implementation has been created within the Cascade framework. The link related
data is stored in a sub-dictionary named link
in our main JSON field.
Prerequisites¶
Before using this plugin, assure that 'cmsplugin_cascade.link'
is member of the list or
tuple CMSPLUGIN_CASCADE_PLUGINS
in the project’s settings.py
.
The behavior of this Plugin is what you expect from a Link editor. The field Link Content is the
text displayed between the opening and closing <a>
tag. If used in combination with
djangocms-text-ckeditor the field automatically is filled out.
By changing the Link type, the user can choose between three types of Links:
- Internal Links pointing to another page inside the CMS.
- External Links pointing to a valid Internet URL.
- Links pointing to a valid e-mail address.
The optional field Title can be used to add a title="some value"
attribute to the
<a href ...>
element.
With Link Target, the user can specify, whether the linked content shall open in the current window or if the browser shall open a new window.
Link Plugin with sharable fields¶
If your web-site contains many links pointing onto external URLs, you might want to refer to them by a symbolic name, rather than having to reenter the URL repeatedly. With djangocms-cascade this can be achieved easily by declaring some of the plugin’s fields as “sharable”.
Assure that INSTALLED_APPS
contain 'cmsplugin_cascade.sharable'
, then redefine the
TextLinkPlugin to have sharable fields in settings.py
:
CMSPLUGIN_CASCADE = {
...
'plugins_with_sharables':
...
'TextLinkPlugin': ('link',), # and optionally other fields
...
},
...
}
This will change the Link Plugin’s editor slightly. Note the extra field added to the bottom of the form.
Now the URL for this ink entity is stored in a central entity. This feature is useful, if for instance the URL of an external web page may change in the future. Then the administrator can change that link in the administration area once, rather than having to go through all the pages and check if that link was used.
To retain the Link settings, click onto the checkbox Remember these settings as: ... and give it a name of your choice. The next time your create a Shared Link element, you may select a previously named settings from the select field Shared Settings. Since these settings can be shared among other plugins, these input fields are disabled and can’t be changed anymore.
Extending the Link Plugin¶
While programming third party modules for Django, one might have to access a model instance through a URL and thus add the method get_absolute_url to that Django model. Since such a URL is neither a CMS page, nor a URL to an external web page, it would be convenient to access that model using a special Link type.
For example, in django-shop we can allow to link directly from a CMS page to a shop’s product. This is achieved by reconfiguring the Link Plugin inside Cascade with:
CMSPLUGIN_CASCADE = {
...
'dependencies': {
'shop/js/admin/shoplinkplugin.js': 'cascade/js/admin/linkplugin.js',
},
'link_plugin_classes': (
'shop.cascade.plugin_base.CatalogLinkPluginBase',
'cmsplugin_cascade.link.plugin_base.LinkElementMixin',
'shop.cascade.plugin_base.CatalogLinkForm',
),
}
The tuple specified through link_plugin_classes
replaces the base class for the LinkPlugi class
and the form class for its editor. Please refer to the django-shop for implementation details of
the classes.
Now the select box for Link type will offer one additional option: “Product”. When this is selected, the site administrator can choose between all of the shops products.