-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from sachbearbeiter/main
initial v4 commit (various issues)
- Loading branch information
Showing
14 changed files
with
686 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,83 @@ | ||
<?php | ||
use Kirby\Uuid\Uuids; | ||
/** | ||
* Good to know: | ||
* - stored Kirby links always have 'id' values, Custom links never have that | ||
* - stored Kirby links may have 'uuid_uri' values, Custom links never have that | ||
* - The uuid_uri can identify a page even after the page slug changes, | ||
* so it is the preferred way of identification. | ||
* - If uuid_uri value is not available for any reason, the plugin uses the 'id' value | ||
* to find the desired page | ||
* - The 'uuid' value used elsewhere in the plugin is not the same as 'uuid_uri' value. | ||
* The 'uuid' value is kept for compatibility reasons. | ||
*/ | ||
|
||
return function () { | ||
return [ | ||
[ | ||
'pattern' => 'listings/(:all)', | ||
'method' => 'GET', | ||
'action' => function ($all) { | ||
$content = []; | ||
$breadcrumbs = []; | ||
$getData = $all !== 'site' ? true : false; | ||
$data = $getData ? page($all) : site(); | ||
return [ | ||
[ | ||
'pattern' => 'listings/(:alpha)/(:all)', | ||
'method' => 'GET', | ||
'action' => function ($language_code, $path) { | ||
$content = []; | ||
$breadcrumbs = []; | ||
$getData = $path !== 'site' ? true : false; | ||
$data = $getData ? page($path) : site(); | ||
|
||
if ($data->hasChildren()) { | ||
if ($getData) { | ||
foreach ($data->children()->first()->parents()->flip() as $parent) { | ||
array_push($breadcrumbs,[ | ||
'id' => $parent->id(), | ||
'title' => $parent->title()->value() | ||
]); | ||
} | ||
} | ||
$multilang = $this->kirby()->languages()->isNotEmpty(); | ||
if ($multilang) { | ||
if ($language_code=='default') { | ||
// default language, use item title and url without translation | ||
$multilang=false; | ||
} | ||
elseif (!$this->kirby()->languages()->find($language_code)) { | ||
// invalid language, do nothing, just return an empty array | ||
$data=null; | ||
} | ||
} | ||
if (($data != null) && $data->hasChildren()) { | ||
if ($getData) { | ||
foreach ($data->children()->first()->parents()->flip() as $parent) { | ||
if ($multilang && ($parent->content($language_code)->title()->value() != null)) { | ||
$title=$parent->content($language_code)->title()->value(); | ||
} | ||
else { | ||
$title = $parent->title()->value(); | ||
} | ||
array_push($breadcrumbs,[ | ||
'id' => $parent->id(), | ||
'title' => $title, | ||
]); | ||
} | ||
} | ||
|
||
foreach ($data->children() as $item) { | ||
array_push($content, [ | ||
'id' => $item->id(), | ||
'url' => $item->url(), | ||
'text' => $item->title()->value(), | ||
'count' => $item->index()->count(), | ||
'children' => [] | ||
]); | ||
} | ||
} | ||
return [ | ||
'content' => $content, | ||
'breadcrumbs' => $breadcrumbs, | ||
]; | ||
foreach ($data->children() as $item) { | ||
$title = $item->title()->value(); | ||
if ($multilang && ($item->content($language_code)->title()->value() != null)) { | ||
$title=$item->content($language_code)->title()->value(); | ||
} | ||
], | ||
]; | ||
array_push($content, [ | ||
// Values for page identification | ||
'type' => 'page', | ||
'uuid_uri' => Uuids::enabled() ? $item->uuid()->toString() : '', | ||
'id' => $item->id(), | ||
// Language-specific values that may change due to editing | ||
$language_code . '_page_url' => $multilang ? $item->url($language_code) : $item->url(), | ||
$language_code . '_page_title' => $title, | ||
// Default values of page links that prop.php also provides | ||
$language_code . '_link_text' => '', | ||
$language_code . '_link_title' => '', | ||
'popup' => false, | ||
'children' => [], | ||
// Temporary helper values that will not be saved | ||
'count' => $item->index()->count(), | ||
]); | ||
} | ||
} | ||
return [ | ||
'content' => $content, | ||
'breadcrumbs' => $breadcrumbs, | ||
]; | ||
} | ||
], | ||
]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
use Kirby\Data\Yaml; | ||
use Kirby\Uuid\Uuids; | ||
|
||
return function ($items) { | ||
// Use anonymous recursive function to process child items | ||
$prepare_item = function($item) use (&$prepare_item) { | ||
if (!is_array($item) || !isset($item['type'])) { | ||
throw new Exception('Unexpected data found in the navigation field while saving'); | ||
} | ||
if ($item['type'] === 'page') { | ||
// The primary way of page identification is by 'uuid_uri', | ||
// if not available, then by 'id'. | ||
// Although page url and page title are saved here, | ||
// these values will be refreshed when the field data is loaded, | ||
// | ||
|
||
// Do not store 'count' value (coming from api.php) | ||
unset($item['count']); | ||
} | ||
// Remove the 'error' value, it will be set again when loading field values | ||
unset($item['error']); | ||
// prepare child items, if any | ||
if (!empty($item['children'])) { | ||
foreach (array_keys($item['children']) as $key) { | ||
$item['children'][$key]=$prepare_item($item['children'][$key]); | ||
} | ||
} | ||
return $item; | ||
}; | ||
|
||
// Remove any item with type 'save', | ||
// indicating field data that was just submitted | ||
// This 'save' item is always added by props.php | ||
// This 'save' item is always removed by the save.php | ||
// This 'save' trick is needed, because the props.php is not only | ||
// executed when a field is loaded, but also when it is saved, and | ||
// otherwise it would not be possible to distinguish between the two. | ||
if (is_array($items) && $items) { | ||
foreach (array_keys($items) as $key) { | ||
if (isset($items[$key]['type']) && ($items[$key]['type']==='save')) { | ||
unset($items[$key]); | ||
} | ||
} | ||
if ($items) { | ||
// When the 'save' item is deleted, there may be a gap in keys | ||
// Make sure that array keys have no gaps, to avoid JS problems | ||
$items=array_values($items); | ||
} | ||
} | ||
|
||
if (is_array($items) && $items) { | ||
foreach (array_keys($items) as $key) { | ||
$items[$key]=$prepare_item($items[$key]); | ||
} | ||
} | ||
|
||
// Get the 'multilang' option of the field blueprint | ||
$blueprint_field=$this->model()->blueprint()->field($this->name()); | ||
if (!empty($blueprint_field) && !empty($blueprint_field['multilang'])) { | ||
// If 'multilang' is set in the blueprint, then data is stored | ||
// in external yaml file, so that it can be shared between languages | ||
$filepathTMP = tempnam(sys_get_temp_dir(), 'navigation'); | ||
if (file_put_contents($filepathTMP, Yaml::encode($items))) { | ||
if (rename($filepathTMP, $this->model()->root() . '/kirby-navigation---' . $this->name() . '.yml')) { | ||
// The data was successfully saved to the external file, | ||
// so change the field data to a simple flag that will tell | ||
// the load function to load the external file. | ||
$items=['multilang' => TRUE]; | ||
} | ||
else { | ||
} | ||
} | ||
else { | ||
} | ||
@unlink($filepathTMP); | ||
} | ||
return $items; | ||
}; |
Oops, something went wrong.