Custom blocks - send data from form to view

Permalink
Hello,

I'm new to concrete 5 so I still don't get it how everyting works around here.
The problem I'm currently facing is making a custom block for displaying data from table. This part works jsut fine, but I want to filter which data will be show in block depending on type field in db. So I want to add a dropdown selection in form.php and depending on which option is selected then display the correct data in view.php.

Problem is I can't figure it out how to send data from form.php to view.php.
I would appriciente some direction or a simple example how this works.

Thanks everybody for your help, Rok.

 
MrKDilkington replied on at Permalink Best Answer Reply
MrKDilkington
Hi korxz,

I am not positive about what you are trying to achieve, but here is an example of using a select drop-down in the core.
- Here is your block form select drop-down, with the selected value saved as "color".
https://github.com/concrete5/concrete5/blob/develop/concrete/blocks/...
- Here is the "color" field database table column.
https://github.com/concrete5/concrete5/blob/develop/concrete/blocks/...
- Here is "$color" used in the block view.
https://github.com/concrete5/concrete5/blob/develop/concrete/blocks/...

When you save the block form, the value of "color" is saved without any additional steps. Unless the color table field was an integer type, then you would need to make sure to cast the value to (int) in the controller save() method. In view.php, concrete5 makes the "$color" variable available with the value saved in the form.
korxz replied on at Permalink Reply
Thank you, that is excatly what I was looking for. I thought variables choosed in form (select) have to be saved in controller or something to be then displayed in view.

Correct me please if I got it wrong. So the key here is that

<div class="form-group controls-only <?php echo isset($controls) && $controls == 0 ? 'hidden' : '';?>">
    <?php  echo $form->label('color', t('Progress Bar Color'))?>
    <?php  echo $form->select('color', array('Katedra' => t('Katedra'), 'Inštituti' => t('Inštituti'), 'Centri' => t('Centri')), $color)?>
</div>


$color variable at the end matches the one in db.xml?
<field name="color" type="X" ></field>
MrKDilkington replied on at Permalink Reply
MrKDilkington
@korxz
<?php echo $form->select('color', array('red' => t('Red'), 'white' => t('White')), $color); ?>

- The 'color' argument is the field name in your db.xml and the column in your database table row that the selected value will be saved to. It will also be the name attribute used in your select drop-down.
- The $color argument is used to determine which select option is "selected". When you first add the block and the form is displayed, $color has no value, so none of the options are selected. After you make a selection and save the block, the selected value is saved in the 'color' column in the database table. When you edit the block and the form is displayed again, $color will have the value of the saved value in the 'color' column database table. concrete5 sets the value of $color for you.
- When the block is saved, the value of 'color' will be saved to the database for you automatically. You don't need to manually save it in the controller, unless the value needs special handling.
- concrete5 will make $color available for you in the form and in the view, using the saved value of 'color'.

This is what the select() form widget creates:
<select id="color" name="color" class="form-control">
    <option value="red">Red</option>
    <option value="white">White</option>
</select>

You don't have to use a form widget, it just makes things faster.

Here is a tutorial that explains how to use other form widgets:
https://documentation.concrete5.org/tutorials/how-to-use-the-form-wi...
korxz replied on at Permalink Reply
Great, I get it now! Thank you so much. I succesfully did what I wanted to do.