### Silverstripe CMS GraphQL Server Getting Started Source: https://docs.silverstripe.org/en/4/developer_guides/graphql This section guides users through setting up their first GraphQL server and building a schema within the Silverstripe CMS environment. ```php addType(TypeCreator::createFromDataObject(MyDataObject::class)); // Add a query $schema->addQuery('myQuery', function() { return 'Hello, GraphQL!'; }); return $schema; ?> ``` -------------------------------- ### PHP: SetUp() for Test Data and Configuration Source: https://docs.silverstripe.org/en/4/developer_guides/testing/unit_testing Demonstrates using the `setUp()` method in PHPUnit for SilverStripe tests. It includes creating multiple data fixtures (pages) and modifying configurations using `Config::modify()->update()`. This setup runs before each test method. ```PHP namespace App\Test; use SilverStripe\Core\Config\Config; use SilverStripe\Dev\SapphireTest; use SilverStripe\Versioned\Versioned; class PageTest extends SapphireTest { protected $usesDatabase = true; protected function setUp(): void { parent::setUp(); // create 100 pages for ($i = 0; $i < 100; $i++) { $page = new Page(['Title' => "Page $i"]); $page->write(); $page->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE); } // set custom configuration for the test. Config::modify()->update('Foo', 'bar', 'Hello!'); } public function testMyMethod() { // ... } public function testMySecondMethod() { // ... } } ``` -------------------------------- ### Install Silverstripe with Composer Source: https://docs.silverstripe.org/en/4/index This guide explains how to install the latest version of Silverstripe onto a web server using Composer. It assumes basic knowledge of PHP7 and Composer. ```bash composer create-project silverstripe/installer my-project-directory ``` -------------------------------- ### Silverstripe CMS Testing with Unit and Behavior Tests Source: https://docs.silverstripe.org/en/4/developer_guides Deploy robust applications by bundling Unit and Behavior tests with your application code and modules in Silverstripe CMS. This guide provides reference documentation and testing code examples. ```php objFromFixture(Page::class, 'homePage'); $this->assertEquals('Welcome to the homepage', $page->Title); } } ``` -------------------------------- ### Initialize Code at Document Ready Source: https://docs.silverstripe.org/en/4/developer_guides/customising_the_admin_interface/javascript_development Provides an example of how to ensure DOM elements are loaded before interacting with them by using jQuery's document.ready wrapper. ```JavaScript // DOM elements might not be available here $(document).ready(() => { // The DOM is fully loaded here }); ``` -------------------------------- ### Run Local Documentation Linting (Yarn & Composer) Source: https://docs.silverstripe.org/en/4/contributing/documentation This snippet shows the command-line instructions to install dependencies and run the documentation linter locally using Yarn and Composer. It's essential for ensuring code quality before submitting changes. ```bash yarn install composer install yarn lint ``` -------------------------------- ### QUnit Unit Test Example Source: https://docs.silverstripe.org/en/4/developer_guides/customising_the_admin_interface/javascript_development A basic unit test example using QUnit, a JavaScript unit testing framework. This test verifies a simple assertion and checks for equality between a string and a variable. ```JavaScript test('a basic test example', () => { ok(true, 'this test is fine'); const value = 'hello'; equals('hello', value, 'We expect value to be hello'); }); ``` -------------------------------- ### Silverstripe CMS v4 Documentation Overview Source: https://docs.silverstripe.org/en/4/developer_guides/model This snippet provides a high-level overview of the Silverstripe CMS v4 documentation structure. It lists the main sections available for developers and users, including getting started, developer guides, model and database information, templates, controllers, forms, configuration, and more. It also highlights the end-of-life status of version 4. ```text Project: /websites/silverstripe_en_4 Content: This app works best with JavaScript enabled. Silverstripe CMS DocumentationCMS Docs Search * V6V5V4V3 __ * __ * Getting Started * Server Requirements * Composer * Environment Management * Directory Structure * Recipes * Lessons * Developer Guides * Model and Databases * Introduction to the Data Model and ORM * Relations between Records * Managing Lists * Data Types, Overloading and Casting * Extending DataObjects * SearchFilter Modifiers * Model-Level Permissions * SQL Queries * Model Validation and Constraints * Versioning * Building Model and Search Interfaces around Scaffolding * Indexes * Managing Records * How To's * Templates and Views * Controllers * Forms * Configuration * Extending Silverstripe CMS * Testing * Debugging * Performance * Security * Email * Integration and Web Services * Search * i18n * Files * Customising the Admin Interface * Execution pipeline * Command Line Interface * Cookies and Sessions * GraphQL * Upgrading * Staying up to date with CMS releases * Upgrading a module * Migrating off CWP CMS recipe * Upgrading to Silverstripe CMS 4 * Upgrading to PHPUnit 9.5 for PHP8 support * Upgrading to GraphQL 4 * Deprecations * Upgrading Fluent * Changelogs * Contributing * Issues and Bug Reports * Contributing Code * Contributing Documentation * Contributing Translations * Coding conventions * Build tooling * Triage and peer review * Release process * Managing security issues * Project Governance * Core committers * Code of conduct * Maintainer guidelines * Request for comment * Major Release Policy * Minor Release Policy * Supported modules * Definition of Public API Version 4 __end of life This version of Silverstripe CMS will not recieve any additional bug fixes or documentation updates. Go to documentation for the most recent stable version. # Model and databases# In Silverstripe CMS, application data will be represented by a DataObject class. A `DataObject` subclass defines the data columns, relationships and properties of a particular data record. For example, Member is a `DataObject` which stores information about a person, CMS user or mail subscriber. ##### __Introduction to the Data Model and ORM Introduction to creating and querying a database records through the ORM (object-relational mapping) ##### __Relations between Records Relate models together using the ORM using has_one, has_many, and many_many. ##### __Managing Lists The SS_List interface allows you to iterate through and manipulate a list of objects. ##### __Data Types, Overloading and Casting Learn how data is stored going in and coming out of the ORM and how to modify it. ##### __Extending DataObjects Modify the data model without using subclasses. ##### __SearchFilter Modifiers Use suffixes on your ORM queries. ##### __Model-Level Permissions Reduce risk by securing models. ##### __SQL Queries Write and modify direct database queries through SQLExpression subclasses. ##### __Model Validation and Constraints Validate your data at the model level ##### __Versioning Add versioning to your database content through the Versioned extension. ##### __Building Model and Search Interfaces around Scaffolding A Model-driven approach to defining your application UI. ##### __Indexes Add Indexes to your Data Model to optimize database queries. ##### __Managing Records Manage your DataObject records ## How to's# ##### __Dynamic Default Fields Learn how to add default values to your models ##### __Grouping DataObject sets Learn how to split the results of a query into subgroups __ Edit on Github ``` -------------------------------- ### Silverstripe CMS Security Best Practices Source: https://docs.silverstripe.org/en/4/developer_guides This guide covers user authentication, the permission system, and how to secure your Silverstripe CMS code against malicious behaviors. It includes reference documentation and security code examples. ```php isLoggedIn() && Security::getCurrentUser()->can('ADMIN')) { // User is an admin } ``` -------------------------------- ### Silverstripe CMS Command Line Interface (CLI) Source: https://docs.silverstripe.org/en/4/developer_guides Automate Silverstripe CMS tasks, run Cron Jobs, or sync with other platforms through the Command Line Interface. This guide provides reference documentation and CLI command examples. ```bash ./vendor/bin/sake dev/build ./vendor/bin/sake flush=all ``` -------------------------------- ### JSpec Shopping Cart Test Example Source: https://docs.silverstripe.org/en/4/developer_guides/customising_the_admin_interface/javascript_development An example of a behavior-driven test for a ShoppingCart using JSpec. It demonstrates setting up a test environment with `before_each` and testing the `addProduct` method to ensure products are added correctly. ```JavaScript describe 'ShoppingCart' before_each cart = new ShoppingCart end describe 'addProduct' it 'should add a product' cart.addProduct('cookie') cart.addProduct('icecream') cart.should.have 2, 'products' end end end ``` -------------------------------- ### Silverstripe CMS GraphQL API Source: https://docs.silverstripe.org/en/4/developer_guides This guide covers integrating with and utilizing the GraphQL API in Silverstripe CMS. It includes reference documentation and examples for GraphQL queries and mutations. ```graphql { products { id title price } } ``` -------------------------------- ### PHP: setUpBeforeClass() and tearDownAfterClass() for Suite Setup Source: https://docs.silverstripe.org/en/4/developer_guides/testing/unit_testing Illustrates the use of static `setUpBeforeClass()` and `tearDownAfterClass()` methods in PHPUnit for SilverStripe tests. These methods execute once for the entire test class, allowing for setup and teardown operations that affect all test methods within the class. ```PHP namespace App\Test; use SilverStripe\Dev\SapphireTest; class PageTest extends SapphireTest { public static function setUpBeforeClass(): void { parent::setUpBeforeClass(); // ... } public static function tearDownAfterClass(): void { parent::tearDownAfterClass(); // ... } // ... } ``` -------------------------------- ### Build GraphQL Schema via CI/CD Source: https://docs.silverstripe.org/en/4/developer_guides/graphql/getting_started/deploying_the_schema Utilize a CI/CD pipeline to automate the GraphQL schema build process. This involves updating deployment scripts to execute the `dev/graphql/build` command, ensuring schema files are generated on each server in a multi-server setup. ```bash dev/graphql/build ``` -------------------------------- ### Install Silverstripe CMS with Composer Source: https://docs.silverstripe.org/en/4/getting_started Installs the Silverstripe CMS framework and its dependencies using Composer, a PHP package manager. This command creates a new project in the specified directory. ```bash composer create-project silverstripe/installer my-project ``` -------------------------------- ### Manage SilverStripe Sake Processes Source: https://docs.silverstripe.org/en/4/developer_guides/cli Provides examples of using the `sake` command to start, stop, and manage daemon processes within a SilverStripe application. These commands interact with the process controller defined elsewhere. ```bash sake -start my_process sake -stop my_process ``` -------------------------------- ### Silverstripe CMS Configuration System Source: https://docs.silverstripe.org/en/4/developer_guides Silverstripe CMS offers various methods for storing and modifying application settings, including site-wide settings and a YAML-based configuration system. This guide provides reference documentation and examples. ```yaml # config.yml example SilverStripe\Core\Config\Config: my_setting: 'some_value' ``` -------------------------------- ### Configure Silverstripe GraphQL Schema Source: https://docs.silverstripe.org/en/4/developer_guides/graphql/getting_started/deploying_the_schema This example shows how to add a basic type to your Silverstripe CMS GraphQL schema configuration. It's a foundational step for defining your API structure. ```yaml SilverStripe\GraphQL\SchemaBuilder\SchemaBuilder: # Add a basic type to the schema configuration schema_config: | query { hello: String } ``` -------------------------------- ### Install GraphQL DevTools for SilverStripe Source: https://docs.silverstripe.org/en/4/developer_guides/model/versioning This command installs the `silverstripe/graphql-devtools` package, which provides tools for exploring and interacting with your SilverStripe GraphQL schema directly from your browser, such as GraphiQL. ```bash composer require --dev silverstripe/graphql-devtools dev-master ``` -------------------------------- ### Example HTTPProvider Query Mapping Source: https://docs.silverstripe.org/en/4/developer_guides/graphql/tips_and_tricks An example of JSON content expected at the URL configured for HTTPProvider, mapping identifiers to GraphQL queries. ```json {"someUniqueID":"query{readMembers{Name+Email}}"} ``` -------------------------------- ### Example FileProvider Query Mapping Source: https://docs.silverstripe.org/en/4/developer_guides/graphql/tips_and_tricks An example of a JSON file content used by FileProvider to map unique identifiers to GraphQL queries. ```json {"someUniqueID":"query{validateToken{Valid Message Code}}"} ``` -------------------------------- ### Silverstripe CMS Unit Test Example Source: https://docs.silverstripe.org/en/4/developer_guides/testing/unit_testing Demonstrates a basic unit test for a custom method in a Silverstripe CMS Page model. It includes the model class and the corresponding test class using SapphireTest. ```php namespace { use SilverStripeCMSModelSiteTree; class Page extends SiteTree { public static function myMethod() { return (1 + 1); } } } ``` ```php namespace App\Test; use Page; use SilverStripe\Dev\SapphireTest; class PageTest extends SapphireTest { public function testMyMethod() { $this->assertEquals(2, Page::myMethod()); } } ``` -------------------------------- ### Granular Namespacing Example (Models) Source: https://docs.silverstripe.org/en/4/developer_guides/graphql/getting_started/configuring_your_schema This example demonstrates granular namespacing for GraphQL schema models. The file 'app/_graphql/news-and-blog/models/blog.yml' maps to 'SilverStripe\GraphQL\Schema\Schema.schemas.default.models'. ```yaml # app/_graphql/news-and-blog/models/blog.yml maps to SilverStripe\GraphQL\Schema\Schema.schemas.default.models ``` -------------------------------- ### JavaScript Object Spread Syntax Example Source: https://docs.silverstripe.org/en/4/developer_guides/customising_the_admin_interface/reactjs_redux_and_graphql A basic example demonstrating the JavaScript spread syntax for creating new objects with updated properties, equivalent to using `Object.assign()`. ```javascript CopynewProps = { ...oldProps, name: 'New name' }; ``` ```javascript CopynewProps = Object.assign( {}, oldProps, { name: 'New name' } ); ``` -------------------------------- ### Install Silverstripe Module with Composer Source: https://docs.silverstripe.org/en/4/developer_guides/extending/modules Installs the latest stable version of the 'silverstripe/blog' module using Composer. This command fetches the module and its dependencies, integrating it into the project. ```bash composer require silverstripe/blog *@stable ``` -------------------------------- ### Silverstripe CMS Execution Pipeline Overview Source: https://docs.silverstripe.org/en/4/developer_guides An overview of the steps involved in delivering a Silverstripe CMS web page, from request to response. This guide provides reference documentation on the execution flow. ```text Request -> Routing -> Controller -> Action -> Template Rendering -> Response ``` -------------------------------- ### Create New Silverstripe Site Source: https://docs.silverstripe.org/en/4/getting_started/composer Creates a new Silverstripe CMS project using Composer. It downloads the latest stable version of the installer by default. Options like '--prefer-source' or '--prefer-dist' can be used to include development fixtures or for a minimal installation, respectively. You can also specify a particular version to install. ```bash composer create-project silverstripe/installer my-project ``` ```bash composer create-project silverstripe/installer my-project --prefer-source ``` ```bash composer create-project silverstripe/installer ./my-project 4.3.3 ``` -------------------------------- ### Install Sake CLI Wrapper Source: https://docs.silverstripe.org/en/4/developer_guides/cli This command shows how to install the `sake` script, a wrapper for `cli-script.php`, by copying it to the system's binary directory. This process is specific to UNIX-like systems. ```bash cd your-webroot/ sudo ./vendor/bin/sake installsake ``` -------------------------------- ### Add Modules to Silverstripe Project Source: https://docs.silverstripe.org/en/4/getting_started/composer Installs Silverstripe CMS modules into your project using Composer. You can specify the module name and optionally a version constraint to install a specific version or range of versions. For example, 'silverstripe/blog' can be installed with a version constraint like '^2'. ```bash composer require silverstripe/blog ``` ```bash composer require silverstripe/blog ^2 ``` -------------------------------- ### Set Up Custom GraphQL Server (PHP) Source: https://docs.silverstripe.org/en/4/developer_guides/graphql/getting_started/activating_the_server This example demonstrates how to set up a custom GraphQL server in Silverstripe CMS. It involves configuring a new controller instance with a specific schema key and then routing requests to this custom controller. ```php SilverStripe\Core\Injector\Injector: # ... SilverStripe\GraphQL\Controller.myNewSchema: class: SilverStripe\GraphQL\Controller constructor: schemaKey: myNewSchema ``` ```php SilverStripe\Control\Director: rules: 'my-graphql': '%$SilverStripe\GraphQL\Controller.myNewSchema' ``` -------------------------------- ### Create New Project with Silverstripe Installer Source: https://docs.silverstripe.org/en/4/getting_started/recipes This command demonstrates how to create a new Silverstripe CMS project using the `silverstripe/installer` recipe, specifying a version constraint. ```bash composer create-project silverstripe/installer myproject ^4 ``` -------------------------------- ### Silverstripe CMS Controller Functionality Source: https://docs.silverstripe.org/en/4/developer_guides Controllers are the backbone of Silverstripe CMS applications, handling URL routing to your templates. This guide provides reference documentation and code examples for controller implementation. ```php renderWith('Product_show'); } } ``` -------------------------------- ### Install and Run Silverstripe Pattern Library Source: https://docs.silverstripe.org/en/4/developer_guides/customising_the_admin_interface/cms_architecture Instructions to install dependencies and run the Silverstripe pattern library locally using Composer and Yarn. It also covers building a static version. ```bash composer install --prefer-source (cd vendor/silverstripe/asset-admin && yarn install) (cd vendor/silverstripe/campaign-admin && yarn install) (cd vendor/silverstripe/cms && yarn install) cd vendor/silverstripe/admin && yarn install && yarn pattern-lib ``` -------------------------------- ### Extending Silverstripe CMS with Extensions and Dependency Injection Source: https://docs.silverstripe.org/en/4/developer_guides Understand how to modify built-in functionality through Extensions, Subclassing, and Dependency Injection in Silverstripe CMS. This guide includes reference documentation and code examples. ```php addMiddleware(new MyCustomMiddleware()); // Pass the request to the application and get the response $response = $app->handle($request); // Output the response $response->output(); ``` -------------------------------- ### Silverstripe CMS Integration and Web Services Source: https://docs.silverstripe.org/en/4/developer_guides Integrate other web services within your Silverstripe CMS application or make your Silverstripe CMS data available. This guide includes reference documentation and examples for API integration. ```php push([ 'id' => $product->ID, 'title' => $product->Title, 'price' => $product->Price->Nice(), ]); } return $this->customise(['data' => $list])->renderWith('JSON'); } ``` -------------------------------- ### GraphQL: Initial Product Query Source: https://docs.silverstripe.org/en/4/developer_guides/graphql/working_with_dataobjects/inheritance An example GraphQL query to read products and their prices. This query assumes a straightforward schema without inheritance complexities. ```GraphQL query { readProducts { nodes { price } } } ``` -------------------------------- ### Handling HTTP Requests with Silverstripe Application Source: https://docs.silverstripe.org/en/4/developer_guides/execution_pipeline/app_object_and_kernel Provides an example of setting up and handling an HTTP request using Silverstripe's HTTPApplication, including middleware. ```php use SilverStripe\Control\HTTPApplication; use SilverStripe\Control\HTTPRequestBuilder; use SilverStripe\Core\CoreKernel; use SilverStripe\Core\Startup\ErrorControlChainMiddleware; require __DIR__ . '/vendor/autoload.php'; // Build request and detect flush $request = HTTPRequestBuilder::createFromEnvironment(); // Default application $kernel = new CoreKernel(BASE_PATH); $app = new HTTPApplication($kernel); $app->addMiddleware(new ErrorControlChainMiddleware($app)); $response = $app->handle($request); $response->output(); ``` -------------------------------- ### Deploy GraphQL Schema Source: https://docs.silverstripe.org/en/4/developer_guides/graphql/getting_started This section provides instructions on how to deploy your GraphQL schema to a test or production environment. It's the final step in making your API accessible. ```markdown ##### __Deploying the schema Deploy your GraphQL schema to a test or production environment ``` -------------------------------- ### Customizing Silverstripe CMS Admin Interface Source: https://docs.silverstripe.org/en/4/developer_guides Extend the admin view to provide custom behavior or new features for CMS and admin users in Silverstripe CMS. This guide includes reference documentation and examples for admin customization. ```php assertTrue(true); } } ``` -------------------------------- ### Run Homepage Controller via Sake Source: https://docs.silverstripe.org/en/4/developer_guides/cli This command uses the `sake` utility to execute the homepage controller, effectively rendering the site's main page from the command line. ```bash sake / ``` -------------------------------- ### Perform GET Request in Silverstripe Source: https://docs.silverstripe.org/en/4/developer_guides/testing/functional_testing Executes a GET request on a specified URL and returns the HTTP response. This action also updates the current page to reflect the response. It's a fundamental part of testing controller logic that requires web requests. ```php $page = $this->get($url); ``` -------------------------------- ### Add Clearer Prerelease Installation Instructions Source: https://docs.silverstripe.org/en/4/Changelogs/4 This commit adds more explicit and clearer instructions for installing Silverstripe CMS prereleases. This aims to improve the user experience for those testing pre-release versions. ```php Add clearer instructions for prerelease installation ``` -------------------------------- ### jQuery.Entwine Highlighter Example Source: https://docs.silverstripe.org/en/4/developer_guides/customising_the_admin_interface/javascript_development Demonstrates how to use jQuery.entwine to create a 'Highlighter' behavior for buttons, allowing customization of foreground and background colors. ```JavaScript (function ($) { $(':button').entwine({ Foreground: 'red', Background: 'yellow', highlight() { this.css('background', this.getBackground()); this.css('color', this.getForeground()); } }); }(jQuery)); ``` -------------------------------- ### Initializing and Pushing a Git Repository Source: https://docs.silverstripe.org/en/4/developer_guides/extending/modules These Git commands initialize a new repository in the module's root directory, stage all files, create the initial commit, link it to a remote GitHub repository, and push the changes. ```bash git init git add -A git commit -m 'first commit' git remote add origin git@github.com:my_vendor/nice_feature.g.it git push -u origin master ``` -------------------------------- ### Get a Cookie Value in Silverstripe CMS Source: https://docs.silverstripe.org/en/4/developer_guides/cookies_and_sessions/cookies Provides an example of how to retrieve the value of a specific cookie by its name using the Cookie::get() method. ```php use SilverStripe\Control\Cookie; Cookie::get($name); // Cookie::get('MyApplicationPreference'); // returns 'Yes' ``` -------------------------------- ### Placeholder for Silverstripe History Viewer Frontend Boot File Source: https://docs.silverstripe.org/en/4/developer_guides/model/versioning This JavaScript file, `app/client/src/boot/index.js`, serves as the entry point for the frontend build process in a Silverstripe CMS project. It is intended to be populated with code that initializes the history viewer and other frontend functionalities. ```javascript // We'll populate this file later - for now we just need it to be sure our build setup works. ``` -------------------------------- ### jQuery.Entwine Usage Examples Source: https://docs.silverstripe.org/en/4/developer_guides/customising_the_admin_interface/javascript_development Illustrates common usage patterns for jQuery.entwine, including calling methods with default options, setting properties, and retrieving property values. ```JavaScript (function ($) { // call with default options $(':button').entwine().highlight(); // set options for existing and new instances $(':button').entwine().setBackground('green'); // get property $(':button').entwine().getBackground(); }(jQuery)); ``` -------------------------------- ### Configure Database Name Selection - Silverstripe Source: https://docs.silverstripe.org/en/4/getting_started/environment_management The SS_DATABASE_CHOOSE_NAME variable enables automatic database name selection based on the installation folder, simplifying setup and preventing manual configuration errors. ```php ; Automatically choose database name based on folder name ; SS_DATABASE_CHOOSE_NAME=true ; Use grandparent folder for database name ; SS_DATABASE_CHOOSE_NAME=3 ``` -------------------------------- ### Clearer Sysadmin Guidance for Packaging Source: https://docs.silverstripe.org/en/4/changelogs/4 Offers clearer guidance for system administrators regarding the 'packaging' process. This aims to simplify deployment and management tasks. ```php * 2021-06-01 fa3c5e6fe Clearer sysadmin guidance for "packaging" (#9960) (Ingo Schommer) ``` -------------------------------- ### Silverstripe CMS Templating and Theme Development Source: https://docs.silverstripe.org/en/4/developer_guides This guide showcases the Silverstripe CMS template engine and teaches you how to build your own themes. It includes reference documentation and code snippets for creating custom templates. ```html <%-- Example Silverstripe Template --%>
Price: $Price.Nice% <% if HasCategory %>
Category: $Category.Title
<% end_if %>