Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to manually override templates in PageIndex #8371

Merged
merged 2 commits into from
Dec 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 56 additions & 7 deletions Sources/PageIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,18 +202,26 @@ class PageIndex implements \Stringable
* "url;start=offset". Default: false.
* @param bool $show_prevnext Whether the Previous and Next links should be
* shown. Default: true.
* @param array $template_overrides Array of template strings to override defaults.
* Supported keys: extra_before, previous_page, current_page, page,
* expand_pages, next_page, extra_after.
*/
public function __construct(string $base_url, int &$start, int $max_value, int $num_per_page, bool $short_format = false, bool $show_prevnext = true)
{
public function __construct(
string $base_url,
int &$start,
int $max_value,
int $num_per_page,
bool $short_format = false,
bool $show_prevnext = true,
array $template_overrides = []
) {
$this->base_url = $base_url;
$this->max_value = $max_value;
$this->num_per_page = $num_per_page;
$this->short_format = $short_format;
$this->show_prevnext = $show_prevnext;
$this->start = $start = $this->fixStart($start);

$this->extra_before = str_replace('{txt_pages}', Lang::$txt['pages'], $this->extra_before);

if (isset(Theme::$current->settings['page_index'])) {
foreach (Theme::$current->settings['page_index'] as $key => $value) {
if (property_exists($this, $key)) {
Expand All @@ -225,6 +233,26 @@ public function __construct(string $base_url, int &$start, int $max_value, int $
if (!isset(Utils::$context['current_page'])) {
Utils::$context['current_page'] = $this->start / $this->num_per_page;
}

$this->setTemplateOverrides($template_overrides);

$this->extra_before = str_replace('{txt_pages}', Lang::$txt['pages'], $this->extra_before);
}

/**
* Sets template overrides.
*
* @param array $template_overrides Array of template strings to override defaults.
* Supported keys: extra_before, previous_page, current_page, page,
* expand_pages, next_page, extra_after.
*/
public function setTemplateOverrides(array $template_overrides = []): void
{
foreach ($template_overrides as $key => $value) {
if (property_exists($this, $key)) {
$this->{$key} = $value;
}
}
}

/**
Expand Down Expand Up @@ -276,11 +304,32 @@ public function __toString(): string
/**
* Static wrapper for constructor.
*
* @param string $base_url The basic URL to be used for each link.
* @param int &$start The start position, by reference. If this is not a
* multiple of the number of items per page, it is sanitized to be so and
* the value will persist upon the function's return.
* @param int $max_value The total number of items you are paginating for.
* @param int $num_per_page The number of items to be displayed on a given
* page. $start will be forced to be a multiple of this value.
* @param bool $short_format Whether to use "url.offset" instead of
* "url;start=offset". Default: false.
* @param bool $show_prevnext Whether the Previous and Next links should be
* shown. Default: true.
* @param array $template_overrides Array of template strings to override defaults.
* Supported keys: extra_before, previous_page, current_page, page,
* expand_pages, next_page, extra_after.
* @return self An instance of this class.
*/
public static function load(string $base_url, int &$start, int $max_value, int $num_per_page, bool $short_format = false, bool $show_prevnext = true): self
{
return new self($base_url, $start, $max_value, $num_per_page, $short_format, $show_prevnext);
public static function load(
string $base_url,
int &$start,
int $max_value,
int $num_per_page,
bool $short_format = false,
bool $show_prevnext = true,
array $template_overrides = []
): self {
return new self($base_url, $start, $max_value, $num_per_page, $short_format, $show_prevnext, $template_overrides);
}

/******************
Expand Down
Loading