From: Matthias Schmidt Date: Sat, 6 Apr 2019 10:51:41 +0000 (+0200) Subject: Add `TemplateFormNode` X-Git-Tag: 5.2.0_Alpha_1~165 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=1683aa9d7da79c8c00ea19711ae5cd2a9ce25ae9;p=GitHub%2FWoltLab%2FWCF.git Add `TemplateFormNode` See #2509 --- diff --git a/wcfsetup/install/files/lib/system/form/builder/TemplateFormNode.class.php b/wcfsetup/install/files/lib/system/form/builder/TemplateFormNode.class.php new file mode 100644 index 0000000000..45e9be733e --- /dev/null +++ b/wcfsetup/install/files/lib/system/form/builder/TemplateFormNode.class.php @@ -0,0 +1,137 @@ + + * @package WoltLabSuite\Core\System\Form\Builder + * @since 5.2 + */ +class TemplateFormNode implements IFormChildNode { + use TFormChildNode; + use TFormNode; + + /** + * application of the template with the contents of the form node + * @var null|string + */ + protected $application; + + /** + * name of the template with the contents of the form node + * @var null|string + */ + protected $templateName; + + /** + * template variables passed to the template + * @var array + */ + protected $variables = []; + + /** + * Sets the application of the template with the contents of the form node and returns this + * form node. + * + * @param string $application application abbreviation + * @return static this form node + * + * @throws \InvalidArgumentException if no application with the given abbreviation exists + */ + public function application($application) { + if (ApplicationHandler::getInstance()->getApplication($application) === null) { + throw new \InvalidArgumentException("Unknown application with abbreviation '{$application}'."); + } + + $this->application = $application; + + return $this; + } + + /** + * Returns the application of the template with the contents of the form node. + * + * If no application has been set, `wcf` will be set and return. + * + * @return string application abbreviation + */ + public function getApplication() { + if ($this->application === null) { + $this->application = 'wcf'; + } + + return $this->application; + } + + /** + * @inheritDoc + */ + public function getHtml() { + return WCF::getTPL()->fetch( + $this->getTemplateName(), + $this->getApplication(), + $this->getVariables(), + true + ); + } + + /** + * Returns the name of the template with the contents of the form node. + * + * @return string name of template with form node contents + * + * @throws \BadMethodCallException if template name has not been set yet + */ + public function getTemplateName() { + if ($this->templateName === null) { + throw new \BadMethodCallException("Template name has not been set yet."); + } + + return $this->templateName; + } + + /** + * Returns the template variables passed to the template. + * + * @return array template variables passed to template + */ + public function getVariables() { + return $this->variables; + } + + /** + * Sets the name of the template with the contents of the form node and returns this form node. + * + * @param string $templateName name of template with form node contents + * @return static this form node + */ + public function templateName($templateName) { + $this->templateName = $templateName; + + return $this; + } + + /** + * @inheritDoc + */ + public function validate() { + // does nothing + } + + /** + * Sets the template variables passed to the template and returns this form node. + * + * @param array $variables template variables passed to template + * @return static this form node + */ + public function variables(array $variables) { + $this->variables = $variables; + + return $this; + } +}