-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from muskie9/enhancement/pageSectionBlock
ENHANCEMENT PageSectionBlock
- Loading branch information
Showing
4 changed files
with
242 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
/** | ||
* Class PageSectionBlock | ||
* | ||
* @method HasManyList $Sections | ||
*/ | ||
class PageSectionBlock extends Block | ||
{ | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private static $singular_name = 'Page Section Block'; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private static $plural_name = 'Page Section Blocks'; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private static $has_many = [ | ||
'Sections' => 'PageSectionObject', | ||
]; | ||
|
||
/** | ||
* @return FieldList | ||
*/ | ||
public function getCMSFields() | ||
{ | ||
$fields = parent::getCMSFields(); | ||
|
||
if ($this->ID) { | ||
// Sections | ||
$config = GridFieldConfig_RecordEditor::create(); | ||
if (class_exists('GridFieldSortableRows')) { | ||
$config->addComponent(new GridFieldSortableRows('SortOrder')); | ||
} | ||
$config->removeComponentsByType('GridFieldAddExistingAutocompleter'); | ||
$config->removeComponentsByType('GridFieldDeleteAction'); | ||
$config->addComponent(new GridFieldDeleteAction(false)); | ||
$sectionsField = GridField::create('Sections', 'Sections', $this->Sections()->sort('SortOrder'), $config); | ||
$fields->addFieldsToTab('Root.Sections', array( | ||
$sectionsField, | ||
)); | ||
} | ||
|
||
return $fields; | ||
} | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
public function getPageSections() | ||
{ | ||
return $this->Sections()->sort('SortOrder'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
<?php | ||
|
||
/** | ||
* Class PageSectionObject | ||
* | ||
* @property string $Name | ||
* @property string $Title | ||
* @property HTMLText $Content | ||
* @property int $SortOrder | ||
* @property int $ImageID | ||
* @property int $PageSectionBlockID | ||
*/ | ||
class PageSectionObject extends DataObject | ||
{ | ||
/** | ||
* @return string | ||
*/ | ||
private static $singular_name = 'Page Section'; | ||
|
||
/** | ||
* @return string | ||
*/ | ||
private static $plural_name = 'Page Sections'; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private static $db = array( | ||
'Name' => 'Varchar(255)', | ||
'Title' => 'Varchar(255)', | ||
'Content' => 'HTMLText', | ||
'SortOrder' => 'Int', | ||
); | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private static $has_one = array( | ||
'Image' => 'Image', | ||
'PageSectionBlock' => 'PageSectionBlock', | ||
); | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private static $default_sort = 'Name ASC'; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private static $summary_fields = array( | ||
'Image.CMSThumbnail' => 'Image', | ||
'Name' => 'Name', | ||
'Title' => 'Title', | ||
); | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private static $searchable_fields = array( | ||
'Name' => 'Name', | ||
'Title' => 'Title', | ||
); | ||
|
||
/** | ||
* @return FieldList | ||
*/ | ||
public function getCMSFields() | ||
{ | ||
$fields = parent::getCMSFields(); | ||
|
||
$fields->removeByName(array( | ||
'PageSectionBlockID', | ||
'SortOrder', | ||
)); | ||
|
||
$fields->dataFieldByName('Name')->setDescription('For internal reference only'); | ||
|
||
$image = $fields->dataFieldByName('Image'); | ||
$image->setFolderName('Uploads/PageSections'); | ||
$fields->insertBefore($image, 'Content'); | ||
|
||
return $fields; | ||
} | ||
|
||
/** | ||
* @return ValidationResult | ||
*/ | ||
public function validate() | ||
{ | ||
$result = parent::validate(); | ||
|
||
if (!$this->Name) { | ||
$result->error('Name is requied before you can save'); | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
* Set permissions, allow all users to access by default. | ||
* Override in descendant classes, or use PermissionProvider. | ||
*/ | ||
|
||
/** | ||
* @param null $member | ||
* | ||
* @return bool | ||
*/ | ||
public function canCreate($member = null) | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* @param null $member | ||
* | ||
* @return bool | ||
*/ | ||
public function canView($member = null) | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* @param null $member | ||
* | ||
* @return bool | ||
*/ | ||
public function canEdit($member = null) | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* @param null $member | ||
* | ||
* @return bool | ||
*/ | ||
public function canDelete($member = null) | ||
{ | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
/** | ||
* Class PageSectionBlockTest | ||
*/ | ||
class PageSectionBlockTest extends SapphireTest | ||
{ | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected static $fixture_file = 'dynamic-blocks/tests/Fixtures.yml'; | ||
|
||
/** | ||
* | ||
*/ | ||
public function testGetCMSFields() | ||
{ | ||
$object = $this->objFromFixture('PageSectionBlock', 'one'); | ||
$fields = $object->getCMSFields(); | ||
$this->assertInstanceOf('FieldList', $fields); | ||
$this->assertNotNull($fields->dataFieldByName('Sections')); | ||
} | ||
|
||
} |