Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 Yesterday 09:33:33

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 5,319
Website GitHub

Showing custom labels in a com_connect email using the body_form?

I have a membership signup form using com_connect (v4.9 branch) and txp 4.9.1 that has a few optional blocks, radio options and checkboxes. I’m trying to give the recipients a clean output using a body_form with a text-only and html part. That all works nicely enough, but I’m having the weirdest problems providing custom labels for the radio buttons and checkboxes.

First of all, the regular text inputs and textareas that have a label attribute all show up fine. As the radios have a group rather than a label and checkboxes (and a few special cases) are handled differently, I get my barebones name attribute in the output. I wanted to make that cleaner.

Here’s the relevant part of the body form (slightly simplified):

<table>
<txp:com_connect_fields break="tr">
    <td >
        <txp:evaluate test="com_connect_if">
        <txp:com_connect_if name="mitgliedstyp">Art der Mitgliedschaft</txp:com_connect_if>
        <txp:com_connect_if name="dateOfBirth">Geburtsdatum</txp:com_connect_if>
        <txp:com_connect_if name="amount">Beitrag</txp:com_connect_if>
        <txp:com_connect_if name="paymentMethod">Zahlungsmethode</txp:com_connect_if>
        <txp:com_connect_if name="zahlungsinterval">Zahlungsinterval</txp:com_connect_if>
        <txp:com_connect_if name="newsletter">Newsletter</txp:com_connect_if>
        <txp:com_connect_if name="datenverarbeitung">Datenverarbeitung</txp:com_connect_if>
        <txp:else />
        <txp:com_connect_label />
        </txp:evaluate>
    </td>
    <td>
        <txp:evaluate test="com_connect_if">
        <txp:com_connect_if name="mitgliedstyp" value="mitglied_voll">Mitglied</txp:com_connect_if>
        <txp:com_connect_if name="mitgliedstyp" value="mitglied_foerder">Fördermitglied</txp:com_connect_if>
        <txp:com_connect_if name="amount"><txp:com_connect_value /> €</txp:com_connect_if>
        <txp:com_connect_if name="paymentMethod" value="bank_ueberweisung">Überweisung</txp:com_connect_if>
        <txp:com_connect_if name="paymentMethod" value="bank_lastschrift">Lastschrift</txp:com_connect_if>
        <txp:com_connect_if name="zahlungsinterval" value="lastschrift_monatlich">Monatlich</txp:com_connect_if>
        <txp:com_connect_if name="zahlungsinterval" value="lastschrift_halbjaehrlich">Halbjährlich</txp:com_connect_if>
        <txp:else />
        <txp:com_connect_value />
        </txp:evaluate>
    </td>
</txp:com_connect_fields>
</table>

I’ve also tried this with populating a variable and then outputting either the variable or the regular internal label, (resetting it for each loop) e.g.:

<table>
<txp:com_connect_fields break="tr">
    <td>
        <txp:variable name="antrag_label" value="" />
        <txp:variable name="antrag_label" trim>
            <txp:com_connect_if name="mitgliedstyp">Art der Mitgliedschaft</txp:com_connect_if>
            <txp:com_connect_if name="dateOfBirth">Geburtsdatum</txp:com_connect_if>
            <txp:com_connect_if name="amount">Beitrag</txp:com_connect_if>
            <txp:com_connect_if name="paymentMethod">Zahlungsmethode</txp:com_connect_if>
            <txp:com_connect_if name="zahlungsinterval">Zahlungsinterval</txp:com_connect_if>
            <txp:com_connect_if name="newsletter">Newsletter</txp:com_connect_if>
            <txp:com_connect_if name="datenverarbeitung">Datenverarbeitung</txp:com_connect_if>
        </txp:variable>
        <txp:if_variable name="antrag_label" value="">
            <txp:com_connect_label />
        <txp:else />
            <txp:variable name="antrag_label" />
        </txp:if_variable>
    </td>
    <!-- analogue for com_connect_value -->
</txp:com_connect_fields>
</table>

Whatever I try, I get output like this in my email (excerpt):

(and so on…). The details marked in bold in the right-hand column are the actual values supplied in the form.

How can I get just the relevant item’s label to show in each case, rather than getting them all in every field?

Perhaps I’m wrongly assuming that txp:com_connect_fields loops like article_custom?

Do I have to write out each table line with a special case individually and then loop over only the relevant items with regular labels using the <txp:com_connect_fields name="field, names, here">…</txp:com_connect_fields> where appropriate?

EDIT: FWIW, this is what it looks like when output as a regular list without any com_connect_if tags:


TXP Builders – finely-crafted code, design and txp

Offline

#2 Yesterday 11:02:24

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 12,587
Website GitHub

Re: Showing custom labels in a com_connect email using the body_form?

Hmm, I think the iterator is at fault here.

Look at the com_connect_fields function (here’s the relevant chunk)…

    foreach ($com_connect_labels as $nm => $lbl) {
        if ((empty($label) && empty($name)) || ($label && in_array($lbl, $labels)) || ($name && in_array($nm, $names))) {
            $com_connect_item = $nm;
            $out[] = parse($thing);
        }
    }

    $com_connect_item = null;

The main thing to note is that it iterates over all the labels and outputs the fully-parsed $thing (container) which, in your case, is trying to access $com_connect_item to do the comparison against each item. That, I expect, remains in scope for every label so it matches every loop and outputs it.

The fix, I. would guess, is to try this:

    foreach ($com_connect_labels as $nm => $lbl) {
        if ((empty($label) && empty($name)) || ($label && in_array($lbl, $labels)) || ($name && in_array($nm, $names))) {
            $com_connect_item = $nm;
            $out[] = parse($thing);
            $com_connect_item = null;
        }
    }

Or maybe even this:

    foreach ($com_connect_labels as $nm => $lbl) {
        if ((empty($label) && empty($name)) || ($label && in_array($lbl, $labels)) || ($name && in_array($nm, $names))) {
            $com_connect_item = $nm;
            $out[] = parse($thing);
        }

        $com_connect_item = null;
    }

That should clear the global each iteration, which means subsequent labels won’t match.

Not sure if it has any other ramifications for other tag uses, but I can’t see any downside. Probably just an oversight, sorry.

If that works and doesn’t make other uses explode, let me know which works and I’ll patch it.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Hire Txp Builders – finely-crafted code, design and Txp

Offline

#3 Today 08:28:08

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 5,319
Website GitHub

Re: Showing custom labels in a com_connect email using the body_form?

Bloke wrote #343344:

The fix, I. would guess, is to try this:

foreach ($com_connect_labels as $nm => $lbl) {...

Or maybe even this:

foreach ($com_connect_labels as $nm => $lbl) {...

That should clear the global each iteration, which means subsequent labels won’t match.

Sounds logical, and I think either of those changes probably make sense anyway, but neither solved the problem.

After lots of dmp-ing to debug.txt, I think I’ve found the culprit, not in the iterator but in com_connect_if.

dmp-ing the various variables in that function to debug.txt reveals that $cond evaluates to true for every instance, even if the field name does not match. It checks if the value is empty or matches but not against the current name. My solution was:

Change line 2544 from:

if ($val) {

to

if ($name == $com_connect_item && $val) {

Result: correct output 🎉

I can make a PR for that if that is okay (or should it be with ===?).


TXP Builders – finely-crafted code, design and txp

Offline

#4 Today 13:37:31

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 12,587
Website GitHub

Re: Showing custom labels in a com_connect email using the body_form?

D’oh! Good catch. Yes pleas a PR with double equals is absolutely fine. Thank you so much for sleuthing this one, and apologies for the shoddy code.

EDIT: does that only apply if you’re comparing the name though? The conditional check just after extracting $atts:

    if (empty($label) && empty($name) && !empty($com_connect_item)) {
        $name = $com_connect_item;
    }

is meant to assign the $name to the current item if you haven’t supplied one. And then the comparator occurs against the name and/or label by utilising the array-nature of com_connect_value.

It’s still faulty logic but what happens if you try to compare label and/or value too? I wonder if the tests need to be split up into three instead of trying to do them all together?

Last edited by Bloke (Today 13:43:02)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Hire Txp Builders – finely-crafted code, design and Txp

Offline

#5 Today 16:52:22

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 5,319
Website GitHub

Re: Showing custom labels in a com_connect email using the body_form?

I haven’t tried with label but I have got name + value combinations and they work:

<txp:com_connect_if name="zahlungsinterval" value="lastschrift_monatlich">Monatlich</txp:com_connect_if>
        <txp:com_connect_if name="zahlungsinterval" value="lastschrift_halbjaehrlich">Halbjährlich</txp:com_connect_if>

But you may be right that label attribute needs covering.


TXP Builders – finely-crafted code, design and txp

Offline

Board footer

Powered by FluxBB