Skip to content

Instantly share code, notes, and snippets.

@bhuvidya
Last active March 17, 2022 23:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bhuvidya/03ed19536cfa46051a8fc52cc65421b2 to your computer and use it in GitHub Desktop.
Save bhuvidya/03ed19536cfa46051a8fc52cc65421b2 to your computer and use it in GitHub Desktop.
Some notes for Craft CMS v3 plugin/module/twig reference.

Plugins / Modules

Modules

  • Get module instance: BlahModule::getInstance() or BlahModule::$instance
  • Get module service instance: BlahModule::getInstance()->myService or BlahModule::$instance->myService
  • Access module variable in twig: {{ craft.blahModule.someVar }} or {{ craft.blahModule.someVar(params) }}

Globals

Get globals set:

Craft::$app->globals
Craft::$app->globals->getSetByHandle('setHandle');

Sites

To get the Sites service:

Craft::$app->getSites()
--or--
Craft::$app->sites

Then use the API: https://docs.craftcms.com/api/v3/craft-services-sites.html#public-methods

Craft::$app->sites->currentSite
Craft::$app->sites->getSiteByHandle('bdvs');
Craft::$app->sites->getSiteById(3);

Paths

See https://docs.craftcms.com/api/v3/craft-services-path.html#public-methods

Craft::$app->path->getSiteTemplatesPath()

Twig

Currrent Site

{{ currentSite.id }}
{{ currentSite.handle }}
{{ currentSite.name }}
{{ currentSite.language }}
{{ currentSite.primary }}
{{ currentSite.baseUrl }}

Query Params

craft.app.request.getParam('someParam')

Debugging Queries

Use getRawSql() e.g. craft.entries({ section: 'artists' }).getRawSql()
See https://craftcms.stackexchange.com/questions/18491/dump-sql-query-in-twig-template

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment