silverstripe-framework/src/Forms/GridField/GridFieldPageCount.php

79 lines
2.3 KiB
PHP
Raw Normal View History

<?php
namespace SilverStripe\Forms\GridField;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\View\SSViewer;
use LogicException;
/**
* GridFieldPage displays a simple current page count summary.
* E.g. "View 1 - 15 of 32"
2014-08-15 08:53:05 +02:00
*
* Depends on {@link GridFieldPaginator} being added to the {@link GridField}.
*/
class GridFieldPageCount extends AbstractGridFieldComponent implements GridField_HTMLProvider
2016-11-29 00:31:16 +01:00
{
use Configurable;
2016-11-29 00:31:16 +01:00
/**
* @var string placement indicator for this control
*/
protected $targetFragment;
2016-11-29 00:31:16 +01:00
/**
* @param string $targetFragment The fragment indicating the placement of this page count
*/
public function __construct($targetFragment = 'before')
{
$this->targetFragment = $targetFragment;
}
2014-08-15 08:53:05 +02:00
2016-11-29 00:31:16 +01:00
/**
* Flag indicating whether or not this control should throw an error if a
* {@link GridFieldPaginator} is not present on the same {@link GridField}
*
* @config
* @var boolean
*/
private static $require_paginator = true;
2014-08-15 08:53:05 +02:00
2016-11-29 00:31:16 +01:00
/**
* Retrieves an instance of a GridFieldPaginator attached to the same control
* @param GridField $gridField The parent gridfield
* @return GridFieldPaginator The attached GridFieldPaginator, if found.
* @throws LogicException
*/
protected function getPaginator($gridField)
{
/** @var GridFieldPaginator $paginator */
2017-02-22 04:14:53 +01:00
$paginator = $gridField->getConfig()->getComponentByType(GridFieldPaginator::class);
2014-08-15 08:53:05 +02:00
2017-02-22 04:14:53 +01:00
if (!$paginator && GridFieldPageCount::config()->uninherited('require_paginator')) {
2016-11-29 00:31:16 +01:00
throw new LogicException(
2018-01-16 19:39:30 +01:00
static::class . " relies on a GridFieldPaginator to be added " . "to the same GridField, but none are present."
2016-11-29 00:31:16 +01:00
);
}
2014-08-15 08:53:05 +02:00
2016-11-29 00:31:16 +01:00
return $paginator;
}
2016-11-29 00:31:16 +01:00
/**
* @param GridField $gridField
* @return array
*/
public function getHTMLFragments($gridField)
{
// Retrieve paging parameters from the directing paginator component
$paginator = $this->getPaginator($gridField);
if ($paginator && ($forTemplate = $paginator->getTemplateParameters($gridField))) {
$template = SSViewer::get_templates_by_class($this, '', __CLASS__);
return [
2016-11-29 00:31:16 +01:00
$this->targetFragment => $forTemplate->renderWith($template)
];
2016-11-29 00:31:16 +01:00
}
2016-11-29 00:31:16 +01:00
return null;
}
}