Get a structurefield content for a own field type

Hi,

I have a structurefield where a customer can generate discountcodes for a registerform for a conference.
No I want to serve him a field where he can see a link where the discountcode is inside as a variable.

My problem now is how can I get a field from the structure I’m actual in it in the panel?

Best

Maybe the controlledlist plugin is a good place to look in?

In short: this plugin fetches its “options” via a controller you create (so you can have whatever logic you want in it), and supplies it to another field (it extends the checkboxes, radio & select field are included).

HI,
thanks for your reply.
To get the content from a normal panelfield is not my problem. I need the content of a structurefield.
I had also not an idea how my logic should look like to get the structure?
I tried it with $thisor parentField()but can’t see what in my pageobjekct can help me to identify in which structure I’m actually in it.

Actually with $page->discounts()->toStructure()I get all structure field contents but I don’t know in which I’m actually in it to use the right key?

Structure Object
(
    [0] => Structure Object
        (
            [hash_id] => Field Object
                (
                    [page] => validation-registration
                    [key] => hash_id
                    [value] => 3ddacdce632652fc3b2c06328cda47b8
                )

            [discountname] => Field Object
                (
                    [page] => validation-registration
                    [key] => discountname
                    [value] => Superstakeholder
                )

            [discount] => Field Object
                (
                    [page] => validation-registration
                    [key] => discount
                    [value] => 250.00
                )

            [discountend] => Field Object
                (
                    [page] => validation-registration
                    [key] => discountend
                    [value] => 2018-08-16
                )

            [discountid] => Field Object
                (
                    [page] => validation-registration
                    [key] => discountid
                    [value] => Superstakeholder-250
                )

        )

    [1] => Structure Object
        (
            [hash_id] => Field Object
                (
                    [page] => validation-registration
                    [key] => hash_id
                    [value] => 71a243d77a9b68fa068a6e97b2943134
                )

            [discountname] => Field Object
                (
                    [page] => validation-registration
                    [key] => discountname
                    [value] => Contentcreator
                )

            [discount] => Field Object
                (
                    [page] => validation-registration
                    [key] => discount
                    [value] => 200.00
                )

            [discountend] => Field Object
                (
                    [page] => validation-registration
                    [key] => discountend
                    [value] => 
                )

            [discountid] => Field Object
                (
                    [page] => validation-registration
                    [key] => discountid
                    [value] => contentcreator-200
                )

        )

)

Maybe you can explain your use case a bit better, I have trouble understanding it.

Do you mean while the user is editing an item. inside the structure field you want to display the value of this. structure field. item in another field? And why?

Or something completely different?

Hi,

the basicfunction of the structurefield is to save discountcodes, for this I have five fields

  • hash_id ( actually not really used)
  • discountid (the discountidentifier)
  • discountname (the discountname)
  • discount (the discountvalue)
  • discountend (for timelimited discounts)

now I want to serve another field where the customer can directly see a link with the discountcode.
And if he press the link he can write directly a mail to his customer with this link to use the discountcode.
Also the return of the field should look like:
return '<a href="mailto:?body='. $page->url() . $page->hash_id() .'">'. $page->url() . $hash->hash_id() .'</a>'; (actually the php output is not right. pls. ignore).
I know that I need javascript for the direct output, but after one save it should work I think. If I get the direct structurefield.

Cheers

Well, an active item is opened in a modal, so you could use JavaScript to read the value of the desired field from the input field in the modal and then inject it into your custom field.

But I think I would handle this differently, somehow this sounds like a rather fragile solution. Maybe output the complete set somwhere with the links or insert an additional field into the structure with the link on page save, so when the user opens the item the. next time, he can click the click.

1 Like