Creating a custom generator in Joomla

⚠️ This article was created for developers!

To be able to follow the steps, you're required to have coding knowledge. If you're not a developer and can't achieve what you're looking for, consider hiring a developer.

Make sure to carefully follow the tutorial and write correct code. We're unable to provide help with custom coding, or the possible issues that are happening because of them.

This feature is only available from Smart Slider 3.5.1.12.

With Smart Slider you can create a generator from any source, if you just write a customized code for it.

☝️ Note: Custom Generators are only available in the Pro version of Smart Slider!
⚠️ Warning: Even if you write a custom generator, you can't get around Smart Slider's limitations! For example, you still won't be able to create a slider based on the content of the current page.
Also, please note that we won't be able to provide any kind of support for your custom generator.

Create a system plugin

Step 1

Create a folder inside your "plugins\system\" folder. I will call it "custom":

plugins\system\ custom

Step 2

Create an XML file with the same name you used for the folder:

plugins\system\custom\ custom.xml

and make it contain this code with a name and filenames of your choice:

<?xml version="1.0" encoding="utf-8"?>
<extension version="1.0" type="plugin" group="system">
    <name>Custom Plugin</name>
    <files>
        <filename plugin="custom">custom.php</filename>
    </files>
</extension>

Step 3

Create a PHP file with the same name you used in the xml file:

plugins\system\custom\ custom.php

Use this code as the starting point:

<?php
global $smartSliderCustomGenerators;

if(empty($smartSliderCustomGenerators)){
    $smartSliderCustomGenerators = array();
}

$options = array();

$smartSliderCustomGenerators[] = array(
    'name'    => 'example_generator',
    'label'   => 'Example',
    'options' => $options,
    'records' => 'getRecords'
);

function getRecords($data) {
    
    $result = array();

    return $result;
}

Step 4

At your Joomla admin area, go to System -> Discover (under Install).

Step 5

Press Discover extensions to install (or the Discover button on top, if you already have some discovered extensions).

Step 6

Select your plugin and press Install.

Step 7

Go to System -> Plugins (under Manage).

Step 8

Filter out the system type plugins and press the little arrows to be able to order them.

Step 9

By using the 3 dots in front of the plugin names you can reorder the plugins. Make sure you have your Custom Plugin before Smart Slider 3 System Plugin.

Step 10

Enable your plugin, by pressing the X icon in front of it.

That's it! Your PHP codes are now running where they should.

Check the documentation further, to see the custom generator given coding options.

Custom generator PHP codes

Step 1

Let's take a look at your current PHP code:

<?php
global $smartSliderCustomGenerators;

if(empty($smartSliderCustomGenerators)){
    $smartSliderCustomGenerators = array();
}

$options = array();

$smartSliderCustomGenerators[] = array(
    'name'    => 'example_generator',
    'label'   => 'Example',
    'options' => $options,
    'records' => 'getRecords'
);

function getRecords($data) {
    
    $result = array();

    return $result;
}

The $smartSliderCustomGenerators variable contains all custom generators and its options, like on/off options or input areas, which can be used easily to modify the result. Check out step 2 to understand it better.

The getRecords function's name can also be anything, and this has to be unique. Make sure you will use the same name next to 'records'. This function is where you are processing your code, so these will produce the datas, which will appear on the slides. Check out step 3 to understand it better.

Step 2

Let's focus on this part:

global $smartSliderCustomGenerators;

if(empty($smartSliderCustomGenerators)){
    $smartSliderCustomGenerators = array();
}

$options = array();

$smartSliderCustomGenerators[] = array(
    'name'    => 'example_generator',
    'label'   => 'Example',
    'options' => $options,
    'records' => 'getRecords'
);

You always have to define global $smartSliderCustomGenerators to be able to add your generators to it. It has to be an array containing arrays of your generators:

  • name: unique identifier of your generator.
  • label: the label of the generator that appears on the admin area.
  • options: the options you will have on the admin area. Read the documentation further to see its coding options.
  • records: the unique name of the function you will use.

Focusing on $options: this creates all the options you would like to have in the admin area.

If you want to add an on/off, add it to the $options array:

$options = array();

$options[] = array(
    'type'    => 'onoff',
    'name'    => 'published',
    'label'   => 'Published',
    'default' => 1
);

$customGenerators[] = array(
    'name'    => 'example_generator',
    'label'   => 'Example',
    'options' => $options,
    'records' => 'getRecords'
);

so this is the only new part:

$options[] = array(
        'type'    => 'onoff',
        'name'    => 'published',
        'label'   => 'Published',
        'default' => 1
    );

The type defines what kind of option would you like to have. I will write a list about all options, but now check out this onoff.

The name and label of this option can be anything. The name has to be unique, as this will be the identifier. The label is just the text written out into the admin area next to the on/off button.

The default value is either 1 or 0, to decide it should be on or off by default.

If you would like to have multiple on/off buttons in one row, create a multidimensional array:

    $options[] = array(
        array(
            'type' => 'onoff',
            'name' => 'example1',
            'label'   => 'First example on off',
            'default' => 1
        ),
        array(
            'type' => 'onoff',
            'name' => 'example2',
            'label'   => 'Second example on off',
            'default' => 0

        )
    );

The other types you have:

Select

    $options[] = array(
            'type'     => 'select',
            'name'     => 'list',
            'label'    => 'Test list',
            'options'  => array(
                1 => 'first',
                2 => 'second'
            ),
            'default'  => 2,
            'multiple' => true,
            'size'     => 5
        );

This is a selection list. The name has to be unique.

The options should contain an array of the selectable options, where the first value is the identifier, the second is the label.

In the default value can contain one of the identifiers from the previous options.

With multiple you can decide whether you want a selection list, where you can select more options together or you want a dropdown list, where only one option can be selected. This value can be true or false.

The size can be used, if multiple is set to true. This decides how many options you see together within the scrollable select. You can write a number in here.

Text

    $options[] = array(
            'type'    => 'text',
            'name'    => 'my_text',
            'label'   => 'Name test',
            'default' => ''
        );

This creates a simple text input. The name has to be unique.

Textarea

    $options[] = array(
        'type'    => 'textarea',
        'name'    => 'test_area',
        'label'   => 'test area',
        'default' => '',
        'width'   => 500,
        'height'  => 300
    );
Same as the text, just you are creating a bigger textarea, where you can write down a pixel value into the width and height options.

Step 3

Now we should discuss this part:

function getRecords($data) {
    
    $result = array();

    return $result;
}

This is where the entire processing happens. The $result should be an array containing the datas, for example:

function getRecords($data) {
    $result[0] = array(
        'first'  => 'first value',
        'second' => 'second value'
    );

    $result[1] = array(
        'first'  => 'xy',
        'second' => 'z'
    );

    return $result;
}

As you see, the keys should be the same for each arrays, as these will be the variables and their values should be unique, what you would like to see on the 1st slide, 2nd slide, etc.

The $data returns the step 2 options this way:

$data['options']['name']

so for example if you had an on/off, you can use it to decide what should happen:

function getRecords($data) {
    
    if($data['options']['usewords']){
        $result[0] = array(
            'first'  => 'first value',
            'second' => 'second value'
        );
    } else {
        $result[0] = array(
            'first'  => 'xy',
            'second' => 'z'
        );      
    }

    return $result;
}

Example

<?php
global $smartSliderCustomGenerators;

if(empty($smartSliderCustomGenerators)){
    $smartSliderCustomGenerators = array();
}

$options = array();

$options[] = array(
    'type'    => 'onoff',
    'name'    => 'published',
    'label'   => 'Published',
    'default' => 1
);

$options[] = array(
    array(
        'type' => 'onoff',
        'name' => 'published2'
    ),
    array(
        'type' => 'onoff',
        'name' => 'published3'
    )
);

$options[] = array(
    array(
        'type'     => 'select',
        'name'     => 'list',
        'label'    => 'Test list',
        'options'  => array(
            1 => 'first',
            2 => 'second'
        ),
        'default'  => 2,
        'multiple' => true,
        'size'     => 5
    ),
    array(
        'type'    => 'text',
        'name'    => 'name',
        'label'   => 'Name test',
        'default' => ''
    )
);

$options[] = array(
    'type'    => 'textarea',
    'name'    => 'test_area',
    'label'   => 'test area',
    'default' => '',
    'width'   => 500,
    'height'  => 300
);

$smartSliderCustomGenerators[] = array(
    'name'    => 'example_generator',
    'label'   => 'Example',
    'options' => $options,
    'records' => 'getRecords'
);

function getRecords($data) {
    $result = array();

    $result[0] = array(
        'first'  => $data['options']['name'],
        'second' => 'something'
    );

    $result[1] = array(
        'first'  => 'xy',
        'second' => 'z'
    );

    return $result;
}

In a more real life example we are asking down datas for example from the database. With a foreach or for loop you can go trough all these datas and return your result:

function getRecords($data) {
    $db = JFactory::getDbo();
    $db->setQuery('SELECT * FROM #__content LIMIT 5');
    $articles = $db->loadAssocList();
    
    $result = array();
    
    foreach($articles AS $article){
        $helper = array();
        $helper['title'] = $article['title'];
        $helper['description'] = $article['introtext'];
        
        $result[] = $helper;
    }

    return $result;
}

Tip

Step 3 could be hard to debug, but not if you know where to look. For debugging purposes you can use var_dump and exit functions to write out what datas you have:

function getRecords($data) {
    $result[0] = array(
        'first'  => $data['options']['name'],
        'second' => 'something'
    );

    $result[1] = array(
        'first'  => 'xy',
        'second' => 'z'
    );
    
    var_dump($result);exit;

    return $result;
}

To see what this results, in Chrome if you press F12 -> click on Network, then by pressing our View records button a new call will appear. In this call's Response you will see the var_dump-ed datas.


Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.