Skip to content
Snippets Groups Projects
Commit 207d6352 authored by Pierre Dureau's avatar Pierre Dureau
Browse files

Issue #3345071 by pdureau: Fix links setting type

parent cbb24f3a
Branches
Tags
2 merge requests!32Add inital media modul,!18Issue #3345071 by pdureau: Fix links setting type
......@@ -95,7 +95,7 @@ abstract class ComplexSettingTypeBase extends PatternSettingTypeBase implements
$this->provider = $instance;
return $instance->getData($value['configuration'][$provider_id]['config'] ?? []);
}
return [];
return $value;
}
/**
......
......@@ -2,6 +2,8 @@
namespace Drupal\ui_patterns_settings\Plugin\UiPatterns\SettingType;
use Drupal\Core\Template\Attribute;
use Drupal\Core\Url;
use Drupal\ui_patterns_settings\Plugin\ComplexSettingTypeBase;
/**
......@@ -51,15 +53,7 @@ class LinksSettingType extends ComplexSettingTypeBase {
$item["url"]->setOptions($item["link"]["#options"]);
unset($item["link"]);
}
if (array_key_exists("url", $item) && ($item["url"] instanceof Url)) {
// Examples: menu.html.twig, links.html.twig.
$url = $item["url"];
$item["url"] = $url->toString();
$options = $url->getOptions();
if (isset($options["attributes"])) {
$item["link_attributes"] = new Attribute($options["attributes"]);
}
}
$item = self::normalizeUrl($item);
if (array_key_exists("below", $item)) {
$item["below"] = self::normalize($item["below"]);
}
......@@ -69,67 +63,32 @@ class LinksSettingType extends ComplexSettingTypeBase {
}
/**
* Convert pager to menu.
*
* Convert pager data structure to menu data structure. Useful for
* pager.html.twig presenter template.
* Normaize URL in an item.
*
* @param array $items
* The pager items to convert.
* @param int $current
* The current page.
*
* @return array
* Useful for: menu.html.twig, links.html.twig.
*/
public static function convertPagerToMenu(array $pager, int $current): array {
$items = [];
if (isset($pager["first"])) {
$items[] = $pager["first"];
private static function normalizeUrl(array $item): array {
if (!array_key_exists("url", $item)) {
return $item;
}
if (isset($pager["previous"])) {
$items[] = $pager["previous"];
$url = $item["url"];
if (!($url instanceof Url)) {
return $item;
}
if (isset($pager["pages"])) {
foreach ($pager["pages"] as $index => $item) {
$item["text"] = $index;
if ($index == $current) {
unset($item["href"]);
}
$items[] = $item;
}
}
if (isset($pager["next"])) {
$items[] = $pager["next"];
if ($url->isRouted() && ($url->getRouteName() === '<nolink>')) {
unset($item["url"]);
}
if (isset($pager["last"])) {
$items[] = $pager["last"];
elseif ($url->isRouted() && ($url->getRouteName() === '<button>')) {
unset($item["url"]);
}
return $items;
}
/**
* Convert mini pager to menu.
*
* Convert views mini pager data structure to menu data structure. Useful for
* views-mini-pager.html.twig presenter template.
*
* @param array $items
* The pager items to convert.
*
* @return array
*/
public static function convertMiniPagerToMenu(array $pager): array {
$items = [];
if ($pager["previous"]) {
$items[] = $pager["previous"];
else {
$item["url"] = $url->toString();
}
$items[] = [
"text" => $pager["current"],
];
if ($pager["next"]) {
$items[] = $pager["next"];
$options = $url->getOptions();
if (isset($options["attributes"])) {
$item["link_attributes"] = new Attribute($options["attributes"]);
}
return $items;
return $item;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment