Craft Themes (4.x)

ThemePlugin extends Plugin
in package
implements ThemeInterface Uses ArrayableTrait

Base class for all themes

Interfaces, Classes and Traits

ThemeInterface
A theme is a regular plugin, it defines regions and can extend another theme.

Table of Contents

EVENT_DEFINE_REGION_LAYOUTS  = 'define_region_layout'
EVENT_DEFINE_SCSS_COMPILER  = 'define_scss_compiler'
$_compiler  : Compiler
$_preferences  : ThemePreferencesInterface
$_regionLayouts  : array<string|int, mixed>
$assetBundles  : array<string|int, mixed>
Bundle assets defined by this theme, keyed by the url path. '*' for all paths, '' for homepage. Wrap key with // for regular expression : [ '*' => [ CommonAssets::class ], '' => [ HomeAssets::class ], 'blog' => [ BlogAsset::class ] ]
$inheritsAssetBundles  : bool
Should the parent asset bundle be registered as well
$inheritsAssets  : bool
Should this theme inherits parent's assets
$templatesPaths  : array<string|int, string>
array of all the template paths (including those of the parents)
afterSet()  : mixed
Callback after the theme has been set for a request
afterThemeInstall()  : mixed
Callback after the plugin is installed, this is only called for pro version.
afterThemeUninstall()  : mixed
Callback after the plugin is uninstalled.
fields()  : array<string|int, mixed>
getAssetUrl()  : string
Get an url for an theme asset.
getDefaultRegionLayout()  : RegionLayoutInterface
Get the default region layout
getDefaultRegionLayoutHandle()  : string|null
Get the default region layout handle. Returning null will take the handle from the parent theme.
getExtends()  : string|null
Which other theme does this extends from.
getHasPreview()  : bool
Does this theme have a preview image
getParent()  : ThemeInterface|null
Get theme parent
getPreferences()  : ThemePreferencesInterface
Get theme preferences
getPreviewImage()  : string
Preview image for the theme, returns a web url
getRegionLayout()  : array<string|int, mixed>
Get a region layout by handle. Will return the default region layout if $handle is null
getRegionLayouts()  : array<string|int, mixed>
Get theme's region layouts definitions
getRegionsTemplate()  : string
Get the base template for rendering regions
getScssCompiler()  : Compiler
Get the scss compiler.
getTemplatePaths()  : array<string|int, string>
Absolute template paths, including those of the parent(s)
getTemplatesFolder()  : string
Folder where the templates are stored for this theme
isPartial()  : bool
Is this theme partial.
registerAssetBundles()  : mixed
Register this theme's assets in view for a specific path
defineRegionLayouts()  : array<string|int, mixed>|null
Define region layouts as array
getAssetBundles()  : array<string|int, string>
Get bundle assets for a url path
getPreferencesModel()  : ThemePreferencesInterface
Get theme preferences model
getPreviewImagePath()  : string|null
Get the path of the image preview file
getScssCompilerOptions()  : array<string|int, mixed>
Get scss compiler default options
getScssCompilerPlugins()  : array<string|int, mixed>
Get the plugins for the scss compiler

Constants

EVENT_DEFINE_REGION_LAYOUTS

public mixed EVENT_DEFINE_REGION_LAYOUTS = 'define_region_layout'
Tags
since
5.0.0

EVENT_DEFINE_SCSS_COMPILER

public mixed EVENT_DEFINE_SCSS_COMPILER = 'define_scss_compiler'

Properties

$_regionLayouts

protected array<string|int, mixed> $_regionLayouts
Tags
since
5.0.0

$assetBundles

Bundle assets defined by this theme, keyed by the url path. '*' for all paths, '' for homepage. Wrap key with // for regular expression : [ '*' => [ CommonAssets::class ], '' => [ HomeAssets::class ], 'blog' => [ BlogAsset::class ] ]

protected array<string|int, mixed> $assetBundles = []

$inheritsAssetBundles

Should the parent asset bundle be registered as well

protected bool $inheritsAssetBundles = true

$inheritsAssets

Should this theme inherits parent's assets

protected bool $inheritsAssets = true

$templatesPaths

array of all the template paths (including those of the parents)

protected array<string|int, string> $templatesPaths

Methods

afterSet()

Callback after the theme has been set for a request

public afterSet() : mixed
Tags
inheritDoc
Return values
mixed

afterThemeInstall()

Callback after the plugin is installed, this is only called for pro version.

public afterThemeInstall() : mixed
Tags
inheritDoc
Return values
mixed

afterThemeUninstall()

Callback after the plugin is uninstalled.

public afterThemeUninstall() : mixed
Tags
inheritDoc
Return values
mixed

fields()

public fields() : array<string|int, mixed>
Tags
inheritDoc
Return values
array<string|int, mixed>

getAssetUrl()

Get an url for an theme asset.

public getAssetUrl(string $path) : string
Parameters
$path : string
Tags
inheritDoc
Return values
string

getDefaultRegionLayoutHandle()

Get the default region layout handle. Returning null will take the handle from the parent theme.

public getDefaultRegionLayoutHandle() : string|null
Tags
inheritDoc
Return values
string|null

getExtends()

Which other theme does this extends from.

public getExtends() : string|null
Tags
inheritDoc
Return values
string|null

getHasPreview()

Does this theme have a preview image

public getHasPreview() : bool
Tags
inheritDoc
Return values
bool

getPreviewImage()

Preview image for the theme, returns a web url

public getPreviewImage() : string
Tags
inheritDoc
Return values
string

getRegionLayout()

Get a region layout by handle. Will return the default region layout if $handle is null

public getRegionLayout([string|null $handle = null ]) : array<string|int, mixed>
Parameters
$handle : string|null = null
Tags
inheritDoc
Return values
array<string|int, mixed>

getRegionLayouts()

Get theme's region layouts definitions

public getRegionLayouts() : array<string|int, mixed>
Tags
inheritDoc
Return values
array<string|int, mixed>

getScssCompiler()

Get the scss compiler.

public getScssCompiler([array<string|int, mixed> $options = [] ]) : Compiler
Parameters
$options : array<string|int, mixed> = []

Override compiler options here. Import paths will be prepended, aliases will be overridden

Tags
inheritDoc
Return values
Compiler

getTemplatePaths()

Absolute template paths, including those of the parent(s)

public getTemplatePaths() : array<string|int, string>
Tags
inheritDoc
Return values
array<string|int, string>

getTemplatesFolder()

Folder where the templates are stored for this theme

public getTemplatesFolder() : string
Tags
inheritDoc
Return values
string

isPartial()

Is this theme partial.

public isPartial() : bool
Tags
inheritDoc
Return values
bool

registerAssetBundles()

Register this theme's assets in view for a specific path

public registerAssetBundles(string $urlPath) : mixed
Parameters
$urlPath : string
Tags
inheritDoc
Return values
mixed

defineRegionLayouts()

Define region layouts as array

protected abstract defineRegionLayouts() : array<string|int, mixed>|null

'default' => [ 'label' => 'Default', 'regions' => [ 'handle' => 'region-handle', 'name' => 'Region', 'width' => '100%' //Used in backend blocks section ] ] ]

Returning null will load parent's theme regions

Return values
array<string|int, mixed>|null

getAssetBundles()

Get bundle assets for a url path

protected getAssetBundles(string $urlPath) : array<string|int, string>
Parameters
$urlPath : string
Return values
array<string|int, string>

getPreviewImagePath()

Get the path of the image preview file

protected getPreviewImagePath() : string|null
Return values
string|null

getScssCompilerOptions()

Get scss compiler default options

protected getScssCompilerOptions() : array<string|int, mixed>
Return values
array<string|int, mixed>

getScssCompilerPlugins()

Get the plugins for the scss compiler

protected getScssCompilerPlugins() : array<string|int, mixed>
Return values
array<string|int, mixed>

Search results