Hot answers tagged

24

You can use the GlobalsService service available with craft()->globals like so: // Get the settings global set $settings = craft()->globals->getSetByHandle('settings'); // Access the address field var_dump($settings->address) Or you could use ElementCriteria as you suggested: // Get the GlobalSet element critera $criteria = craft()->...


10

In Craft 3 you can do this: use craft\helpers\UrlHelper; UrlHelper::siteUrl(); Craft::$app->sites->currentSite->name;


8

You want to use "a" as your DateInterval format parameter instead of "d". Also, you may need to use Twig's date function on the end date, which "converts an argument to a date to allow date comparison". This should give you the correct number of days between now and the future date: {{ date( inschrijven.datumEvenement|date ).diff( now ).format('%a') }} ...


7

Not sure why you're referencing "entry" in your code, I'd think the code should be: {% for block in footer.links %} {% if block.navigationLinks | length %} <h3>{{ block.navigationHeadline }}</h3> {# Show the navigationHeadline if it has links #} <ul> {% for row in block.navigationLinks %} <li>{{ row....


7

According to the Craft docs , the table field returns an array of rows. Assuming that is the case, you should be able to use the Twig slice filter: {% for row in registration.registrationCost | slice(0, 3) %} You can then use a simple conditional to output the row divider: {% if loop.index == 3 %} <hr> {% endif %} Stephen


7

It's currently not possible to change the order in the control panel, but we might look into that for a future release. They are currently set to display alphabetically.


7

The performance aspect is difficult to answer. On every pageload, Craft pulls all your global sets, which can be a performance hit, especially if you have many huge, complex global sets with a lot of fields. Any Single used in place of a global could be pulled on a need-to-use basis, i.e. not on every page, although the extra call to the EntriesService would ...


7

Edit: I needed to do a little digging. Internally, Craft can actually have multiple instances of Twig_Environment running (a Twig_Environment instance is what you get back by calling craft()->templates->getTwig()). Craft caches each instance in a private class variable, with the template loader classname and a hash of the environment's options making ...


6

You can get them via craft()->getSiteUrl() and craft()->getSiteName().


5

Yes, globals can be localised. Make sure you have ticked "This field is translatable" in the field settings for the ones in your global set, and you should be good to go.


5

Not directly. You could have a dropdown of all the global set handles (use Dynamic Fields Plugin if you don't want to maintain this dropdown manually), and then in your template fetch the global set from its handle with getSetByHandle: {% set globalSet = craft.globals.getSetByHandle(entry.globalSetHandle.value) %} {% if globalSet %} {# could conceivably be ...


5

seo.twittercard is an ElementCriteriaModel. To access the actual asset, you need to use seo.twittercard.first (or equivalently: seo.twittercard.first()). However, you should always check that this exists before referencing it: {% if seo.twittercard|length %} <meta(name='twitter:image:', content='{{ seo.twittercard.first.getUrl() }}')> {% endif %} ...


5

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 you to use craft.globals to prevent issues with the routing custom variables.


4

For what it's worth, you can strip out all non-numerics with this something like this: {{ globalPhoneNumber.phoneNumber|replace('/[^0-9]/', '') }} Or, as part of a tel: anchor: <a href="tel:{{ globalPhoneNumber.phoneNumber|replace('/[^0-9]/', '') }}">{{ globalPhoneNumber.phoneNumber }}</a>


4

I haven't tried this but I think that you can use the twig replace function. Something like: {{ global.phone|replace({"(":"", ")":"", "-":"", " ":""}) }} Update In your case, since the prefix (1-) is required, and "-" are allowable, the replace string for "(555) 555-5555" would be: {{ global.phone|replace({"(" : "1-", ") " : "-"}) }}


4

You call globals with the globals' handle, and the field name of a field in it. So, if you have a text input field named "myField" inside a global with handle "myGlobal", you can call it anywhere in your templates with: {{ myGlobal.myField }}


4

Per my comment on Jamie's answer above, I didn't notice that on the Globals section in the control panel, the locale switcher is up where the version switcher usually is on entries: One of those UI-blindness moments.


4

Looks to me like a matrix field with 1 block (Navigation) and two fields : Plain Text - Headline Table - 2 columns with headings Label (Single-line text) and URL (Single-line text) It's worth noting that the table field doesn't have a relations column, so they're probably inserting these URLs manually.


4

One solution is to retrieve the entry ids for the season entries and all entries separately; subtract the 'season entry ids' from 'all entry ids' (using craft's without twig filter); then merge the two arrays back together (using twig's merge filter) in the proper order. You can then use this new id array to generate an elementCriteriaModel which can be used ...


4

If companyInfo if your global and logo if your Asset field, then you'd use it just like you would anywhere else keeping in mind that all relation fields (Assets, Users, Entries, Tags, Categories, etc.) always return a collection of results. So, companyInfo.logo.first() will return the first asset and if you want to loop through them all: {% for image in ...


4

No, the isEditable property isn't supported for Global Sets. The confusing thing is that the GlobalSetModel class does actually have an isEditable property, however it will always return false. The reason is that this property is inherited from the BaseElementModel class, where it will return false unless the element type overrides it to (potentially) ...


4

isEditable() would be a method of the content model, not of a field. Unfortunately, this is currently only useful for EntryModel objects; It is not presently implemented for GlobalSetModel objects, so using .isEditable on a global set will always return false. However, you could use the .can() method on a user, and check for the permission 'editglobalset:X' ...


4

Since Global Sets are elements just like entries, users, etc., you can create API endpoints that are all about them: 'globals/<handle:\w+>' => function($handle) { return [ 'elementType' => 'GlobalSet', 'criteria' => ['handle' => $handle], 'first' => true, 'transformer' => function(\Craft\...


4

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 access your global set and use it as you usually would by setting it to a variable. You can do so like this: {% set companyInfo = craft.globals.getSetByHandle(...


4

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 differently for modules/plugins. As per the Sites section in the Craft 3.5.x Class Reference docs at https://docs.craftcms.com/api/v3/craft-services-sites.html, you would ...


3

Image fields are an array, because you can have multiple images within that field. Usually you would loop through the array to output your images: {% if Logo.logo | length %} {% for image in Logo.logo %} {{ image.url }} {% endfor %} {% endif %} Or, if you only have a single image in the array, you can just do this: {% set image = Logo.logo....


3

That's the beauty of Globals... they can be used anywhere! Common usage examples include (but not limited to): Content in your header Content in your footer Content in your sidebar Anything else that appears frequently in your website It's not a strict rule, but Globals tend to work great in situations where you only need a small amount of content to be ...


3

You can, using reference tags, but unfortunately they only work in plain text fields: Reference tags in rich text fields parse and converted on save


3

If your putting the variable in the craft settings input, try wrapping it in single brackets like {siteUrl} instead of {{ siteUrl }} That should do the trick


3

Try using setContentFromPost, as mentioned in this answer from Brandon. $content = array( 'fieldHandle' => 'value', ); $globalSet->setContentFromPost($content); craft()->globals->saveContent($globalSet);


Only top voted, non community-wiki answers of a minimum length are eligible