Skip to content

Instantly share code, notes, and snippets.

View danaenz's full-sized avatar

Danae danaenz

  • Silverstripe
  • New Zealand
View GitHub Profile
@danaenz
danaenz / Form.php
Last active January 24, 2018 03:58
Custom json validation response to avoid parsing errors on SilverStripe forms
<?php
protected function getValidationErrorResponse()
{
$request = $this->getRequest();
if ($request->isAjax()) {
$validationErrors = [];
foreach ($this->validator->getErrors() as $error) {
@danaenz
danaenz / htmlentities.php
Created January 9, 2018 01:26
Removes html encoded non displayable characters
// Remove entity-encoded U+FFFD replacement character. It signifies non-displayable characters,
// and shows up as an encoding error in browsers.
$result->Excerpt = DBField::create_field(
'HTMLText',
str_replace(
'&#65533;',
'',
implode(' ... ', $combinedHighlights)
)
@danaenz
danaenz / CMSMainExtension.php
Last active August 31, 2017 23:47
Hide preview panel on pages. Add private static $hide_preview_panel = true on pages
<?php
/**
* Extension to disable the preview on pages that don't need it.
* To hide the preview on a page, add this to the class:
*
* private static $hide_preview_panel = true;
*/
class CMSMainExtension extends Extension
{
@danaenz
danaenz / DataObject.php
Created February 10, 2017 02:27
DataObject create default tabset
$fields = FieldList::create(TabSet::create('Root'));
xvfb-run -a -s "-screen 0 640x480x16" wkhtmltopdf "$@"
@danaenz
danaenz / RequiredFieldsValidator.php
Created August 26, 2016 00:39
Example required fields validator, conditional based on a type field
<?php
class CustomValidator extends RequiredFields
{
public function php($data)
{
if ($data['Type'] == 'Video') {
$this->addRequiredField('VideoImage');
}
@danaenz
danaenz / Slug.php
Created June 28, 2016 07:31
Creates a URL Slug based off the Title
<?php
public function onBeforeWrite()
{
/** =========================================
* @var URLSegmentFilter $filter
* ========================================*/
if ($this->isChanged('Title') || !$this->getField('Slug')) {
$filter = singleton('URLSegmentFilter');
@danaenz
danaenz / ArrayToXML.php
Created June 24, 2016 03:51
Recursive array to XML functionality
<?php
/**
* Source: http://stackoverflow.com/a/5965940/2
*
* @param array $data
* @param \SimpleXMLElement $xml_data
*/
protected function array_to_xml($data, &$xml_data)
{
foreach ($data as $key => $value) {
@danaenz
danaenz / BuildTask.php
Last active June 24, 2016 03:52
BuildTask skeleton
<?php
class MyTask extends BuildTask
{
protected $title = 'My Task';
protected $description = 'My task description';
protected $br;
protected $hr;
public function __construct()
@danaenz
danaenz / xdebug.sh
Created June 1, 2016 01:32
Configuration for OS X SnowLeapord when installing xdebug
phpize
CFLAGS=-m32 CPPFLAGS=-m32 CCASFLAGS=-m32 ./configure
make
sudo make install