praiadeseselle/wire/modules/Inputfield/InputfieldMarkup.module

185 lines
4.8 KiB
Text
Raw Normal View History

2022-03-08 15:55:41 +01:00
<?php namespace ProcessWire;
/**
* Intended just for outputting markup as help or commentary among other Inputfields
*
2022-11-05 18:32:48 +01:00
* ProcessWire 3.x, Copyright 2021 by Ryan Cramer
* https://processwire.com
*
2022-03-08 15:55:41 +01:00
* @property callable|string|null $markupFunction
* @property array $textformatters
* @property string $markupText
*
*/
class InputfieldMarkup extends InputfieldWrapper {
2022-11-05 18:32:48 +01:00
/**
* Get module info
*
* @return array
*
*/
2022-03-08 15:55:41 +01:00
public static function getModuleInfo() {
return array(
'title' => __('Markup', __FILE__),
'summary' => __('Contains any other markup and optionally child Inputfields', __FILE__),
'version' => 102,
'permanent' => true,
2022-11-05 18:32:48 +01:00
);
2022-03-08 15:55:41 +01:00
}
2022-11-05 18:32:48 +01:00
/**
* Whether render() has been called from renderValue()
*
* @var bool
*
*/
2022-03-08 15:55:41 +01:00
protected $renderValueMode = false;
2022-11-05 18:32:48 +01:00
/**
* Init
*
*/
2022-03-08 15:55:41 +01:00
public function init() {
$this->set('markupText', '');
$this->set('markupFunction', null); // closure or name of function that returns markup, receives $this as arg0.
$this->set('textformatters', array());
$this->skipLabel = Inputfield::skipLabelBlank;
parent::init();
2022-11-05 18:32:48 +01:00
}
/**
* Render ready
*
* @param Inputfield|null $parent
* @param bool $renderValueMode
* @return bool
*
*/
2022-03-08 15:55:41 +01:00
public function renderReady(Inputfield $parent = null, $renderValueMode = false) {
$label = $this->getSetting('label');
if(!strlen($label) && $this->skipLabel == Inputfield::skipLabelBlank) {
$this->addClass('InputfieldHeaderHidden');
}
return parent::renderReady($parent, $renderValueMode);
}
2022-11-05 18:32:48 +01:00
/**
* Render
*
* @return string
*
*/
2022-03-08 15:55:41 +01:00
public function ___render() {
2022-11-05 18:32:48 +01:00
$modules = $this->wire()->modules;
2022-03-08 15:55:41 +01:00
$out = '';
2022-11-05 18:32:48 +01:00
$value = (string) $this->attr('value');
2022-03-08 15:55:41 +01:00
if(strlen($value)) {
$out .= "\n" . $value;
}
$markupFunction = $this->getSetting('markupFunction');
$markupText = $this->getSetting('markupText');
$textformatters = $this->getSetting('textformatters');
$description = $this->getSetting('description');
if($markupFunction !== null & is_callable($markupFunction)) {
$out .= "\n" . call_user_func($markupFunction, $this);
}
2022-11-05 18:32:48 +01:00
if(is_string($markupText) && strlen($markupText)) {
2022-03-08 15:55:41 +01:00
$out .= "\n" . $markupText;
}
$out = trim($out);
if(wireCount($textformatters)) {
foreach($textformatters as $className) {
2022-11-05 18:32:48 +01:00
/** @var Textformatter $t */
$t = $modules->get($className);
2022-03-08 15:55:41 +01:00
if(!$t) continue;
2022-11-05 18:32:48 +01:00
$t->formatValue($this->wire()->page, $this->wire(new Field()), $out);
2022-03-08 15:55:41 +01:00
}
}
if(strlen($description)) {
$textFormat = $this->getSetting('textFormat');
if($this->getSetting('entityEncodeText') !== false && $textFormat != Inputfield::textFormatNone) {
if($textFormat == Inputfield::textFormatBasic) {
$description = $this->entityEncode($description, Inputfield::textFormatBasic);
2022-11-05 18:32:48 +01:00
$out = "<p class='description'>$description</p>$out";
2022-03-08 15:55:41 +01:00
} else if($textFormat == Inputfield::textFormatMarkdown) {
$out = "<div class='description'>" . $this->entityEncode($description, Inputfield::textFormatMarkdown) . "</div>$out";
}
} else {
$out = "<div class='description'>$description</div>$out";
}
$this->description = ''; // prevents it from appearing again at the bottom
}
// prevent possible double render
$v = $this->attr('value');
$this->attr('value', '');
$out .= parent::___render();
$this->attr('value', $v);
return $out;
}
2022-11-05 18:32:48 +01:00
/**
* Render value
*
* @return string
*
*/
2022-03-08 15:55:41 +01:00
public function ___renderValue() {
$this->renderValueMode = true;
$out = $this->render();
$this->renderValueMode = false;
return $out;
}
2022-11-05 18:32:48 +01:00
/**
* Configure Inputfield
*
* @return InputfieldWrapper
*
*/
2022-03-08 15:55:41 +01:00
public function ___getConfigInputfields() {
2022-11-05 18:32:48 +01:00
$modules = $this->wire()->modules;
2022-03-08 15:55:41 +01:00
$inputfields = parent::___getConfigInputfields();
if($this->hasFieldtype) return $inputfields;
2022-11-05 18:32:48 +01:00
/** @var InputfieldTextarea $f */
$f = $modules->get('InputfieldTextarea');
2022-03-08 15:55:41 +01:00
$f->attr('id+name', 'markupText');
$f->attr('value', $this->markupText);
$f->attr('rows', 10);
$f->label = $this->_('Markup Text');
$inputfields->add($f);
2022-11-05 18:32:48 +01:00
/** @var InputfieldAsmSelect $f */
$f = $modules->get('InputfieldAsmSelect');
2022-03-08 15:55:41 +01:00
$f->attr('id+name', 'textformatters');
$f->label = $this->_('Text Formatters');
2022-11-05 18:32:48 +01:00
foreach($modules->findByPrefix('Textformatter') as $moduleName) {
$info = $modules->getModuleInfo($moduleName);
$title = $info['title'] ? $info['title'] : $moduleName;
$f->addOption($moduleName, $title);
2022-03-08 15:55:41 +01:00
}
$f->attr('value', $this->textformatters);
$f->description = $this->_('Select the format that your Markup Text is in, or the formatters that you want to be applied to it, in the order you want them applied.');
$f->notes = $this->_('If your Markup Text is plain HTML, you may not want to select any Text Formatters.');
$inputfields->add($f);
return $inputfields;
}
}