Getting id from stream in a loop for use in edit form pyrocms

219 views Asked by At

I am using the pyrocms with the streams module to loop through content

{{ streams:gallery}}

<div class="col-lg-3 col-md-4 col-sm-4">
    <a href="#">
        <div class="ratio" style="background:url({{gallery_images:image}})"></div>
    </a>
    <div class="text-center">
        <h5>{{title}}</h5>
    </div>
</div>

<!-- FORM CODE BELOW WILL GO HERE USING THE GALLERY STREAM -->

{{ /streams:gallery }}

I would like to get the id of the current looped item and then use that in a stream form to edit the content. Like so

{{ if user:logged_in }}
{{ streams:form stream="gallery" mode="edit" edit_id="1" include="page_image|deschtml"}}
{{ form_open }}

<span class="click-to-edit">
    <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span> Edit
</span>
<span class="inline-edit">

    {{ error }}
    {{ page_image:input }}
    {{ deschtml:input }}
    {{ form_submit }}

    <button class="inline-close pull-right" type="button">Cancel</button>
</span>
{{ form_close }}
{{ /streams:form }}
{{ endif }}

I want to be able to get the id value and pass it in

edit_id="ID-VALUE-HERE"

I figured something like this might work

edit_id="{{id}}"

but the lex parser is breaking inside of a stream that is inside of a stream.

I am not sure if it is possible to get values from the stream to use in a child stream. Is there a way to achieve this somehow? Thanks

EDIT

Regarding this issue and using [segments]

This works completely using url segments as the id that is passed. For example

{{ streams:form stream="custom_details" mode="edit" edit_id="[segment_3]" include="page_image|deschtml"}}

where [segment_3] , in my case, this is the id for the stream item. Which is awesome. However as in the code for the initial example it will not work using the streams id or {{ id }}

1

There are 1 answers

2
mgrueter On
{{ streams:form stream="custom_details" mode="edit" edit_id=id include="page_image|deschtml" }}

should work.

See PyroCMS Tags documentation - Using Tags and Variables in Tag Parameters for more information.

Generally you can omit the curly braces if using a variable as tag parameter, or a plugin call without any parameters.

// Update

Here is an example implementation for using a custom plugin to circumvent problems with nested tag calls:

create a new plugin in addons/shared_addons/plugin called "customplugin.php"

code:

<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Plugin_Customplugin extends Plugin {

    public function galeryform()
    {
        $id = $this->attribute('galeryid', false);

        if( ! $id ) {
            return;
        }

        return $this->theme_view('partials/galery_form', array('galeryid' => $id), true);
}

create a folder "partials" in your theme folder, and add "galery_form.html", which contains the markup for your stream form:

{{ streams:form stream="custom_details" mode="edit" edit_id=galeryid include="page_image|deschtml"}}
[ ... and the rest of your markup ... ]

replace the form code in your galery layout/view with the plugin call:

{{ customplugin:galeryform galery_id=id }}

You can of course change the name of the plugin, just make sure the classname matches the filename + 'Plugin_' prefix, and change the plugin call accordingly.

It's almost always easier to work with custom plugins, than nesting lex tags.