Skip to content

Instantly share code, notes, and snippets.

@flokosiol
Last active July 19, 2016 12:31
Show Gist options
  • Save flokosiol/0de6a275471250774c5ddcaa6e411587 to your computer and use it in GitHub Desktop.
Save flokosiol/0de6a275471250774c5ddcaa6e411587 to your computer and use it in GitHub Desktop.
<?php
/**
* Field plugin Subpagelist
*
* @package Kirby CMS
* @author Flo Kosiol <git@flokosiol.de>
* @link http://flokosiol.de
* @version customized version for https://github.com/flokosiol/kirby-subpagelist/issues/6
*/
class SubpagelistField extends BaseField {
const LANG_DIR = 'languages';
/**
* Assets
*/
public static $assets = array(
'css' => array(
'subpagelist.css',
),
);
/**
* Constructor
*/
public function __construct() {
$baseDir = __DIR__ . DS . self::LANG_DIR . DS;
$lang = panel()->language();
if (file_exists($baseDir . $lang . '.php')) {
require $baseDir . $lang . '.php';
}
else {
require $baseDir . 'en.php';
}
}
/**
* Set field property and default value if required
*
* @param string $option
* @param mixed $value
*/
public function __set($option, $value) {
/* Set given value */
$this->$option = $value;
/* Validation */
switch($option) {
case 'flip':
if(!is_bool($value))
$this->flip = false;
break;
}
}
/**
* Generate label markup
*
* @return string
*/
public function label() {
$label = parent::label();
$label->addClass('hgroup-title');
return $label;
}
/**
* Generate field content markup
*
* @return string
*/
public function content() {
$wrapper = new Brick('div');
$wrapper->addClass('subpagelist');
$children = $this->subpages();
// add pagination to the subpages
$limit = ($this->limit()) ? $this->limit() : 1000;
$children = $children->paginate($limit, array('page' => get('page')));
// use custom template instead of existing snippet
$wrapper->html(tpl::load(__DIR__ . DS . 'template.php', array(
'title' => l('pages.show.subpages.title'),
'page' => $this->page(),
'subpages' => $children,
'addbutton' => !api::maxPages($this, $this->subpages()->max()),
'pagination' => $children->pagination(),
)));
return $wrapper;
}
/**
* Get subpages
*
* @return object
*/
public function subpages() {
$field = &$this;
$subpages = $this->page()->children();
if (isset($this->flip) && $this->flip == TRUE) {
$subpages = $subpages->flip();
}
return $subpages;
}
}
<?php if ($subpages()->count() > 0): ?>
<?php
# copy and paste the sidebar subpages snippet from here: panel/app/snippets/pages/sidebar/subpages.php
# inside this code replace the snippet for a single subpage (new Snippet('pages/sidebar/subpage', array('subpage' => $subpage)))
# with the content of this file: panel/app/snippets/pages/sidebar/subpage.php
# adjust the template as you like
?>
<?php else: ?>
<div class="input input-with-items">
<div class="item subpagelist-item-empty">
<?php echo l::get('subpagelist.empty') ?>
</div>
</div>
<?php endif ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment