Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 Today 09:33:33

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 5,317
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="zahlungsinterval">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="zahlungsinterval">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 Today 11:02:24

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 12,586
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

Board footer

Powered by FluxBB