14
votes
How to retrieve Site Url and Site Name in Plugin
In Craft 3 you can do this:
use craft\helpers\UrlHelper;
UrlHelper::siteUrl();
Craft::$app->sites->currentSite->name;
5
votes
Accepted
Use global values in the Entry URL Format field
Your CraftCMS global can be accessed using the code
{{craft.globals.getSetByHandle('siteDefaults').artworksList.first.uri|default}}/{slug}
I believe the issue is that the Entry URL field requires ...
5
votes
Accepted
Is there a way to eager-load fields in a global set?
I know you can use this method to eager load fields on injected entry/category variables, so I'm assuming it would work for global sets as well:
{% do craft.app.elements.eagerLoadElements(
...
4
votes
Accepted
Order PDF in back-end error with Globals
Since the templates are rendered on the server and not in a regular http request (in the front end templates), the globals are not loaded automatically, into the global twig namespace.
You can still ...
4
votes
Accepted
How do I get the siteName in a module (Craft 3)
When searching the Craft 3.5.x docs for siteName, I see references to Global Variables and General Config Settings. Screenshot: https://cln.sh/egvIXhxZSzaFjeCgNLxX
However, AFAICT, it works ...
4
votes
Accepted
Get global variable value PHP
From your example, the Global handle is globalWebsite and the field handle is freeTrialPeriodDays.
So this should work in PHP:
$trialDays = \Craft::$app->getGlobals()->getSetByHandle('...
3
votes
Accepted
Get globals from other sites
The function is
public function getSetByHandle(string $globalSetHandle, int $siteId = null)
Thus you can grab all the siteIds you want and loop through them
{% for siteId in sites %}
{% set ...
3
votes
Accepted
Trying to set an image URL using a global asset field
I don't know if it's the cause of the error you're receiving or not, but this is incorrect Twig syntax:
{% set image = '{{ websitePreferences.placeholderImage.first.url }}' %}
You'll never need to ...
3
votes
Accepted
Use Globals in email templates
They aren't loaded into the twig template global scope but you can access them with:
https://craftcms.com/docs/templating/craft.globals#getSetByHandle
3
votes
Global Image Variables?
This is a general structure to display global images in any template:
<img src="{{ globalHandle.globalAssetHandle.one().getUrl }}" alt="">
You have to set your global handle ...
3
votes
Is There a Way to Add Tabs in Global Fields?
As far as my knowledge goes there is not a way to do this without some though changes.
Wouldn't it just be better to add another global and just name it settings?
Something like:
globalSettings(...
3
votes
Accepted
Use Craft globals for CSS styles
It’s possible to do this by making the entire CSS file a Twig template.
Create a new file in your templates folder, such as templates/css/styles.css, with all the dynamic styles you want.
To prevent ...
2
votes
Accepted
Is it Possible to Use Live Preview with Global Settings
Anything's possible, but not without hacking core files, which isn't recommended.
By default, global settings don't even have the concept of a "template", which is a fundamental part of getting a ...
2
votes
Accepted
Create an Array in Twig and loop through fields in global set
Seems like a great use case for a Twig macro. Completely untested, but something like this:
{% macro social(object, type) %}
{% set value = attribute(object, type) %}
{% if value | length %...
2
votes
Access global field content
I do not see anything obviously wrong with your code, but something that has caught me out many times is a misspelling of field names, so be sure your twig code matches your field names exactly. One ...
2
votes
Accepted
Saving fields to a GlobalSet
Something like this should work:
// Generate name and handle.
$globalSetName = "My Global Set";
$globalSetHandle = lcfirst(str_replace(" ","", ucwords($globalSetName)));
// Create a global set
$...
2
votes
Accepted
Defining TwigExtension globals in Craft 3
There are two ways to achieve this:
Option 1
Add this in your plugin/module main init function:
$twig = Craft::$app->view->getTwig(null, ['safe_mode' => false]);
$twig->...
2
votes
How to edit a global footer link
Look under Globals in the Control Panel, which is the likely place for such links. If you do not see it, either you do not have permissions OR there are no Globals. Assuming you do not see them, Robin'...
2
votes
Getting image url from globals asset field
Thanks to Andrew Fairlie for pointing out I need to make sure Assets in this volume have public URLs set
2
votes
Accepted
Comparing date field object with global variable "now" gives unexpected result
now represents the current DateTime so unless you specify exactly the correct time in your field the condition deliveryDate == now will never become true. If the user visits the page one second ...
2
votes
Accepted
Updating global set from controller
Oli’s comment is mostly right, but I think the solution is event simpler—the method you discovered is specifically for saving the GlobalSet settings/definition (like its name, handle, and the fields ...
2
votes
What is appropriate for a Global field in terms of site speed?
I agree with Jamie's answer re: how to build out a site's IA.
As for performance, each additional global set adds queries to every page that is loaded. It's pretty unlikely that there will be a huge ...
2
votes
Accepted
What is appropriate for a Global field in terms of site speed?
I can't speak on the speed impact of having lots of content in the Globals, but the use cases you have described don't really seem suitable for Globals.
I would instead recommend creating Sections ...
2
votes
Accepted
Show a list of pages but hide the current page from the list
To me it looks like the entries in the section with the handle pages have their own URLs. This means that if you visit a URL that belongs to an entry in that section Craft will automatically make that ...
2
votes
Is there a way to eager-load fields in a global set?
Do you mean lazy load or eager load?
Usually you lazy load images via JS to speed up site loading but not I'm not sure doing it to one image across multiple pages is all that beneficial?
After the ...
2
votes
Globals disappeared/Queue Failed — Propagating global sets
You're probably experiencing this issue which can result in global sets not being created in some environments. This issue has already been fixed, the fix will be release with the next Craft version (...
2
votes
Accepted
Globals disappeared/Queue Failed — Propagating global sets
I think the failed propagation job is the problem here.
You can re-run this by creating an executable to push a new propagation job.
Duplicate your craft executable and rename it to propagate (or ...
2
votes
Accepted
Querying Globals from a Custom Field Type gives 502 Bad Gateaway
The answer below is mostly taken from Brandon Kelly's answer on Github which I suggest to read for more detailed information: https://github.com/craftcms/cms/issues/12847
The reason for the endless ...
2
votes
Accepted
Looking to simplify this if else. Any tips to make this simpler?
There are a lot of superfluous variables there. You don't need to keep track of whether there are restrictions, all you need to know is whether the current page and station are allowed to show the ...
1
vote
Best practise to use translate in admin panel
You could try if the Translate plugin for Craft CMS could help you out. You can use this plugin as a base for you own and save the values in a custom table and use the registerTranslateSources hook to ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
globals × 91templating × 15
plugin-development × 13
craft3 × 9
matrix × 9
localization × 7
variable × 6
multi-site × 5
assets × 4
field × 4
plugins × 3
datetime × 3
control-panel × 2
search × 2
plugin-elementapi × 2
error-message × 2
image × 2
emails × 2
fieldtypes × 2
eager-loading × 2
table-field × 2
content × 2
reference-tags × 2
entries × 1
plugin-craftcommerce × 1