silverstripe-framework/src/Forms/GridField/GridFieldPageCount.php
GuySartorelli 5c54276b6f
ENH Make all GridField components injectable (using abstract class) (#10204)
* ENH Make all GridField components injectable.

Some components were already injectable, but all GridField components shipped in silverstripe should be injectable.
This makes it a LOT easier to make global project-specific changes to a given component.
The new AbstractGridFieldComponent also makes it easy to make similar wide-spread changes in the future.

* DOCS Encourage injection for GridField and GridFieldComponents.
2022-02-02 11:14:33 +13:00

79 lines
2.3 KiB
PHP
Executable File

<?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"
*
* Depends on {@link GridFieldPaginator} being added to the {@link GridField}.
*/
class GridFieldPageCount extends AbstractGridFieldComponent implements GridField_HTMLProvider
{
use Configurable;
/**
* @var string placement indicator for this control
*/
protected $targetFragment;
/**
* @param string $targetFragment The fragment indicating the placement of this page count
*/
public function __construct($targetFragment = 'before')
{
$this->targetFragment = $targetFragment;
}
/**
* 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;
/**
* 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 */
$paginator = $gridField->getConfig()->getComponentByType(GridFieldPaginator::class);
if (!$paginator && GridFieldPageCount::config()->uninherited('require_paginator')) {
throw new LogicException(
static::class . " relies on a GridFieldPaginator to be added " . "to the same GridField, but none are present."
);
}
return $paginator;
}
/**
* @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 [
$this->targetFragment => $forTemplate->renderWith($template)
];
}
return null;
}
}