Use PaginatorHelper in Cells CakePhp 3 - cakephp

Hi I'm trying to use Paginator within a Cell the Paginator works rigth but the PaginatorHelper in the Template of the cell just doesn't work.
I'm using CakePhp 3.5
Here is my code
src/View/Cell/FinderCell.php
namespace App\View\Cell;
use Cake\View\Cell;
use Cake\Datasource\Paginator;
class FinderCell extends Cell {
public function display() {
$this->loadModel('Pokemon');
$paginador = new Paginator();
$pokemons = $paginador->paginate(
$this->Pokemon->find()
);
$this->set(compact('pokemons'));
}
}
src/Template/Cell/Finder/display.ctp
<?php
foreach ($pokemons as $pokemon) :
?>
<div class="tipo form large-2 medium-2 columns content">
<?php echo $this->Html->image($pokemon->pokemon_image) ?>
</div>
<?php endforeach; ?>
<div class="paginator">
<ul class="pagination">
<?= $this->Paginator->prev('<< Anterior') ?>
<?= $this->Paginator->numbers() ?>
<?= $this->Paginator->next('Siguiente >>') ?>
</ul>
<p><?= $this->Paginator->counter() ?></p>
</div>
I get this

If you want to use the helper, then you need to populate either the request object with the pagination parameters, which is what the paginator component would normally do for you:
// ...
$this->request = $this->request->withParam(
'paging',
$paginator->getPagingParams() + (array)$this->request->getParam('paging')
);
$this->set(compact('pokemons'));
or the paginator helper, which in turn sets the parameters on the request object:
// ...
$pagingParams = $paginator->getPagingParams();
$this->set(compact('pokemons', 'pagingParams'));
$this->Paginator->options(['paging' => $pagingParams]);
See also
API > \Cake\Datasource\PaginatorInterface::getPagingParams()
API > \Cake\View\Helper\PaginatorHelper::options()

Related

How to show only 2 post from post in ACF relationship

After creating customfield from ACF. I select multiple articles. How can I show only the last 2 posts? Thank!
Show only 2 post from relationship ACF
I would use the post_object field. With this field, you can select which objects you would like to display, when you configure it to "multiple values".
<?php
$featured_posts = get_field('featured_posts');
if( $featured_posts ) { ?>
<ul>
<?php foreach( $featured_posts as $post ) {
setup_postdata($post); ?>
<li>
<?php the_title(); ?>
</li>
<?php } ?>
</ul>
<?php
wp_reset_postdata(); ?>
<?php } ?>

Manage pagination with cell in cakephp 4

With Cakephp 4 I would like to display some data. As there is too much data I would like to use pagination only I don't want to reload the whole page, just the part that displays this data. So I created a cell like this:
fileCell.php
public function display() {
$results = $paginator->paginate(
$query,
$this->request->getQueryParams(),
[
'limit' => 20
]
);
}
$paging = $paginator->getPagingParams() + (array)$this->request->getParam('paging');
$this->request = $this->request->withParam('paging', $paging);
$this->set('res', $results);
and I display the pagination in my view like this:
<div class="instances index content">
<div class="paginator">
<ul class="pagination">
<?= $this->Paginator->first('<< ' . __('first')) ?>
<?= $this->Paginator->prev('< ' . __('previous')) ?>
<?= $this->Paginator->numbers() ?>
<?= $this->Paginator->next(__('next') . ' >') ?>
<?= $this->Paginator->last(__('last') . ' >>') ?>
</ul>
<p><?= $this->Paginator->counter(__('Page {{page}} of {{pages}}, showing {{current}} record(s) out of {{count}} total')) ?></p>
</div>
</div>
Except that by doing so I reload the whole page while I would like to reload only the part concerning the cell, can someone help me?
I am just a beginner at Cake, but I wonder if a finder function in your Controller could take a page parameter?
I don't have an example, but I will soon be doing something similar.
I made a standard ajax call, I create function in my controller. I create view associate corresponding to my element.
In my main view (where I will place my element), I add div :
<div id="test">
</div>
And, in my ajax response I add :
$('#test').html(response);
After that, I manage the pagination of my element normally with cakephp pagination.

View inside a view using CakePHP 3

Is it possible to display a view inside another view?
I have the following code:
<?php if ($result->type === 'brochure') : ?>
<div>
// massive template block
</div>
<?php elseif ($result->type === 'library') : ?>
<div>
// massive template block different from above
</div>
<?php else : ?>
<div>
// massive template block different from both above
</div>
<?php endif; ?>
I want to replace those with a content block so to speak. I had a look at view blocks but I'm either using it wrong or it doesn't do what I want it to do.
Is this possible in CakePHP 3?
you can use elements for that.
first you should create elements in src/Template/Element directory with .ctp format like this
// in brochure.ctp file in src/Template/Element
<div>
// your massive template block
</div>
then you can call elements like this :
<?php if ($result->type === 'brochure') : ?>
<?= $this->element("brochure") ?>
<?php elseif ($result->type === 'library') : ?>
<?= $this->element("library") ?>
<?php else : ?>
<?= $this->element("default") ?>
<?php endif; ?>

Cakephp working whit Cell

Hi I have a trouble whit Cell in my application I follow the tutorial but did not work for me
here is my code https://github.com/boguda/okpionir.git
I bake cell for News and add display function
I want to use this cell on my static page src/Template/Pages/pocetna.ctp
but it doesn't work
just change
<?php $cell = $this->cell('News'); ?>
<?= $cell ?>
to
<?= $this->cell('News::display'); ?>
Call Cell has to refer to specific method name
EDIT:
Also you have an error in your src/View/Cell/NewsCell
public function display()
{
$this->loadModel('News');
----> $top_news= $this->News->find()
->select('title')
->order(['created'=>'DESC'])
->limit(3)
->toArray();
$this->set('top_news' => $news); <-----
}
should be:
$this->set('top_news', $top_news);
or
$this->set(compact('top_news'));
And your view Template/Cell/News/display.ctp might look like this
<ul class="list-group">
<?php foreach ($top_news as $news): ?>
<li><?= h($news['title']); ?></li>
<?php endforeach; ?>
<li class="list-group-item">Second item</li>
<li class="list-group-item">Third item</li>
</ul>

Is it possible to get a different header.php on language switch with qtranslate (Wordpress)?

the problem is that on home page I have a lot of code which is added through header.php and it is not amendable through admin panel. What I want to do is to switch header files on language switch. Whenever a user press on the language icon, lets say on the English language icon the header should also switch from <?php get_header('mylanguage') ?> to <?php get_header('english') ?> or etc. Is it possible to do like this?
Regards,
In your page template instead of calling header through:
get_header();
use this to enable header switching when changing languages;
<?php if (qtrans_getLanguage() == 'de'): ?>
<?php include(TEMPLATEPATH.'/header-home-de.php' ); ?>
<?php else : ?>
<?php include(TEMPLATEPATH.'/header-home-en.php' ); ?>
<?php endif; ?>
In this example there are 2 headers depending on selected language.
Surely, it is possible in qTranslate. Even I've done qTranslate Swicher in bootstrap navigation.
<?php } if (qtranxf_getLanguage() == 'en') { ?>
English
<ul class="dropdown-menu">
<?php echo qtranxf_generateLanguageSelectCode('code'); ?>
</ul>
<?php } elseif (qtranxf_getLanguage() == 'fr') { ?>
Français
<ul class="dropdown-menu">
<?php echo qtranxf_generateLanguageSelectCode('code'); ?>
</ul>
<?php } ?>

Resources