Last active
October 11, 2018 10:33
-
-
Save jmartsch/768d9dc19293528fe9ae82b7a8ee1c42 to your computer and use it in GitHub Desktop.
ProcessWire: Output an admin template on the frontend
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
<?php namespace ProcessWire; | |
$uikit = $this->modules->get('AdminThemeUikit'); | |
$uikit->init(); | |
?> | |
<html> | |
<link rel="stylesheet" href="<?php echo $uikit->getUikitCSS() ?>"/> | |
<head> | |
</head> | |
<body> | |
<div> | |
<?php | |
$this->addHookBefore('InputfieldTable::render', 'hookBeforeRenderInputfield'); | |
function hookBeforeRenderInputfield(HookEvent $event) { | |
$inputfield = $event->object; | |
d('hook'); | |
d($inputfield); | |
} | |
function ownMarkup($pageId) | |
{ | |
$page = wire()->page; | |
$pages = wire()->pages; | |
$page = $pages->get($pageId); // get the page however you like | |
$fieldgroup = $page->fields; | |
foreach ($fieldgroup as $field) { | |
$field = $fieldgroup->getFieldContext($field); | |
$inputfield = $field->getInputfield($page); | |
$width = $inputfield->columnWidth ?: 100; | |
$fieldType = wire('fields')->$field->type->className(); | |
if ($fieldType === "FieldtypeTable") { | |
echo "Tabelle"; | |
$inputfield->addClass('uk-table'); | |
} | |
// getFieldValue() is a function/method you create that returns a string (could be markup) value for $field on $p, depending on the type of $field | |
$value = getFieldValue($page, $inputfield, $fieldType); | |
$label = "<strong>{$field->getLabel()}:</strong>"; | |
if ($fieldType === 'FieldtypePageTitle') { | |
$label = ""; | |
} | |
if ($fieldType === "FieldtypeFieldsetOpen") { | |
$label = "<h2>{$field->getLabel()}</h2>"; | |
} | |
if ($fieldType === "FieldtypeFieldsetClose") { | |
$label = ""; | |
} | |
echo "<div class='' style='width: {$width}%'>{$label}<br>$value</div>"; | |
} | |
} | |
function nl2p($string) | |
{ | |
$paragraphs = ''; | |
foreach (explode("\n", $string) as $line) { | |
if (trim($line)) { | |
$paragraphs .= '<p>' . $line . '</p>'; | |
} | |
} | |
return $paragraphs; | |
} | |
function getFieldValue($page, $inputfield, $fieldType) | |
{ | |
$exportMultipleValuesSeparator = ","; | |
$fieldName = $inputfield->name; | |
$field = $page->$fieldName; | |
$value = $field; | |
$user = wire('user'); | |
d($fieldType); | |
if ($fieldType == 'FieldtypeTable') { | |
return $value; | |
} | |
if ($fieldType === "FieldtypeImage") { | |
$value = "<img src='{$field->url}'>"; | |
return $value; | |
} | |
if ($fieldType === 'FieldtypeTextarea' && $value != "") { | |
$value = nl2p($value); | |
$value = trim(preg_replace('/\s+/', " ", strip_tags($value))); | |
} | |
if ($fieldType === 'FieldtypeOptions') { | |
$returnValue = ""; | |
foreach ($field as $option) { | |
$returnValue .= "<input type='checkbox' class='uk-checkbox' checked> $option->title"; | |
} | |
return $returnValue; | |
} | |
if ($fieldType === 'FieldtypeCheckbox') { | |
$checkboxLabel = $inputfield->getSetting("checkboxLabel$user->language"); | |
$returnValue = ""; | |
$returnValue .= "<input type='checkbox' class='uk-checkbox' checked> $checkboxLabel"; | |
return $returnValue; | |
} | |
if ($value instanceof WireArray) { | |
$values = array(); | |
foreach ($value as $v) { | |
if (is_object($v)) { | |
if ($v instanceof Page) { | |
$values[] = $v->get('title|name'); | |
} else { | |
$values[] = (string)$v; | |
} | |
} else if (is_array($v)) { | |
$values[] = (string)print_r($v, true); | |
} else { | |
$values[] = $v; | |
} | |
} | |
$value = implode(", ", $values); | |
unset($values); | |
} else { | |
$value = $value; | |
if (is_object($value)) { | |
if ($value instanceof Page) { | |
$value = $value->get('title|name'); | |
} else if ($value instanceof PageArray) { | |
$value = $value->implode("\n", "{title|name}"); | |
} | |
} else { | |
$value = (string)$value; | |
} | |
} | |
return $value; | |
} | |
ownMarkup(1261); | |
?> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment