Skip to content

Commit

Permalink
BUGFIX check if element relations exist (#72)
Browse files Browse the repository at this point in the history
* BUGFIX check if element relations exist

This is a FE Template breaking change that will require updating template variables referencing `$Content` in listing pages (Blog.ss) to reference `$FirstContent`

* UPDATE tests
  • Loading branch information
muskie9 authored Jul 8, 2019
1 parent 624d903 commit 4735bbf
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
30 changes: 19 additions & 11 deletions src/ORM/BlogPostDataExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
use SilverStripe\ORM\DataList;
use SilverStripe\ORM\FieldType\DBHTMLText;

/**
* Class BlogPostDataExtension
* @package Dynamic\Base\ORM
*/
class BlogPostDataExtension extends DataExtension
{
/**
Expand All @@ -22,12 +26,12 @@ public function updateCMSFields(FieldList $fields)
'CustomSummary',
));

$fields->insertAfter(TextField::create('SubTitle', 'Sub Title'), 'Title');
$fields->insertAfter('Title', TextField::create('SubTitle', 'Sub Title'));

$featured = $fields->dataFieldByName('FeaturedImage')
->setFolderName('Uploads/Blog')
;
$fields->insertBefore($featured, 'Content');
$fields->insertBefore('Content', $featured);
}

/**
Expand All @@ -54,17 +58,21 @@ public function getRelatedPosts()
/**
* Returns the content of the first content element block
*
* @return HTMLText
* @return DBHTMLText
*/
public function getContent()
public function getFirstContent()
{
$content = $this->owner->ElementalArea()
->Elements()->filter(array(
'ClassName' => ElementContent::class
))->first();
if ($content && $content->exists()) {
return $content->HTML;
if($this->owner->hasMethod('getElementalRelations') && $this->owner->getElementalRelations()){
$content = $this->owner->ElementalArea()
->Elements()->filter(array(
'ClassName' => ElementContent::class
))->first();
if ($content && $content->exists()) {
return $content->HTML;
}
return DBHTMLText::create();
}
return DBHTMLText::create();

return $this->owner->Content;
}
}
12 changes: 8 additions & 4 deletions tests/ORM/BlogPostDataExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,13 @@ public function testGetContent()
$expected = "Test";
/** @var BlogPost $post */
$post = $this->objFromFixture(TestBlogPost::class, 'one');
$this->assertEquals('', $post->getContent());
$this->assertEquals('', $post->getFirstContent());

$post->ElementalArea()->Elements()->add(ElementContent::create());
$this->assertEquals('', $post->getContent());
// todo update below test portion to be appropriate based on extension implementation
// currently the module does not apply any elemental extensions to blog, as we don't want it tightly coupled

/*$post->ElementalArea()->Elements()->add(ElementContent::create());
$this->assertEquals('', $post->getFirstContent());
$element = $post->ElementalArea()
->Elements()->filter([
Expand All @@ -77,6 +80,7 @@ public function testGetContent()
$element->HTML = $expected;
$element->write();
$this->assertEquals($expected, $post->getContent());
$this->assertEquals($expected, $post->getFirstContent());
//*/
}
}

0 comments on commit 4735bbf

Please sign in to comment.