Skip to content

Instantly share code, notes, and snippets.

@byron-roots
Created July 8, 2020 09:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save byron-roots/b30be2a1e1de14a7198257fbad4df0b6 to your computer and use it in GitHub Desktop.
Save byron-roots/b30be2a1e1de14a7198257fbad4df0b6 to your computer and use it in GitHub Desktop.
Perch Field type to chose a page in a selectize
<!-- In admin show all pages from the parent page venue (Uses path not name) -->
<perch:content id="pages" type="page_list" label="Pages" parent="venues" suppress/>
<!-- In admin show all pages and limit to 1 selection -->
<perch:content id="pages" type="page_list" label="Pages" max="1" suppress/>
<!-- Output the page path in template -->
<perch:content id="pages" type="page_list" output="pagePath" />
<!-- Output the page title in template -->
<perch:content id="pages" type="page_list" output="pageTitle" />
<?php
/**
* Class PerchFieldType_Page_List
*
* A field type for allowing users to select multiple pages that can be filtered by parent pages
*
* Examples:
* <perch:content id="pages" type="page_list" label="Pages" parent="venues" />
*
* @author Byron Fitzgerald <byron@rootstudio.co.uk>
* @version 1.0.0
*/
class PerchFieldType_Page_List extends PerchAPI_FieldType {
/**
* Render the input in the admin interface
*
* @param array $details
*
* @return string
*/
public function render_inputs($details = []) {
$PerchPages = new PerchContent_Pages();
$pages = [];
if ($parent = $this->Tag->parent()) {
PerchUtil::mark(json_encode($PerchPages->find_by_path('/' . $parent)));
if ($parent = $PerchPages->find_by_path('/' . $parent)) {
$pages = $PerchPages->get_by_parent($parent->id());
}
} else {
$pages = $PerchPages->all();
}
$opts = [];
if (PerchUtil::count($pages)) {
foreach ($pages as $Item) {
$opts[] = [
'label' => $Item->pageTitle(),
'value' => $Item->id()
];
}
}
return $this->render_select($details, $opts);
}
private function render_select($details, $opts) {
$attributes = $this->Tag->get_data_attribute_string();
$attributes .= ' data-display-as="categories"';
if ($this->Tag->max()) {
$attributes .= ' data-max="' . (int) $this->Tag->max() . '"';
}
$attributes = trim($attributes);
return $this->Form->select($this->Tag->input_id(), $opts, $this->Form->get($details, $this->Tag->id(), $this->Tag->default(), $this->Tag->post_prefix()), 'input-simple categories ' . $this->Tag->size('xxl'), true, $attributes) . $this->render_field_annotations();
}
public function get_raw($post = false, $Item = false) {
if ($post === false) {
$post = PerchRequest::post();
}
$id = $this->Tag->id();
if (isset($post[$id])) {
$this->raw_item = $post[$id];
if(is_array($this->raw_item)) {
$this->raw_item = implode(', ', $this->raw_item);
}
PerchUtil::debug($this->raw_item);
return $this->raw_item;
}
}
public function get_search_text($raw = false) {
if ($raw === false) $raw = $this->get_raw();
return null;
if (is_array($raw) && count($raw)) {
$out = [];
$PerchPages = new PerchContent_Pages();
if ($parent = $this->Tag->parent()) {
PerchUtil::mark(json_encode($PerchPages->find_by_path('/' . $parent)));
if ($parent = $PerchPages->find_by_path('/' . $parent)) {
$pages = $PerchPages->get_by_parent($parent->id());
}
} else {
$pages = $PerchPages->all();
}
foreach ($raw as $itemID) {
$Cat = $Categories->find((int) $itemID);
$out[] = $Cat->catTitle();
}
return implode(', ', $out);
}
return $raw;
}
public function render_admin_listing($raw = false) {
return PerchUtil::html($this->get_search_text($raw));
}
public function get_index($raw = false) {
PerchUtil::mark(json_encode($raw));
if ($raw === false) $raw = $this->get_raw();
$id = $this->Tag->id();
PerchUtil::mark($id);
$out = [];
if (is_array($raw)) {
$Blog = new PerchBlog_Posts();
$out = $Blog->find_by_slug($this->Tag->post(), $raw, $id);
}
return $out;
}
public function get_processed($raw = false) {
$pages = new PerchContent_Pages();
if ($this->Tag->output) {
$item = $pages->find($raw[0]);
if ($item) {
PerchUtil::mark(json_encode($item));
$field = $this->Tag->output;
return $item->{$field}();
} else {
return '';
}
}
return '';
}
public function get_content_summary($details = [], $Template) {
$pages = new PerchContent_Pages();
if ($this->Tag->output) {
$item = $pages->find($details);
if ($item) {
PerchUtil::mark(json_encode($item));
$field = $this->Tag->output;
return $item->{$field}();
} else {
return '';
}
}
return '';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment