9

I want to embed a simple slideshow to my template, and I want to use subform to make easier to change the image.

This is the code I use to call the child xml file

<field name="slideshow_items" type="subform" formsource="templates/jomlamaster/admin/subform/slideshow.xml" min="1" max="4" multiple="true" layout="joomla.form.field.subform.repeatable-table" groupByFieldset="true" label="Subform Field" description="Subform Field Description" />

and this is the content of slideshow.xml :

<?xml version="1.0" encoding="UTF-8"?>
<form>
<fieldset name="fieldset" label="Slideshow">
<field name="slideshow_img" type="media" directory="" label="Slideshow item" description="Choose an image slideshow"/>
<field name="slideshow_title" type="text" class="inputbox" label="Slideshow text" size="40" />
</fieldset>
</form>

This is what I get when print_r($slideshow_items);

stdClass Object
(
    [slideshow_items0] =&gt; stdClass Object
        (
            [slideshow_img] =&gt; images/slideshow/img0.jpg
            [slideshow_text] =&gt; Title 1
        )

    [slideshow_items1] =&gt; stdClass Object
        (
            [slideshow_img] =&gt; images/slideshow/img1.jpg
            [slideshow_text] =&gt; Title 2
        )

    [slideshow_items2] =&gt; stdClass Object
        (
            [slideshow_img] =&gt; images/slideshow/img2.jpg
            [slideshow_text] =&gt; Title 3
        )

    [slideshow_items3] =&gt; stdClass Object
        (
            [slideshow_img] =&gt; images/slideshow/img3.jpg
            [slideshow_text] =&gt; Title 4
        )

)

How can I parse the values above so I can use them for slideshow?

Thanks!

4 Answers 4

7

This is my solution. If there's any better solution please let me know

<?php foreach ($slideshow_items as $item) : ?>
  <div class="slideshow">
    <img src="<?php echo $item->slideshow_img; ?>" />
    <p class="slideshow_text">
      <?php echo $item->slideshow_text; ?>
    </p>
  </div>
<?php endforeach; ?>
1
  • 1
    that is correct solution ;) only thing, I would check whether property exist, something like: echo empty($item->slideshow_img) ? '' : $item->slideshow_img;
    – Fedik
    Jul 14, 2016 at 20:16
3

Sub Form Fields data save as JSON format so first need to "json_decode()" this to get the data then run foreach look to get data.

<?php $slideshow_items= json_decode($this->item->slideshow_items); ?>

<?php foreach ($slideshow_items as $item) : ?>
<div class="slideshow">
<img src="<?php echo $item->slideshow_img; ?>" />
<p class="slideshow_text">
  <?php echo $item->slideshow_text; ?>
</p>
</div>
<?php endforeach; ?>
3

When you need to get just one field names' value:

use Joomla\Utilities\ArrayHelper;

$items = ArrayHelper::getColumn((array) $params->get('subformName'), 'fieldName');
1
  • How do you add or call the Joomla\Utilities\ArrayHelper;?
    – Eoin
    Dec 6, 2017 at 0:18
1

If you are using the Bootstrap Carousel. this works for me.

<div id="gallery-1" class="carousel slide" data-ride="carousel">
    <div class="carousel-inner" role="listbox">
        <?php foreach ($slideshow_items as $item => $value) :?>
            <div class="item<?php echo ($item=='slideshow_items0') ? ' active': ''; ?>">
                <img src="<?php echo $value->slideshow_img;?>">
            </div>
            <div class="carousel-caption>">
                <p><?php echo $value->slideshow_text;?></p>
            </div>
        <?php endforeach; ?>
    </div>
</div>

if someone wants to improve or provide best practices code is welcome.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.