Skip to content

Instantly share code, notes, and snippets.

View wilr's full-sized avatar

Will Rossiter wilr

View GitHub Profile
@wilr
wilr / admin.yml
Last active July 4, 2024 02:33
Big & Beautiful Silverstripe Elemental Previews
---
Name: "admin-custom"
---
SilverStripe\Admin\LeftAndMain:
extra_requirements_css:
- "app/client/element-preview.css"
@wilr
wilr / SubsiteAwareRedirectExtension.php
Created September 14, 2022 07:27
A Silverstripe DataExtension which adds subsite support to the redirectedurls module.
<?php
use SilverStripe\Forms\FieldList;
use SilverStripe\ORM\DataExtension;
use SilverStripe\ORM\DataQuery;
use SilverStripe\ORM\Queries\SQLSelect;
use SilverStripe\Subsites\Model\Subsite;
use SilverStripe\Subsites\State\SubsiteState;
class SubsiteAwareRedirectExtension extends DataExtension
@wilr
wilr / Project.php
Created December 7, 2021 21:23
Example of how to do searchable_fields with a many_many relation
<?php
use SilverStripe\ORM\DataObject;
use SilverStripe\Forms\DropdownField;
use SilverStripe\ORM\Filters\ExactMatchFilter;
class Project extends DataObject
{
private static $db = [
'Title' => 'Varchar(200)',
@wilr
wilr / GridFieldPrintLicencesButton.php
Last active December 7, 2020 22:46
GridField action button (for the whole list)
<?php
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\Convert;
use SilverStripe\Core\Extensible;
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\FieldType\DBDatetime;
use SilverStripe\ORM\FieldType\DBHTMLText;
use SilverStripe\Security\Security;
@wilr
wilr / GridFieldRestrictedRiderAction.php
Created December 7, 2020 22:43
Custom GridField row action example
<?php
use SilverStripe\Control\Controller;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\ValidationException;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridField_FormAction;
use SilverStripe\Forms\GridField\GridField_ColumnProvider;
use SilverStripe\Forms\GridField\GridField_ActionProvider;
use SilverStripe\Forms\GridField\GridField_ActionMenuItem;
@wilr
wilr / PostmarkMailTransport.php
Last active August 3, 2020 03:34
Postmark Mailing in Silverstripe 4.5
<?php
use Psr\Log\LoggerInterface;
use SilverStripe\Control\Email\Email;
use SilverStripe\Control\Director;
use SilverStripe\Core\Environment;
use SilverStripe\Core\Injector\Injector;
class PostmarkMailTransport extends \Postmark\Transport
{
@wilr
wilr / SecureControllerExtension.php
Last active February 10, 2020 23:08
Secure Silverstripe Controller Extension
<?php
use SilverStripe\Control\Director;
use SilverStripe\Control\HTTP;
use SilverStripe\Security\Member;
use SilverStripe\Core\Extension;
/**
* Implements Content-Security-Policy and other security headers on the
* controllers.
@wilr
wilr / composer-script.php
Last active November 14, 2022 07:46
Auto PHP Linting
<?php
// This script will copy a pre-commit hook from this repo to a developers local git
// The pre-commit hook will be run whenever a developer runs "git commit"
// This script will run after running composer install, via the "post-install-cmd" in composer.json
//
$gitHookFilename = '.git/hooks/pre-commit';
$shellHookFilename = 'pre-commit.sh';
if (file_exists($gitHookFilename)) {
unlink($gitHookFilename);
@wilr
wilr / CustomIndex.diff
Created May 14, 2019 22:06
solr-issues-silverstripe
--- a/app/src/Search/CustomerSolrIndex.php
+++ b/app/src/Search/CustomerSolrIndex.php
@@ -9,10 +9,14 @@ use SilverStripe\Control\Director;
*/
class CustomerSolrIndex extends GazetteSolrIndex
{
+ private static $casting = [
+ 'FieldDefinitions' => 'HTMLFragment',
+ 'CopyFieldDefinitions' => 'HTMLFragment'
+ ];
@wilr
wilr / require-alt-text.js
Last active February 23, 2019 18:39
SilverStripe 4, Require the user to enter alt text for the image
import Injector from 'lib/Injector';
Injector.transform(
'require-alt-text',
(updater) => {
updater.form.addValidation(
'AssetAdmin.*',
(values, Validation) => {
if (typeof values.AltText === 'undefined') {
return;