Skip to content

Instantly share code, notes, and snippets.

@mooror
Created November 3, 2019 21:09
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 mooror/84235f076b4329b9008e1f00c07771c0 to your computer and use it in GitHub Desktop.
Save mooror/84235f076b4329b9008e1f00c07771c0 to your computer and use it in GitHub Desktop.
Common Silverstripe 4 Snippets for VSCode (File->Preferences->User Snippets. Then search for php.json)
{
// Silverstripe Bootstraps
"SS Bootstrap Page Class": {
"prefix": [
"page",
"boot-page"
],
"body": [
"use SilverStripe\\CMS\\Model\\SiteTree;",
"/**",
" * ${3:Description}",
" *",
" * @category Page",
" * @package ${4:Silverstripe}",
" ${5:* @subpackage ${6:module-name}}",
" */",
"class $1 extends ${2:Page}",
"{",
"\t${0}",
"}"
],
"description": "Bootstrap a Page class"
},
"SS Bootstrap Page Controller Class": {
"prefix": [
"pagecontroller",
"boot-page-controller"
],
"body": [
"use SilverStripe\\CMS\\Controllers\\ContentController;",
"/**",
" * ${3:Description}",
" *",
" * @category Page",
" * @package ${4:Silverstripe}",
" ${5:* @subpackage ${6:module-name}}",
" */",
"class ${1}Controller extends ${2:Page}Controller",
"{",
"\tpublic function doInit()",
"\t{",
"\t\tparent::doInit();",
"\t}${0}",
"}"
],
"description": "Bootstrap a Page controller class"
},
"SS Bootstrap DataObject Class": {
"prefix": [
"dataobject",
"boot-data-object"
],
"body": [
"use SilverStripe\\\\ORM\\\\DataObject;",
"",
"/**",
" * ${3:Description}",
" *",
" * @category Model",
" * @package ${4:Silverstripe}",
" ${5:* @subpackage ${6:module-name}}",
" */",
"class ${1} extends ${2:DataObject}",
"{",
"\t${0}",
"}"
],
"description": "Bootstrap a DataObject class"
},
"SS Bootstrap Model Admin Class": {
"prefix": [
"modeladmin",
"boot-model-admin"
],
"body": [
"use SilverStripe\\\\Admin\\\\ModelAdmin;",
"",
"/**",
"* ${5:Description}",
"*",
"* @category Model",
"* @package ${6:Silverstripe}",
"${7:* @subpackage ${8:module-name}}",
"*/",
"class ${1}Admin extends ModelAdmin",
"{",
"\t/**",
"\t * Managed data objects for CMS",
"\t * @var array",
"\t */",
"\tprivate static \\$managed_models = [",
"\t\t'${2}'",
"\t];",
"",
"\t/**",
"\t * URL Path for CMS",
"\t * @var string",
"\t */",
"\tprivate static \\$url_segment = '${3}';",
"",
"\t/**",
"\t * Menu title for Left and Main CMS",
"\t * @var string",
"\t */",
"\tprivate static \\$menu_title = '${4}';",
"",
"\t${0}",
"}"
],
"description": "Bootstrap a ModelAdmin class"
},
// Silverstripe properties
"SS menu_title": {
"prefix": "menu_title",
"body": [
"/**",
"* Menu title for Left and Main CMS",
"* @var string",
"*/",
"private static \\$menu_title = '${1}';${0}"
],
"description": "Silverstripe $menu_title property"
},
"SS menu_icon": {
"prefix": "menu_icon",
"body": [
"/**",
"* Menu icon for Left and Main CMS",
"* @var string",
"*/",
"private static \\$menu_icon = '${1}';${0}"
],
"description": "Silverstripe $menu_icon property"
},
"SS singular_name": {
"prefix": "singular_name",
"body": [
"/**",
"* Singular name for CMS",
"* @var string",
"*/",
"private static \\$singular_name = '${1}';${0}"
],
"description": "Silverstripe $singular_name property"
},
"SS table_name": {
"prefix": "table_name",
"body": [
"/**",
"* Defines the database table name",
"* @var string",
"*/",
"private static \\$table_name = '${1}';${0}"
],
"description": "Silverstripe $table_name property"
},
"SS db": {
"prefix": "db",
"body": [
"/**",
"* Database fields",
"* @var array",
"*/",
"private static \\$db = [",
"\t'${1}' => '${2}',",
"];${0}"
],
"description": "Silverstripe $db property"
},
"SS has_one": {
"prefix": "has_one",
"body": [
"/**",
"* Has_one relationship",
"* @var array",
"*/",
"private static \\$has_one = [",
"\t'${1}' => '${2}',",
"];${0}"
],
"description": "Silverstripe $has_one relationship property"
},
"SS has_many": {
"prefix": "has_many",
"body": [
"/**",
"* Has_many relationship",
"* @var array",
"*/",
"private static \\$has_many = [",
"\t'${1}' => '${2}',",
"];${0}"
],
"description": "Silverstripe $has_many relationship property"
},
"SS many_many": {
"prefix": "many_many",
"body": [
"/**",
"* Many_many relationship",
"* @var array",
"*/",
"private static \\$many_many = [",
"\t'${1}' => '${2}',",
"];${0}"
],
"description": "Silverstripe $many_many relationship property"
},
"SS many_many_extraFields": {
"prefix": "many_many_extraFields",
"body": [
"/**",
"* Defines Database fields for the Many_many bridging table",
"* @var array",
"*/",
"private static \\$many_many_extraFields = [",
"\t'${1}' => [",
"\t\t'${2:Sort}' => '${3:Int}'",
"\t]",
"];${0}"
],
"description": "Silverstripe $many_many_extraFields relationship property"
},
"SS belongs_to": {
"prefix": "belongs_to",
"body": [
"/**",
"* Belongs_to relationship",
"* @var array",
"*/",
"private static \\$belongs_to = [",
"\t'${1}' => '${2}',",
"];",
"${0}"
],
"description": "Silverstripe $belongs_to relationship property"
},
"SS belongs_many_many": {
"prefix": "belongs_many_many",
"body": [
"/**",
"* Belongs_many_many relationship",
"* @var array",
"*/",
"private static \\$belongs_many_many = [",
"\t'${1}' => '${2}',",
"];${0}"
],
"description": "Silverstripe $belongs_many_many relationship property"
},
"SS owns": {
"prefix": "owns",
"body": [
"/**",
"* Relationship version ownership",
"* @var array",
"*/",
"private static \\$owns = [",
"\t'${1}'",
"];"
],
"description": "Silverstripe $owns property"
},
"SS casting": {
"prefix": "casting",
"body": [
"/**",
"* Ensures that the methods are wrapped in the correct type and",
"* values are safely escaped while rendering in the template.",
"* @var array",
"*/",
"private static \\\\$casting = [",
"\\t'${1}' => '${2}'",
"];"
],
"description": "Silverstripe $casting property"
},
"SS defaults": {
"prefix": "defaults",
"body": [
"/**",
"* Add default values to database",
"* @var array",
"*/",
"private static \\$defaults = [",
"\t'${1}' => '${2}'",
"];"
],
"description": "Silverstripe $defaults property"
},
"SS allowed_actions": {
"prefix": "allowed_actions",
"body": [
"/**",
"* Defines methods that can be called directly",
"* @var array",
"*/",
"private static \\$allowed_actions = [",
"\t'${1}' => ${2:true}",
"];"
],
"description": "Silverstripe $allowed_actions property"
},
"SS summary_fields": {
"prefix": "summary_fields",
"body": [
"/**",
"* Defines summary fields commonly used in table columns",
"* as a quick overview of the data for this dataobject",
"* @var array",
"*/",
"private static \\$summary_fields = [",
"\t'${1}' => '${2}'",
"];"
],
"description": "Silverstripe $summary_fields property"
},
"SS searchable_fields": {
"prefix": "searchable_fields",
"body": [
"/**",
"* Defines a default list of filters for the search context",
"* @var array",
"*/",
"private static \\$searchable_fields = [",
"\t'${1}' => '${2}'",
"];"
],
"description": "Silverstripe $searchable_fields property"
},
// Silverstripe functions
"SS main translator function": {
"prefix": [
"_t",
"lexicon",
"translate"
],
"body": [
"_t(__CLASS__ . '.${1/([a-zA-Z]*)([^a-zA-Z]*)/\\U$1/}', '${2:${1}}')${0}"
],
"description": "Silverstripe translator function (_t)"
},
// Silverstripe methods
"SS onAfterWrite": {
"prefix": "onAfterWrite",
"body": [
"/**",
"* Event handler called after writing to the database.",
"*/",
"public function onAfterWrite()",
"{",
"\tparent::onAfterWrite();",
"\t${0}",
"}"
],
"description": "Silverstripe onAfterWrite() method"
},
"SS getCMSFields": {
"prefix": "getCMSFields",
"body": [
"/**",
"* CMS Fields",
"* @return FieldList",
"*/",
"public function getCMSFields()",
"{",
"\t\\$fields = parent::getCMSFields();",
"\t\\$fields->${0}",
"\treturn \\$fields;",
"}"
],
"description": "Silverstripe getCMSFields() method"
},
"SS getCMSFieldsScafoldless": {
"prefix": "getCMSFieldsScafoldless",
"body": [
"/**",
"* CMS Fields",
"* @return FieldList",
"*/",
"public function getCMSFields()",
"{",
"\t\\$fields = FieldList::create();",
"\t\\$fields->push(",
"\t\tTabSet::create(",
"\t\t\t'Root',",
"\t\t\tTab::create('Main')",
"\t\t)",
"\t);",
"\t\\$fields->${0}",
"\t\\$this->extend('updateCMSFields', \\$fields);",
"\treturn \\$fields;",
"}"
],
"description": "Silverstripe getCMSFields() method without parent scaffolding"
},
"SS updateCMSFields": {
"prefix": "updateCMSFields",
"body": [
"/**",
"* Update Fields",
"* @return FieldList",
"*/",
"public function updateCMSFields(FieldList \\$fields)",
"{",
"\t\\$owner = $this->owner;",
"\t\\$fields->${0}",
"\treturn \\$fields;",
"}"
],
"description": "Silverstripe updateCMSFields() method"
},
"SS getEditForm": {
"prefix": "getEditForm",
"body": [
"/**",
"* @param Int $id",
"* @param FieldList $fields",
"* @return Form",
"*/",
"public function getEditForm($id = null, $fields = null)",
"{",
"\t\\$form = parent::getEditForm($id, $fields);",
"\t${0}",
"\treturn \\$form;",
"}"
],
"description": "Silverstripe getEditForm() method"
},
// Silverstripe Member
"SS Current Member": {
"prefix": [
"member",
"current-member"
],
"body": [
"Member::currentUser()"
],
"description": "Get the current member"
},
"SS Current Member ID": {
"prefix": [
"memberid",
"current-memberid"
],
"body": [
"Member::currentUserID()"
],
"description": "Get the current member ID"
},
// Silverstripe configuration
"SS Current SiteConfig": {
"prefix": [
"siteconfig",
"site-config"
],
"body": [
"SiteConfig::current_site_config()"
],
"description": "Get the SiteConfig object"
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment