Skip to content

Instantly share code, notes, and snippets.

View jkersu's full-sized avatar

Jon Su jkersu

View GitHub Profile
@jkersu
jkersu / gist:f7ae8bdacb7a7178662ca2701c355167
Created December 1, 2021 03:26
Add a button to a Silverstripe Page in the CMS
// Add an extension to SilverStripe\Admin\LeftAndMain
class LeftAndMainExtension extends Extension
{
public function someFancyAction($data, $form)
{
// do some fancy stuff
}
}
@jkersu
jkersu / gist:8502975a0265604ffe3625d622557885
Created December 1, 2021 03:23
Add a new button inside a Gridfield individual item for Silverstripe
// Create an extension attached to: SilverStripe\Forms\GridField\GridFieldDetailForm_ItemRequest
class GridFieldDetailFormItemRequestExtension extends Extension
{
public function updateFormActions(FieldList $actions)
{
$record = $this->owner->getRecord();
// This extension would run on every GridFieldDetailForm, so ensure you ignore contexts where
// you are managing a DataObject you don't care about
@jkersu
jkersu / example.php
Created June 4, 2020 11:56
SilverStripe 4 - Upload file from uploaded file from frontend form
if (isset($data['FileID']) && $data['FileID'] && $data['FileID']['error'] == 0) {
$attachment = $data['FileID'];
try {
$upload = Upload::create();
$file = File::create();
$file->Title = $attachment['name'];
$folder = Folder::find_or_make('Uploads/FolderName/' . Security::getCurrentUser()->ID);
$upload->loadIntoFile($attachment, $file, $folder->getFilename());
$fileID = $file->write();
$app->AttachmentID = $fileID;
@jkersu
jkersu / gist:f575e888273c46b51aa4115f2dda4d70
Created April 7, 2020 06:23
ModelAdmin Search Filter
private static $searchable_fields = [
'Title' => [
'title' => 'Title'
],
'Year' => [
'title' => 'Year',
'field' => DropdownField::class
],
'ProductType.Title' => [
'title' => 'Product Type',
@jkersu
jkersu / gist:bacb2dcbd04302aca217bf3b1ebd442b
Last active March 10, 2020 09:50
Basic Site Search in SilverStripe 4
Sometimes when implementing search functionality on a SilverStripe website, you may find youself just needing a very basic site search without wanting to implement a full-scale search platform like Solr. In that case, SilverStripe 4 by default offers the ability to search Pages and File objects directly out of the box. This functionality is handled in ContentControllerSearchExtension.php which means that any Controllers that extend off ContentController.php can enable the Site Search functionality.
To setup a basic site search, there are only 3 key steps required:
Step 1:
We first want to enable the search engine and this can be done simply by adding the following to mysite/_config.php. By default this will search both Pages and Files objects. If you want to restrict the search to only 1 type, you can also pass Page::class or File::class into the enable() method as a parameter:
FulltextSearchable::enable();