Skip to content
This repository was archived by the owner on Oct 13, 2025. It is now read-only.

Commit 6bf96e7

Browse files
author
Otis Wright
committed
Init
1 parent 467d6c4 commit 6bf96e7

14 files changed

+541
-0
lines changed

.editorconfig

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# For more information about the properties used in this file,
2+
# please see the EditorConfig documentation:
3+
# http://editorconfig.org
4+
5+
[*]
6+
charset = utf-8
7+
end_of_line = lf
8+
indent_size = 4
9+
indent_style = space
10+
insert_final_newline = true
11+
trim_trailing_whitespace = true
12+
13+
[{*.yml,package.json}]
14+
indent_size = 2
15+
16+
# The indent size used in the package.json file cannot be changed:
17+
# https://github.com/npm/npm/pull/3180#issuecomment-16336516

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/.gitignore export-ignore

.scrutinizer.yml

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
inherit: true
2+
3+
checks:
4+
php:
5+
verify_property_names: true
6+
verify_argument_usable_as_reference: true
7+
verify_access_scope_valid: true
8+
useless_calls: true
9+
use_statement_alias_conflict: true
10+
variable_existence: true
11+
unused_variables: true
12+
unused_properties: true
13+
unused_parameters: true
14+
unused_methods: true
15+
unreachable_code: true
16+
too_many_arguments: true
17+
sql_injection_vulnerabilities: true
18+
simplify_boolean_return: true
19+
side_effects_or_types: true
20+
security_vulnerabilities: true
21+
return_doc_comments: true
22+
return_doc_comment_if_not_inferrable: true
23+
require_scope_for_properties: true
24+
require_scope_for_methods: true
25+
require_php_tag_first: true
26+
psr2_switch_declaration: true
27+
psr2_class_declaration: true
28+
property_assignments: true
29+
prefer_while_loop_over_for_loop: true
30+
precedence_mistakes: true
31+
precedence_in_conditions: true
32+
phpunit_assertions: true
33+
php5_style_constructor: true
34+
parse_doc_comments: true
35+
parameter_non_unique: true
36+
parameter_doc_comments: true
37+
param_doc_comment_if_not_inferrable: true
38+
optional_parameters_at_the_end: true
39+
one_class_per_file: true
40+
no_unnecessary_if: true
41+
no_trailing_whitespace: true
42+
no_property_on_interface: true
43+
no_non_implemented_abstract_methods: true
44+
no_error_suppression: true
45+
no_duplicate_arguments: true
46+
no_commented_out_code: true
47+
newline_at_end_of_file: true
48+
missing_arguments: true
49+
method_calls_on_non_object: true
50+
instanceof_class_exists: true
51+
foreach_traversable: true
52+
fix_line_ending: true
53+
fix_doc_comments: true
54+
duplication: true
55+
deprecated_code_usage: true
56+
deadlock_detection_in_loops: true
57+
code_rating: true
58+
closure_use_not_conflicting: true
59+
catch_class_exists: true
60+
blank_line_after_namespace_declaration: false
61+
avoid_multiple_statements_on_same_line: true
62+
avoid_duplicate_types: true
63+
avoid_conflicting_incrementers: true
64+
avoid_closing_tag: true
65+
assignment_of_null_return: true
66+
argument_type_checks: true
67+
68+
filter:
69+
paths: [code/*, tests/*]

README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Installation Instructions #
2+
## Composer ##
3+
Run the following to add this module as a requirement and install it via composer.
4+
5+
```
6+
#!bash
7+
8+
composer require "webfox/silverstripe-helpers"
9+
```
10+
then browse to /dev/build?flush=all
11+
12+
Several of these helpers are disabled by default but can be enabled in the config.
13+
14+
15+
#Requirements#
16+
* Silverstripe 3.1+
17+
* php5.4+
18+
* php5-intl Package
19+
20+
#Extension Overview#
21+
22+
##DataListExtension##
23+
- Adds a new *ListColumn* function to a DataList to allow implode() style functionality of the list items
24+
e.g. `$list->ListColumn('ID', ' |')` would result in something like "1 |18 |19 |24".
25+
- Automatically applied
26+
27+
##ExtraPageFieldsExtension##
28+
- Moves the metadata fields to a new tab
29+
- Adds a new "Meta Title" field
30+
- Automatically applied
31+
32+
##FooterMenuExtension##
33+
- Adds a new *ShowInFooter* option to the page settings
34+
- Adds a new `FooterPages()` function to the SiteTree to return only pages that have this checked
35+
36+
##ImageExtension##
37+
- Adds a new *Image Alt Text* option to the image settings
38+
- Makes the image settings fields full width
39+
- Automatically applied
40+
41+
##LinkExtension##
42+
- Adds a new *SEO Text* option to the link settings
43+
- Adds a *force download* checkbox to the link if it's set to a file.
44+
- Automatically applied
45+
46+
#Template Providers Overview#
47+
48+
##HelpersTemplateProvider
49+
- Adds a new `Repeat($times)` function to the templates to return a loopable list (kind of like a for loop)
50+
- Adds a new `Dump($obj)` function to the templates to allow easy dumping of any template variable
51+
52+
#Extendable Classes Overview#
53+
54+
##OwnerPermissionedDataObject##
55+
- Allows data objects to inherit the access permissions from their attached relation parent
56+
- Relation parent defaults to `Page()`
57+
- If `static::$relationOwner` is set to `null` will fall back to asking the standard `Page` for permission

_config/.gitkeep

Whitespace-only changes.

_config/config.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
Name: silverstripe-helpers
3+
---
4+
DataList:
5+
extensions:
6+
- DataListExtension
7+
8+
SiteTree:
9+
extensions:
10+
- ExtraPageFieldsExtension
11+
12+
Image:
13+
extensions:
14+
- ImageExtension
15+
16+
Link:
17+
extensions:
18+
- LinkExtension
19+
20+
#SiteTree:
21+
# extensions:
22+
# - FooterMenuExtension

code/DataListExtension.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/**
4+
* Class DataListExtension
5+
*
6+
* @property DataList owner
7+
*/
8+
class DataListExtension extends Extension
9+
{
10+
11+
public function ListColumn($column, $glue = ', ')
12+
{
13+
return implode($glue, $this->owner->column($column));
14+
}
15+
16+
public function Displayed($visible = true)
17+
{
18+
$list = clone $this->owner;
19+
20+
return $list->filter('Displayed', $visible);
21+
}
22+
23+
public function OnlyVisible()
24+
{
25+
return $this->Displayed(true);
26+
}
27+
28+
public function OnlyHidden()
29+
{
30+
return $this->Displayed(false);
31+
}
32+
33+
public function WithVisible($relation)
34+
{
35+
$list = clone $this->owner;
36+
37+
return $list->filterByCallback(function ($obj) use ($relation) {
38+
return $obj->{$relation}()->OnlyVisible()->exists();
39+
});
40+
}
41+
42+
}

code/ExtraPageFieldsExtension.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
/**
4+
* Class ExtraPageFieldsExtension
5+
*
6+
*/
7+
class ExtraPageFieldsExtension extends SiteTreeExtension
8+
{
9+
10+
private $MetaDescriptionLength = 156;
11+
12+
private static $db = array(
13+
'SubTitle' => 'Text',
14+
'MetaTitle' => 'Text',
15+
'MenuTarget' => 'Varchar(255)'
16+
);
17+
18+
public function updateCMSFields(FieldList $fields)
19+
{
20+
21+
//change Page Name label to Primary Heading - H1 - Only if the title hasn't already been changed
22+
/** @var TextField $titleField */
23+
$titleField = $fields->dataFieldByName('Title');
24+
if ($titleField->Title() == 'Page name') {
25+
$fields->renameField('Title', 'Primary Heading');
26+
}
27+
28+
//Add secondary heading - H2
29+
$fields->insertAfter(TextField::create('SubTitle', 'Secondary Heading'), 'Title');
30+
31+
//Move meta fields to their own tab
32+
33+
/** @var ToggleCompositeField $metaDataChildren */
34+
$metaDataChildren = $fields->fieldByName('Root.Main.Metadata');
35+
// ddd($metaDataChildren->fieldByName('MetaDescription'));
36+
$children = array_merge([$metaTitle = TextField::create('MetaTitle')], $metaDataChildren->getChildren()->toArray());
37+
$fields->removeFieldFromTab('Root.Main', 'Metadata');
38+
$fields->addFieldToTab('Root', Tab::create('Metadata'), 'Content');
39+
40+
//Add META Title tag to METADATA
41+
$length = $this->owner->config()->MetaDescriptionLength ?: $this->MetaDescriptionLength;
42+
$metaDataChildren->fieldByName('MetaDescription')->setAttribute('maxlength', $length);
43+
$fields->addFieldsToTab('Root.Metadata', $children);
44+
45+
$metaTitle->setDescription('Displayed as the tab/window name; Also displayed in search engine result listings as the page title.<br />
46+
Falls back to the Primary Heading field if not provided.');
47+
48+
}
49+
50+
public function updateSettingsFields(FieldList $fields)
51+
{
52+
//quick links option
53+
$fields->addFieldToTab("Root.Settings", new DropdownField('MenuTarget', 'Open page in', [
54+
'' => 'Current Tab (Browser default)',
55+
'_blank' => 'New Tab'
56+
]));
57+
}
58+
59+
Public function MenuTarget()
60+
{
61+
return empty($this->owner->MenuTarget) ? '' : "target=\"{$this->owner->MenuTarget}\"";
62+
}
63+
64+
Public function MetaTags(& $tags) {
65+
if(is_a(Director::get_current_page(), 'Security')){
66+
$tags = $tags . '<meta name="robots" content="noindex">';
67+
}
68+
}
69+
70+
public function onBeforeWrite()
71+
{
72+
parent::onBeforeWrite();
73+
74+
$length = $this->owner->config()->MetaDescriptionLength ?: $this->MetaDescriptionLength;
75+
if (strlen($this->owner->MetaDescription) > $length) {
76+
/** @var Text $value */
77+
$value = $this->owner->dbObject('MetaDescription');
78+
$value = $value->LimitCharacters($length);
79+
$this->owner->MetaDescription = $value;
80+
};
81+
}
82+
}
83+
84+

code/FooterMenuExtension.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/**
4+
* Class FooterMenuExtension
5+
*
6+
*/
7+
class FooterMenuExtension extends SiteTreeExtension {
8+
9+
private static $db = array(
10+
'ShowInFooter' => 'Boolean'
11+
);
12+
13+
public function updateSettingsFields(FieldList $fields) {
14+
//quick links option
15+
$fields->addFieldToTab("Root.Settings", new CheckBoxField('ShowInFooter', 'Show in footer menu?'), 'ShowInSearch');
16+
}
17+
18+
Public function FooterPages(){
19+
return SiteTree::get()->filter('ShowInFooter', true);
20+
}
21+
22+
}

code/HelpersTemplateProvider.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
class HelpersTemplateProvider implements TemplateGlobalProvider {
4+
/**
5+
* @return array
6+
*/
7+
public static function get_template_global_variables() {
8+
return array(
9+
'Repeat',
10+
'Dump'
11+
);
12+
}
13+
14+
public static function Repeat($times){
15+
$list = new ArrayList();
16+
$formatter = new NumberFormatter('en-NZ', NumberFormatter::SPELLOUT);
17+
for($i = 1; $i <= $times; $i++){
18+
$list->push(array('Num' => $i, 'Word' => ucwords(strtolower($formatter->format($i)))));
19+
}
20+
21+
return $list;
22+
}
23+
24+
public static function Dump($object){
25+
dd($object);
26+
}
27+
}

0 commit comments

Comments
 (0)