From 325ee3da154778248e49c31626d5a521252c2a9d Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Wed, 13 Jun 2018 15:34:41 -0400 Subject: [PATCH] Add a couple of new traversal methods to Inputfield: $inputfield->getRootParent(); and $inputfield->getForm();. While it was possible to get this info before, this makes it more obvious and straightforward. --- wire/core/Inputfield.php | 46 +++++++++++++++++-- wire/core/Modules.php | 1 + .../ProcessModule/ProcessModule.module | 2 +- 3 files changed, 43 insertions(+), 6 deletions(-) diff --git a/wire/core/Inputfield.php b/wire/core/Inputfield.php index 1ebe5ad7..72c6a2b3 100644 --- a/wire/core/Inputfield.php +++ b/wire/core/Inputfield.php @@ -497,6 +497,39 @@ abstract class Inputfield extends WireData implements Module { return $parents; } + /** + * Get the root parent InputfieldWrapper element (farthest parent, commonly InputfieldForm) + * + * This returns null only if Inputfield it is called from has not yet been added to an InputfieldWrapper. + * + * #pw-group-traversal + * + * @return InputfieldForm|InputfieldWrapper|null + * @since 3.0.106 + * + */ + public function getRootParent() { + $parents = $this->getParents(); + return count($parents) ? end($parents) : null; + } + + /** + * Get the InputfieldForm element that contains this field or null if not yet defined + * + * This is the same as the `getRootParent()` method except that it returns null if root parent + * is not an InputfieldForm. + * + * #pw-group-traversal + * + * @return InputfieldForm|null + * @since 3.0.106 + * + */ + public function getForm() { + $form = $this instanceof InputfieldForm ? $this : $this->getRootParent(); + return ($form instanceof InputfieldForm ? $form : null); + } + /** * Set an attribute * @@ -1571,9 +1604,12 @@ abstract class Inputfield extends WireData implements Module { */ public function entityEncode($str, $markdown = false) { + /** @var Sanitizer $sanitizer */ + $sanitizer = $this->wire('sanitizer'); + // if already encoded, then un-encode it if(strpos($str, '&') !== false && preg_match('/&(#\d+|[a-zA-Z]+);/', $str)) { - $str = html_entity_decode($str, ENT_QUOTES, "UTF-8"); + $str = $sanitizer->unentities($str); } if($markdown && $markdown !== self::textFormatNone) { @@ -1585,17 +1621,17 @@ abstract class Inputfield extends WireData implements Module { if(!$textFormat) $textFormat = self::textFormatBasic; if($textFormat & self::textFormatBasic) { // only basic markdown allowed (default behavior) - $str = $this->wire('sanitizer')->entitiesMarkdown($str, array('allowBrackets' => true)); + $str = $sanitizer->entitiesMarkdown($str, array('allowBrackets' => true)); } else if($textFormat & self::textFormatMarkdown) { // full markdown, plus HTML is also allowed - $str = $this->wire('sanitizer')->entitiesMarkdown($str, array('fullMarkdown' => true)); + $str = $sanitizer->entitiesMarkdown($str, array('fullMarkdown' => true)); } else { // nothing allowed, text fully entity encoded regardless of $markdown request - $str = $this->wire('sanitizer')->entities($str); + $str = $sanitizer->entities($str); } } else { - $str = $this->wire('sanitizer')->entities($str); + $str = $sanitizer->entities($str); } return $str; diff --git a/wire/core/Modules.php b/wire/core/Modules.php index ee90d20e..ffafa863 100644 --- a/wire/core/Modules.php +++ b/wire/core/Modules.php @@ -3615,6 +3615,7 @@ class Modules extends WireArray { // we allow for option of no return statement in the method $module = $this->getModule($moduleName); $fields = $this->wire(new InputfieldWrapper()); + $fields->setParent($form); $_fields = $module->getModuleConfigInputfields($fields); if($_fields instanceof InputfieldWrapper) $fields = $_fields; unset($_fields); diff --git a/wire/modules/Process/ProcessModule/ProcessModule.module b/wire/modules/Process/ProcessModule/ProcessModule.module index bae17f2f..dae939bd 100644 --- a/wire/modules/Process/ProcessModule/ProcessModule.module +++ b/wire/modules/Process/ProcessModule/ProcessModule.module @@ -1241,7 +1241,7 @@ class ProcessModule extends Process { $form->add($field); } - $fields = $this->wire('modules')->getModuleConfigInputfields($moduleName); + $fields = $this->wire('modules')->getModuleConfigInputfields($moduleName, $form); if($fields) { foreach($fields as $field) { $form->add($field);