Skip to content

Commit

Permalink
added recent contents block widget
Browse files Browse the repository at this point in the history
see #111
  • Loading branch information
quickapps committed Nov 6, 2016
1 parent 28d675e commit b3d711f
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* Licensed under The GPL-3.0 License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @since 2.0.0
* @author Christopher Castro <[email protected]>
* @link http://www.quickappscms.org
* @license http://opensource.org/licenses/gpl-3.0.html GPL-3.0 License
*/
?>

<h2><?= $block->title; ?></h2>
<ul class="qa-recent-content">
<?php foreach ($contents as $content): ?>
<li>
<?= $this->Html->link($content->title, $content->url, ['title' => $content->description]); ?>
</li>
<?php endforeach; ?>
</ul>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* Licensed under The GPL-3.0 License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @since 2.0.0
* @author Christopher Castro <[email protected]>
* @link http://www.quickappscms.org
* @license http://opensource.org/licenses/gpl-3.0.html GPL-3.0 License
*/
?>

<?=
$this->Form->input('filter_criteria', [
'type' => 'text',
'label' => __d('contents', 'Filter criteria')
]);
?>
<em class="help-block"><?= __d('contents', 'Search criteria which determinates Contents that will be present in the list. e.g. "type:articles" if you want to list latest Articles.'); ?></em>

<?=
$this->Form->input('limit', [
'type' => 'number',
'label' => __d('taxonomy', 'Size of the list')
]);
?>
66 changes: 66 additions & 0 deletions plugins/Content/src/Widget/RecentContentWidget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* Licensed under The GPL-3.0 License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @since 2.0.0
* @author Christopher Castro <[email protected]>
* @link http://www.quickappscms.org
* @license http://opensource.org/licenses/gpl-3.0.html GPL-3.0 License
*/
namespace Content\Widget;

use Block\Model\Entity\Block;
use Block\Widget;
use Cake\ORM\TableRegistry;
use CMS\View\View;

/**
* Used to render a list of latest content publiched on the site.
*
* Aimed to be used in frontend themes.
*/
class RecentContentWidget extends Widget
{

/**
* {@inheritDoc}
*/
public function render(Block $block, View $view)
{
$contentsTable = TableRegistry::get('Content.Contents');
$query = $contentsTable->find('all', ['fieldable' => false]);

if (!empty($block->settings['filter_criteria'])) {
$query = $contentsTable->search($block->settings['filter_criteria'], $query);
}

$contents = $query
->order(['created' => 'DESC'])
->where(['Contents.status' => true])
->limit($block->settings['limit'])
->all();

return $view->element('Content.Widget/recent_content_render', compact('block', 'contents'));
}

/**
* {@inheritDoc}
*/
public function settings(Block $block, View $view)
{
return $view->element('Content.Widget/recent_content_settings', compact('block'));
}

/**
* {@inheritDoc}
*/
public function defaultSettings(Block $block)
{
return [
'filter_criteria' => '',
'limit' => 10,
];
}
}

0 comments on commit b3d711f

Please sign in to comment.