Shoekrates

Formbuilder: allow html in label

Recommended Posts

Hi,

I want to add a checkbox to my contact form to have users confirm that they have read the privacy policy before sending the form.
In the label of the Checkbox I want to add a link to the privacy statement page. But HTML isn't rendered, it apears as text output in the label.

How can I allow html in formbuilder checkbox labels?

Thanks for a hint.

Share this post


Link to post
Share on other sites

My thought is that you could use a hook  to add the html into the rendering of the Inputfield, or maybe you could add a Markup field in FormBuilder so you can place this link, right bellow the checkbox. 

Share this post


Link to post
Share on other sites

I usually use a simple checkbox and an adjacent Markup field with all the text and links and other BS(sorry). Works great and everybody can edit the text if necessary.

checkbox-markup.png.5ea83035611ca81745be1796d17b8acf.png

  • Like 1

Share this post


Link to post
Share on other sites

Thanks to all. I built something similar as Klenkes' solution by adding the Markup CKEditor module to pw.

Share this post


Link to post
Share on other sites

@Klenkes To accomplish this, you need to replace line 94 in InputfieldCheckbox.module

"<span class='pw-no-select'>" . $this->entityEncode($label) . "</span></label>";

with this:

"<span class='pw-no-select'>" . $label . "</span></label>";

but it isn't a good idea to modify core parts.

@ryan Is it possible that you remove the entityEncode from InputfieldSelect in the core, so we can use hyperlinks, classes,  etc. in the label, or are there any security concerns? What Klenkes describes is a common need here in Germany. The other solution using InputfieldMarkup isn't as good as this solution. 

However I think one could replace the render function of the InputfieldCheckbox.module with a hook, but it would be nicer to have this feature in the core.

What do you think?

Share this post


Link to post
Share on other sites

I tried to replace the InputfieldCheckbox render function with a hook in my `_init.php`but it did not work. I get the error in the screenshot below.

Here is my hook function:

$this->addHookAfter('InputfieldCheckbox::render', function ($event) { // outside a class
//    $page = $event->object;
//    if ($page->template == 'admin') return;
    // tell ProcessWire we are replacing the method we've hooked
    $event->replace = true;

    $label = '';
    $user = wire('user');
    if ($user->language) $label = Inputfield::getSetting("checkboxLabel$user->language");
    if (!$label) $label = Inputfield::getSetting("checkboxLabel");
    if (!$label && $this->checkedValueIsLabel) $label = $this->checkedValue;
    if (!$label) $label = Inputfield::getSetting('label2');

    $this->set('skipLabel', $this->description || $label ? Inputfield::skipLabelFor : Inputfield::skipLabelHeader);
    if (!$label) $label = $this->label;

    // TBA: if($this->uncheckedValue) return $this->renderRadio();

    $attrs = Inputfield::getAttributes();
    $attrs['value'] = $this->checkedValue;

    $out =
        "<label><input type='checkbox' " . Inputfield::getAttributesString($attrs) . " />" .
        "<span class='pw-no-select'>" . $label . "</span></label>";

    $event->return = $out;
});

 

error.png

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.