Skip to content

Commit

Permalink
add support for Elemental 4 (#17)
Browse files Browse the repository at this point in the history
* update singular and plural names of TestimonialCategory to Category
  • Loading branch information
jsirish committed Jan 20, 2019
1 parent eb9c5e1 commit f730808
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 16 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -15,8 +15,8 @@ Testimonial Element for SilverStripe 4.

## Requirements

* SilverStripe ^4.0
* SilverStripe Elemental ^2.0
* silverstripe/recipe-cms: ^4@dev
* dnadesign/silverstripe-elemental: ^4@dev

## Installation

Expand Down
6 changes: 3 additions & 3 deletions composer.json
Expand Up @@ -16,9 +16,9 @@
"type": "silverstripe-vendormodule",
"license": "BSD-3-Clause",
"require": {
"silverstripe/recipe-cms": "^1@dev || ^4@dev",
"silverstripe/recipe-cms": "^4@dev",
"silverstripe/vendor-plugin": "^1@dev",
"dnadesign/silverstripe-elemental": "^2@dev || ^3@dev",
"dnadesign/silverstripe-elemental": "^4@dev",
"symbiote/silverstripe-gridfieldextensions": "^3@dev"
},
"require-dev": {
Expand All @@ -38,7 +38,7 @@
"prefer-stable": true,
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
"dev-master": "2.0.x-dev"
}
},
"scripts": {
Expand Down
48 changes: 41 additions & 7 deletions src/Elements/ElementTestimonials.php
Expand Up @@ -7,6 +7,7 @@
use Dynamic\Elements\Model\TestimonialCategory;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter;
use SilverStripe\Forms\ListboxField;
use SilverStripe\ORM\DB;
use SilverStripe\ORM\FieldType\DBField;
use Symbiote\GridFieldExtensions\GridFieldAddExistingSearchButton;
Expand All @@ -23,7 +24,7 @@ class ElementTestimonials extends BaseElement
/**
* @var string
*/
private static $icon = 'vendor/dnadesign/silverstripe-elemental/images/base.svg';
private static $icon = 'font-icon-chat';

/**
* @var string
Expand Down Expand Up @@ -65,9 +66,33 @@ class ElementTestimonials extends BaseElement
/**
* @return DBHTMLText
*/
public function ElementSummary()
public function getSummary()
{
return DBField::create_field('HTMLText', $this->Content)->Summary(20);
if ($this->TestimonialCategories()->count() > 0) {
$ct = 0;
foreach ($this->TestimonialCategories() as $category) {
$ct += $category->Testimonials()->count();
}
if ($ct == 1) {
$label = ' testimonial';
} else {
$label = ' testimonials';
}
return DBField::create_field(
'HTMLText',
$ct . $label
)->Summary(20);
}
}

/**
* @return array
*/
protected function provideBlockSchema()
{
$blockSchema = parent::provideBlockSchema();
$blockSchema['content'] = $this->getSummary();
return $blockSchema;
}

/**
Expand All @@ -88,12 +113,21 @@ public function getCMSFields()
->setRows(8);

$fields->dataFieldByName('Limit')
->setTitle('Testimonials to show');
->setTitle('Number of testimonials to show');

if ($this->exists()) {
$config = $fields->dataFieldByName('TestimonialCategories')->getConfig();
$config->removeComponentsByType(GridFieldAddExistingAutocompleter::class);
$config->addComponent(new GridFieldAddExistingSearchButton());
$fields->removeByName([
'TestimonialCategories'
]);

$fields->addFieldToTab('Root.Main',
'Limit',
ListboxField::create(
'TestimonialCategories',
'Categories',
TestimonialCategory::get()->map()->toArray()
)
);
}
});

Expand Down
4 changes: 2 additions & 2 deletions src/Model/TestimonialCategory.php
Expand Up @@ -16,12 +16,12 @@ class TestimonialCategory extends DataObject
/**
* @var string
*/
private static $singular_name = 'Testimonial Category';
private static $singular_name = 'Category';

/**
* @var string
*/
private static $plural_name = 'Testimonial Categories';
private static $plural_name = 'Categories';

/**
* @var string
Expand Down
4 changes: 2 additions & 2 deletions tests/Elements/ElementTestimonialsTest.php
Expand Up @@ -23,10 +23,10 @@ class ElementTestimonialsTest extends SapphireTest
/**
*
*/
public function testGetElementSummary()
public function testGetSummary()
{
$element = $this->objFromFixture(ElementTestimonials::class, 'one');
$this->assertEquals($element->dbObject('Content')->Summary(20), $element->ElementSummary());
$this->assertEquals('4 testimonials', $element->getSummary());
}

/**
Expand Down

0 comments on commit f730808

Please sign in to comment.