# Repeater in widget

Some widgets need to have an infinite group of fields (ex: columns). Before all repeaters had to be coded in Minotaure js files. Now you just have to create in your form an array of input you will need.

# Types available

  • Input
  • Editor
  • Media

# Input

"Input": #the name of the function to call  
{
    #required
    "label": "Titre", ##Label of the input
    "name": "title",  #Name and id of the input

    #optionnal
    "type" : "number", #Type of the input (text,date,number,…)
},
1
2
3
4
5
6
7
8
9

# Media

"Media": #the name of the function to call  
{
    #required
    "label": "Image", ##Label of the input
    "name": "image",  #Name and id of the input

    #optionnal
    "type" : "image", #Type of the media to load
    "button" : "Add a new media", #Label of the button to add a media
},
1
2
3
4
5
6
7
8
9
10

# Editor

"Editor": #the name of the function to call  
{
    #required
    "label": "Content", ##Label of the input
    "name": "content",  #Name and id of the input
},
1
2
3
4
5
6

# How to create

Complete tutorial

# Register.php

In your Register.php, you will need this class

use Be\Minotaure\Widgets\Core\Sections;
1

In your public function form, sections instance must to be merge with $data, created and finally set

public function form($instance)
  {
    $data = array_merge($this->default, $instance, [
      'widget' => $this,
      'sections' => ''
    ]);

    $newSections = Sections::create($data);
    $data['sections'] = Sections::set($newSections, $this);

    SetView::form('package.widget_name', $data);
  }
1
2
3
4
5
6
7
8
9
10
11
12

In public function update, sections need to be recovered, updated and finally merge with $data;

public function update($instance, $old_instance)
  {

    $sections = Sections::get($instance);
    $newSections = Sections::update($sections);

    $data = Validator::multiple($instance);


    return array_merge($data, $newSections);
  }
1
2
3
4
5
6
7
8
9
10
11

And in public function widget, sections need to be recovered, parsed and sent to $data.

public function widget($args, $instance)
  {

    $data = array_merge($args, $this->default, $instance);

    $sections = Sections::get($instance);
    if (!empty($sections)) $sections = Sections::parse($sections);
    $data['sections'] = $sections;

    SetView::widget('package.widget_name', $data);
  }
1
2
3
4
5
6
7
8
9
10
11

# form.twig

Your sections are stocked in the variables sections sent by Register.php. The view need two blocks ; one to diplay sections already registered, another to display the repeater form.

Please respect the entiry structure of those blocks, the js depend on it.

# Display sections registered


<div class="repeater-wrapper">
{% if sections %}
    <div class="mt-repeater-hidden">
        <div class="mt-repeater-hidden-inputs">
            {{ sections|raw }}
        </div>
    </div>
{% endif %}
</div>
1
2
3
4
5
6
7
8
9

# Display repeater form


<div class="mt-repeater-main-container repeater-container-columns">
      "My_Type":{
        "label": "My label",
        "name": "my_name"
      }
</div>
1
2
3
4
5
6

# widget.twig

As in form.twig, sections variable was sent by Register.php.

{% for section in sections if sections %}
    {{section.title|raw}}
    {{fn.apply_filters('the_content', fn.html_entity_decode(section.text))|raw}}
{% endfor %}
1
2
3
4

WARNING

Texts need to be escape for special characters. Use |raw

Editor need to be decode and escape. Use fn.html_entity_decode(section.text))|raw

Last Updated: 4/10/2020, 1:02:42 PM