Skip to content

Commit

Permalink
Added children and hasChildren tmpl methods
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamminf committed May 11, 2016
1 parent 35426d5 commit 508157b
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
59 changes: 59 additions & 0 deletions neo/services/NeoService.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class NeoService extends BaseApplicationComponent
private $_blockRecordsById;
private $_uniqueBlockTypeAndFieldHandles;
private $_parentNeoFields;
private $_blocks = [];

public $currentSavingBlockType;

Expand Down Expand Up @@ -682,6 +683,40 @@ public function getParentNeoField(FieldModel $neoField)
return $this->_parentNeoFields[$neoField->id];
}

public function getChildBlocks(Neo_BlockModel $block)
{
$owner = $block->getOwner();
$type = $block->getType();
$field = craft()->fields->getFieldById($type->fieldId);

$blocks = $this->_getBlocksByOwnerAndField($owner, $field);

$childLevel = ((int) $block->level) + 1;
$children = array();
$foundBlock = false;

foreach($blocks as $testBlock)
{
if($foundBlock)
{
if($testBlock->level == $childLevel)
{
$children[] = $testBlock;
}
else if($testBlock->level < $childLevel)
{
break;
}
}
if($testBlock->id == $block->id)
{
$foundBlock = true;
}
}

return $children;
}

public function requirePlugin($plugin)
{
if(!craft()->plugins->getPlugin($plugin))
Expand Down Expand Up @@ -885,4 +920,28 @@ private function _applyFieldTranslationSetting($owner, $field, $blocks)
}
}
}

private function _getBlocksByOwnerAndField(BaseElementModel $owner, FieldModel $field)
{
$key = $owner->id . ':' . $field->id;

if(!isset($this->_blocks[$key]))
{
$result = Neo_BlockRecord::model()->findAllByAttributes([
'ownerId' => $owner->id,
'fieldId' => $field->id,
]);

$models = Neo_BlockModel::populateModels($result);

usort($models, function(Neo_BlockModel $a, Neo_BlockModel $b)
{
return $a->sortOrder - $b->sortOrder;
});

$this->_blocks[$key] = $models;
}

return $this->_blocks[$key];
}
}
15 changes: 15 additions & 0 deletions neo/variables/NeoVariable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
namespace Craft;

class NeoVariable
{
public function children(Neo_BlockModel $block)
{
return craft()->neo->getChildBlocks($block);
}

public function hasChildren(Neo_BlockModel $block)
{
return !empty($this->children($block));
}
}

0 comments on commit 508157b

Please sign in to comment.