From 0932aea586d1f6be8358c26d124fab2989d9e227 Mon Sep 17 00:00:00 2001 From: Daniel Berthereau Date: Mon, 17 Sep 2018 00:00:00 +0200 Subject: [PATCH 1/2] Added a block to display the list of sites. --- application/config/module.config.php | 1 + .../src/Site/BlockLayout/ListOfSites.php | 64 +++++++++++++++++++ .../common/block-layout/list-of-sites.phtml | 10 +++ 3 files changed, 75 insertions(+) create mode 100644 application/src/Site/BlockLayout/ListOfSites.php create mode 100644 application/view/common/block-layout/list-of-sites.phtml diff --git a/application/config/module.config.php b/application/config/module.config.php index 7260164212..180903628d 100644 --- a/application/config/module.config.php +++ b/application/config/module.config.php @@ -482,6 +482,7 @@ 'media' => Site\BlockLayout\Media::class, 'browsePreview' => Site\BlockLayout\BrowsePreview::class, 'itemShowCase' => Site\BlockLayout\ItemShowcase::class, + 'listOfSites' => Site\BlockLayout\ListOfSites::class, 'tableOfContents' => Site\BlockLayout\TableOfContents::class, 'lineBreak' => Site\BlockLayout\LineBreak::class, 'itemWithMetadata' => Site\BlockLayout\ItemWithMetadata::class, diff --git a/application/src/Site/BlockLayout/ListOfSites.php b/application/src/Site/BlockLayout/ListOfSites.php new file mode 100644 index 0000000000..e067b9a427 --- /dev/null +++ b/application/src/Site/BlockLayout/ListOfSites.php @@ -0,0 +1,64 @@ + null, + ]; + + public function getLabel() + { + return 'List of sites'; // @translate + } + + public function form(PhpRenderer $view, SiteRepresentation $site, + SitePageRepresentation $page = null, SitePageBlockRepresentation $block = null + ) { + $data = $block ? $block->data() + $this->defaults : $this->defaults; + + $form = new Form(); + $form->add([ + 'name' => 'o:block[__blockIndex__][o:data][limit]', + 'type' => Element\Number::class, + 'options' => [ + 'label' => 'Max number of sites', // @translate + 'info' => 'An empty value means no limit.', // @translate + ], + 'attributes' => [ + 'id' => 'list-of-sites-limit', + 'placeholder' => '100', // @translate + ], + ]); + + $form->setData([ + 'o:block[__blockIndex__][o:data][limit]' => $data['limit'], + ]); + + return $view->formCollection($form); + } + + public function render(PhpRenderer $view, SitePageBlockRepresentation $block) + { + $limit = $block->dataValue('limit', $this->defaults['limit']); + + $data = []; + if ($limit) { + $data['limit'] = $limit; + } + + $response = $view->api()->search('sites', $data); + $sites = $response->getContent(); + + return $view->partial('common/block-layout/list-of-sites', [ + 'sites' => $sites, + ]); + } +} diff --git a/application/view/common/block-layout/list-of-sites.phtml b/application/view/common/block-layout/list-of-sites.phtml new file mode 100644 index 0000000000..ff78a8edb4 --- /dev/null +++ b/application/view/common/block-layout/list-of-sites.phtml @@ -0,0 +1,10 @@ +plugin('hyperlink'); +?> +
+ +
From 2da77b22f42c4c2944abeaeb25e0b314693d7725 Mon Sep 17 00:00:00 2001 From: Daniel Berthereau Date: Mon, 17 Sep 2018 00:00:00 +0200 Subject: [PATCH 2/2] Added pagination to the list of sites. --- .../src/Site/BlockLayout/ListOfSites.php | 26 ++++++++++++++++++- .../common/block-layout/list-of-sites.phtml | 3 +++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/application/src/Site/BlockLayout/ListOfSites.php b/application/src/Site/BlockLayout/ListOfSites.php index e067b9a427..06d8524986 100644 --- a/application/src/Site/BlockLayout/ListOfSites.php +++ b/application/src/Site/BlockLayout/ListOfSites.php @@ -12,6 +12,7 @@ class ListOfSites extends AbstractBlockLayout { protected $defaults = [ 'limit' => null, + 'pagination' => false, ]; public function getLabel() @@ -37,9 +38,20 @@ public function form(PhpRenderer $view, SiteRepresentation $site, 'placeholder' => '100', // @translate ], ]); + $form->add([ + 'name' => 'o:block[__blockIndex__][o:data][pagination]', + 'type' => Element\Checkbox::class, + 'options' => [ + 'label' => 'Add pagination in case of a limit', // @translate + ], + 'attributes' => [ + 'id' => 'list-of-sites-pagination', + ], + ]); $form->setData([ 'o:block[__blockIndex__][o:data][limit]' => $data['limit'], + 'o:block[__blockIndex__][o:data][pagination]' => $data['pagination'], ]); return $view->formCollection($form); @@ -48,17 +60,29 @@ public function form(PhpRenderer $view, SiteRepresentation $site, public function render(PhpRenderer $view, SitePageBlockRepresentation $block) { $limit = $block->dataValue('limit', $this->defaults['limit']); + $pagination = $limit && $block->dataValue('pagination', $this->defaults['pagination']); $data = []; - if ($limit) { + if ($pagination) { + $currentPage = $view->params()->fromQuery('page', 1); + $data['page'] = $currentPage; + $data['per_page'] = $limit; + } elseif ($limit) { $data['limit'] = $limit; } $response = $view->api()->search('sites', $data); + + if ($pagination) { + $totalCount = $response->getTotalResults(); + $view->pagination(null, $totalCount, $currentPage, $limit); + } + $sites = $response->getContent(); return $view->partial('common/block-layout/list-of-sites', [ 'sites' => $sites, + 'pagination' => $pagination, ]); } } diff --git a/application/view/common/block-layout/list-of-sites.phtml b/application/view/common/block-layout/list-of-sites.phtml index ff78a8edb4..f838d91857 100644 --- a/application/view/common/block-layout/list-of-sites.phtml +++ b/application/view/common/block-layout/list-of-sites.phtml @@ -2,6 +2,9 @@ $hyperlink = $this->plugin('hyperlink'); ?>
+ + pagination(); ?> +
  • title(), $site->siteUrl()); ?>