Skip to content

Commit

Permalink
Merge pull request #69 from ajoneil/master
Browse files Browse the repository at this point in the history
Add "Available Globally" setting to elements
  • Loading branch information
wilr authored Mar 20, 2017
2 parents 1b1114b + e2337f4 commit e7374bf
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ classes:
- 'image_small' : 'Small'
````

### Limiting global elements

By default any element is available to be linked to multiple pages. This can be
changed with the "Available globally" checkbox in the settings tab of each element.
The default can be changed so that global elements are opt-in:
````
BaseElement:
default_global_elements: false
````

### Defining your own elements.

An element is as simple as a class which extends `BaseElement`. After you add the class, ensure you have rebuilt your
Expand Down
4 changes: 3 additions & 1 deletion code/extensions/ElementPageExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,11 @@ public function updateCMSFields(FieldList $fields)
->addComponent(new GridFieldSortableRows('Sort'))
);

$searchList = BaseElement::get()->filter('AvailableGlobally', true);
if($list) {
$autocomplete->setSearchList(BaseElement::get()->filter('ClassName', array_keys($list)));
$searchList = $searchList->filter('ClassName', array_keys($list));
}
$autocomplete->setSearchList($searchList);

$autocomplete->setResultsFormat('($ID) $Title');
$autocomplete->setSearchFields(array('ID', 'Title'));
Expand Down
26 changes: 19 additions & 7 deletions code/models/BaseElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class BaseElement extends Widget
*/
private static $db = array(
'ExtraClass' => 'Varchar(255)',
'HideTitle' => 'Boolean'
'HideTitle' => 'Boolean',
'AvailableGlobally' => 'Boolean(1)'
);

/**
Expand Down Expand Up @@ -55,7 +56,8 @@ class BaseElement extends Widget
),
'Title',
'LastEdited',
'ClassName'
'ClassName',
'AvailableGlobally'
);

/**
Expand All @@ -65,7 +67,7 @@ class BaseElement extends Widget

/**
* Enable for backwards compatibility
*
*
* @var boolean
*/
private static $disable_pretty_anchor_name = false;
Expand All @@ -91,6 +93,16 @@ class BaseElement extends Widget
*/
public $virtualOwner;

/**
* @config
* Elements available globally by default
*/
private static $default_global_elements = true;

public function populateDefaults() {
$this->AvailableGlobally = $this->config()->get('default_global_elements');
parent::populateDefaults();
}

public function getCMSFields()
{
Expand All @@ -105,6 +117,7 @@ public function getCMSFields()
$fields->removeByName('ParentID');
$fields->removeByName('Sort');
$fields->removeByName('ExtraClass');
$fields->removeByName('AvailableGlobally');

if (!$this->config()->enable_title_in_template) {
$fields->removeByName('HideTitle');
Expand All @@ -116,6 +129,7 @@ public function getCMSFields()
}

$fields->addFieldToTab('Root.Settings', new TextField('ExtraClass', 'Extra CSS Classes to add'));
$fields->addFieldToTab('Root.Settings', new CheckboxField('AvailableGlobally', 'Available globally - can be linked to multiple pages'));

if (!is_a($this, 'ElementList')) {
$lists = ElementList::get()->filter('ParentID', $this->ParentID);
Expand Down Expand Up @@ -376,9 +390,9 @@ public function getPage()
public function forTemplate($holder = true)
{
$config = SiteConfig::current_site_config();

if ($config->Theme) Config::inst()->update('SSViewer', 'theme', $config->Theme);

return $this->renderWith($this->class);
}

Expand Down Expand Up @@ -472,5 +486,3 @@ public function getPublishedVirtualLinkedElements() {
return $v;
}
}


0 comments on commit e7374bf

Please sign in to comment.