Skip to content

Commit

Permalink
Merge branch 'master' into 1.0
Browse files Browse the repository at this point in the history
* master:
  this fix will handling controller methods from template
  FIX optional block area preview button
  NEW Allow disabling of block area preview button
  NEW access current page scope
  Bug fix to Block Filters: couldn't use Block Set model admin
  • Loading branch information
sheadawson committed Mar 20, 2016
2 parents c467563 + 82b361d commit 0ba0ef4
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 6 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ BlockManager:
#disabled_blocks: #allows you to disable specific blocks
# - ContentBlock
use_default_blocks: false # Disable/enable the default Block types (ContentBlock) (default if undeclared: true)
block_area_preview: false # Disable block area preview button in CMS (default if undeclared: true)
```
Remember to run ?flush=1 after modifying your .yml config to make sure it gets applied.
Expand Down Expand Up @@ -116,6 +117,8 @@ It's likely that your block areas may require different templates. You can achie

Each subclass of Block requires it's own template with the same name as the class. So, SlideshowBlock.php would have a SlideshowBlock.ss template. If your block requires different templates depending on the BlockArea it's in, you can create SlideshowBlock_{AreaName}.ss

The current page scope can be accessed from Block templates with `$CurrentPage`.

### Block Area Preview

To aid website admins in identifying the areas they can apply blocks to, a "Preview Block Areas for this page" button is available in the cms. This opens the frontend view of the page in a new tab with ?block_preview=1. In Block Preview mode, Block Areas in the template are highlighted and labeled.
Expand Down
7 changes: 7 additions & 0 deletions code/BlockManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ class BlockManager extends Object
**/
private static $use_default_blocks = true;

/**
* Show a block area preview button in CMS
*
* @var bool
**/
private static $block_area_preview = true;

public function __construct()
{
parent::__construct();
Expand Down
2 changes: 1 addition & 1 deletion code/controllers/BlockAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function getSearchContext()
$context = parent::getSearchContext();
$fields = $context->getFields();
$subclasses = $this->blockManager->getBlockClasses();
if (sizeof($subclasses) > 1) {
if ($fields->dataFieldByName('q[ClassName]') && sizeof($subclasses) > 1) {
$fields->dataFieldByName('q[ClassName]')->setSource($subclasses);
$fields->dataFieldByName('q[ClassName]')->setEmptyString('(any)');
} else {
Expand Down
20 changes: 17 additions & 3 deletions code/dataobjects/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,14 @@ public function getCMSFields()
$currentPage = Controller::curr()->currentPage();
$fields->addFieldToTab(
'Root.Main',
DropdownField::create('ManyMany[BlockArea]', _t('Block.BlockArea','Block Area'), $this->blockManager->getAreasForPageType($currentPage->ClassName))
$blockAreaField = DropdownField::create('ManyMany[BlockArea]', _t('Block.BlockArea','Block Area'), $this->blockManager->getAreasForPageType($currentPage->ClassName))
->setHasEmptyDefault(true)
->setRightTitle($currentPage->areasPreviewButton()),
->setEmptyString('(Select one)'),
'ClassName'
);
if (BlockManager::config()->get('block_area_preview')) {
$blockAreaField->setRightTitle($currentPage->areasPreviewButton());
}
}

$fields->removeFieldFromTab('Root', 'BlockSets');
Expand Down Expand Up @@ -218,7 +221,7 @@ public function forTemplate()
}
}

return $this->renderWith($this->ClassName);
return $this->renderWith($this->ClassName, $this->getController());
}

/**
Expand Down Expand Up @@ -427,6 +430,16 @@ public function CSSClasses($stopAtClass = 'DataObject')
return $classes;
}

/**
* Access current page scope from Block templates with $CurrentPage
*
* @return Controller
*/
public function getCurrentPage()
{
return Controller::curr();
}

/**
* @throws Exception
*
Expand All @@ -447,6 +460,7 @@ public function getController()
throw new Exception("Could not find controller class for $this->classname");
}
$this->controller = Injector::inst()->create($controllerClass, $this);
$this->controller->init();

return $this->controller;
}
Expand Down
6 changes: 4 additions & 2 deletions code/extensions/BlocksSiteTreeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ public function updateCMSFields(FieldList $fields)

if ($areas && count($areas)) {
$fields->addFieldToTab('Root', new Tab('Blocks', _t('Block.PLURALNAME')));
$fields->addFieldToTab('Root.Blocks',
LiteralField::create('PreviewLink', $this->areasPreviewButton()));
if (BlockManager::config()->get('block_area_preview')) {
$fields->addFieldToTab('Root.Blocks',
LiteralField::create('PreviewLink', $this->areasPreviewButton()));
}

// Blocks related directly to this Page
$gridConfig = GridFieldConfig_BlockManager::create(true, true, true, true)
Expand Down

0 comments on commit 0ba0ef4

Please sign in to comment.