Skip to content

Commit

Permalink
FormBlock - initial build
Browse files Browse the repository at this point in the history
fixes #5
  • Loading branch information
jsirish committed Oct 11, 2016
1 parent 3835481 commit 4a30bf3
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 0 deletions.
88 changes: 88 additions & 0 deletions code/blocks/FormBlock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

class FormBlock extends Block
{
/**
* @var string
*/
private static $singular_name = 'Form Block';

/**
* @var string
*/
private static $plural_name = 'Form Blocks';

/**
* @var array
*/
private static $db = array(
'Content' => 'HTMLText',
);

/**
* @var array
*/
private static $has_one = array(
'Form' => 'UserDefinedForm',
);

/**
* @return FieldList
*/
public function getCMSFields()
{
$fields = singleton('Block')->getCMSFields();

if (class_exists('UserDefinedForm')) {
$fields->addFieldToTab('Root.Main', DropdownField::create(
'FormID',
'Form',
UserDefinedForm::get()->map()
)->setEmptyString(''));
}

$fields->addFieldToTab('Root.Main', HtmlEditorField::create('Content'));

return $fields;
}

/**
* @return Forms|HTMLText
*/
public function BlockForm()
{
if ($this->Form()->exists()) {
$controller = new UserDefinedForm_Controller($this->Form());
$current = Controller::curr();
if ($current && $current->getAction() == 'finished') {
return $controller->renderWith('ReceivedFormSubmission');
}
$form = $controller->Form();
return $form;
}
}

/**
* @param null $member
* @return bool
*/
public function canCreate($member = null)
{
if (!class_exists('UserDefinedForm')) {
return false;
}
return parent::canCreate();
}

/**
* @param null $member
* @return bool
*/
public function canView($member = null)
{
if (!class_exists('UserDefinedForm')) {
return false;
}
return parent::canView();
}
}
5 changes: 5 additions & 0 deletions templates/Includes/FormBlock.ss
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div class="$CSSClasses">
<h3>$Title</h3>
<% if $Content %>$Content<% end_if %>
$BlockForm
</div>
3 changes: 3 additions & 0 deletions tests/Fixtures.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,6 @@ AccordionBlock:
Headline: "Biography sub-head"
Content: "<p>Biography overview lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean euismod bibendum laoreet. Proin gravida dolor sit amet lacus accumsan et viverra justo commodo.</p>"
Panels: =>AccordionPanel.one,=>AccordionPanel.two
FormBlock:
one:
Title: "Form"
20 changes: 20 additions & 0 deletions tests/FormBlockTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

class FormBlockTest extends SapphireTest
{
/**
* @var string
*/
protected static $fixture_file = 'dynamic-blocks/tests/Fixtures.yml';

/**
*
*/
public function testGetCMSFields()
{
$object = $this->objFromFixture('FormBlock', 'one');
$fields = $object->getCMSFields();
$this->assertInstanceOf('FieldList', $fields);
$this->assertNotNull($fields->dataFieldByName('FormID'));
}
}

0 comments on commit 4a30bf3

Please sign in to comment.