Skip to content

Instantly share code, notes, and snippets.

@a-am
Last active January 23, 2018 01:32
Show Gist options
  • Save a-am/1de5e37ef686fc66f1072776f204fd5e to your computer and use it in GitHub Desktop.
Save a-am/1de5e37ef686fc66f1072776f204fd5e to your computer and use it in GitHub Desktop.
Collection of syntax changes for CraftCMS 3

REQUEST

Craft::$app->getRequest()
Craft::$app->getRequest()->getAcceptsJson()
$this->asJson();

CURRENT USER

Craft::$app->getUser()->getIdentity()

GET SESSION ERROR|NOTICE

Craft::$app->getSession()->setError(Craft::t('app', 'Couldn’t save entry.'))
Craft::$app->getSession()->setNotice(Craft::t('app', 'Entry saved.'))

SET ROUTE PARAMS

Craft::$app->getUrlManager()->setRouteParams([
      'entry' => $entry
]);

USER SESSION

Craft::$app->getUser()

GET ELEMENTS

$event = VentiEvent::find()
    ->eid($variables['eid'])
    ->localeEndabled(null)
    ->one();

TIMEZONE

Craft::$app->getTimeZone()

PLUGINS

Craft::$app->getPlugins()->getPlugin()

DATE FORMATS

DateTimeHelper::toIso8601()
// \craftcms\src\i18n\locale
Craft::$app->locale->getDateFormat('short',Locale::FORMAT_PHP)
Craft::$app->locale->getTimeFormat('medium')
Craft::$app->locale->getDateTimeFormat('long')

SITES

Craft::$app->getSites()->getAllSiteIds()
Craft::$app->getSites()->currentSite->id
Craft::$app->getSites()->getAllSites()
Craft::$app->getSites()->getSiteById()
Craft::$app->getSites()->getSiteByHandle()
Craft::$app->getSites()->getEditableSiteIds()
Craft::$app->getSites()->getPrimarySite()->id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment