Repository URL to install this package:
|
Version:
2.5.1 ▾
|
#Global Content
Global Content is meant to give developers an easy and quick way to add small pieces of content to a site that an administrator can then freely change as they see fit. Of course there are many options for content already like nodes and other entities to fill out a page, but these are usually overkill when just talking about a text in a custom form or similar.
This is what Global Content is for. Very small pieces of content in places where it's usually quite bothersome to embed any other type of content entity.
For most usecases using a Global Content is as easy as calling the gc() function whenever you need it.
$build = gc('example-machine-name');
This returns a render array with all the necessary cacheability information and context menus for the Global Content in question. There are also other variants for more specialised uses like gci() which works like gc() except without a context menu in case the extra markup is undesired, but is still a render array. Last we have gcs() which just delivers a rendered string, but cacheability is lost and must be taken care of manually.
There are also Twig functions with the same names...
<p>{{ gc('example-machine-name') }}</p>
When defining new Global Content you need to create a MODULE.globalcontent.yml file or THEME.globalcontent.yml which will include definitions for all the global content needed for that module or theme.
example-machine-name: type: textfield title: Example title description: A Global Content used as an example for documentation. category: Example Category group: Example group subgroup: Example group default: en: 'English test title' da: 'Dansk test titel' options: translatable: TRUE token_types: - node
Every definition starts with a unique machine name for the Global Content followed by the following properties:
Provides a simple plain text field for entering short texts containing no markup.
Same as textfield, but for longer texts and provided with multiple lines for editing.
Same as textfield, but with validation for e-mail addresses.
Gives a field for entering a number integer with validation and support for mobile number input.
The field gives access to a Rich Text editor enabling the administrator to use markup for formatting. The default text format and allowed formats can be configured for all text_format fields on the Global Content settings page.
This field gives a date picker for selecting a date to be displayed in a selected Drupal date format. The default date format can be configured for all date fields on the Global Content settings page.
This field gives a date picker for selecting a date and time to be displayed in a selected Drupal date format. The default date format can be configured for all datetime fields on the Global Content settings page.
The field allows the administrator to specify an url, either internal, external or both. If internal urls are allowed it will give suggestions for nodes, but any relative url is allowed.
The field allows the administrator to specify an url, either internal, external or both and a title which together creates a link. If internal urls are allowed it will give suggestions for nodes, but any relative url is allowed.
The field allows the administrator to specify an url, either internal, external or both and a title which together creates a button. If internal urls are allowed it will give suggestions for nodes, but any relative url is allowed. The field type default to a link with the button class when rendered, but can be configured to use any other class as appropiate to the theme on the site.
This field can allow the administrator to select a single or multiple entities of a certain type using an autocomplete field.
This field can allow the administrator to select a single or multiple media entities using an autocomplete field. Sadly Drupal Core still does not support using Media Library outside of fields yet.
This field can allow the administrator to select a single icon using the icon library.
Should the need for more advanced usage of Global Content be required then it's recommended to use the GlobalContentViewBuilder service. Using this service you get access to ViewableGlobalContent objects via the get() method and these are much more adjustable than simply using gc().
ViewableGlobalContent objects allow you to set the following properties before rendering the Global Content:
It also gives you access to lots of information useful rendering or other usage:
And lastly you have several options for rendering or other output: