Verwendung: Options Callback Function with dependency

Christian Romeni's Avatar

Christian Romeni

29 Jan, 2016 12:44 PM

Produkt: Custom Elements Contao
Contao-Version: 3.2.8

Lets say, we have 2 selects: Category & Option
Both have data stored in a database table: tl_categories & tl_options
An options are children of categories.

So if I select the Category in the first select, I only want to see the options who's pid is the category's id.

This is kinda how I would do it in a regular DCA:

public function getOptions(DataContainer $dc){
  $arrOptions = array();
  $currentCategory = $dc->activeRecord->filterCategory1;
  $q = "SELECT * FROM `tl_options` WHERE visible = 1 AND pid = " . $currentCategory . " ORDER BY alias ASC";
  $result = $this->Database->execute($q);
  while ($result->next()){
    $arrOptions[$result->id] = $result->filter;
  }
  return $arrOptions;
}

Any Ideas how I could do that in a custom content element config?

  1. Support Staff 1 Posted by RockSolid Theme... on 29 Jan, 2016 01:44 PM

    RockSolid Themes's Avatar

    Thank you for submitting your question!

    Instead of

    $currentCategory = $dc->activeRecord->filterCategory1;
    

    you can use the following code:

    $currentCategory = json_decode($dc->activeRecord->rsce_data)->filterCategory1;
    
  2. 2 Posted by Christian Romen... on 29 Jan, 2016 02:19 PM

    Christian Romeni's Avatar

    Ok, this works.. now it gets just a little bit more complicated.. ;)
    Its inside a list ... :)

    Can I find out inside wich element I currently am? So that I can set it accordingly?

  3. Support Staff 3 Posted by RockSolid Theme... on 29 Jan, 2016 02:34 PM

    RockSolid Themes's Avatar

    If your list field is named list you can use the following code:

    $index = explode('__', $dc->field);
    if ($index[1] !== 'rsce_dummy') {
        $currentCategory = json_decode($dc->activeRecord->rsce_data)
            ->list[$index[1]]
            ->filterCategory1;
    }
    
  4. 4 Posted by Christian Romen... on 29 Jan, 2016 02:51 PM

    Christian Romeni's Avatar

    Nice.. Works like a Charm! ;)

    Thank you.

  5. 5 Posted by Reiner on 10 Jan, 2017 01:51 PM

    Reiner's Avatar

    I played around with this callbackfunctions and I'm able to read out some values of my Custom Element

    $PlayListElement = explode('__', $dc->field);
    if ($PlayListElement[1] !== 'rsce_dummy')
    {
        $thumb = (json_decode($dc->activeRecord->rsce_data)->playlist[$PlayListElement[1]]->VideoNumber);
    }
    

    How can I write this String back to another field in my custom element ?

  6. Support Staff 6 Posted by RockSolid Theme... on 10 Jan, 2017 02:46 PM

    RockSolid Themes's Avatar

    The options_callback is not intended to change the value of a field. It is used to get the options for a select box or a checkbox group.

  7. 7 Posted by Reiner on 10 Jan, 2017 03:27 PM

    Reiner's Avatar

    I know, but isn't there a load_callback or input_field_callback. As far as I know they should do the Job.
    My Problem is that I dont know how to return the value,

    normally it looks like

    'thumb' => array(
        'label' => array('Videothumbnail', ''),
        'inputType' => 'text',
            'input_field_callback' => function($dc){return VARIABLE},
    ),
    

    If I return "**" , I can see those ** in my Backend instead the whole thumb field with label, field and explanation.

  8. Support Staff 8 Posted by RockSolid Theme... on 11 Jan, 2017 08:15 AM

    RockSolid Themes's Avatar

    The Contao Callbacks are not related to the Custom Elements extension. You can read more about how the DCA callbacks work in the documentation of Contao: https://docs.contao.org/books/api/dca/callbacks.html

  9. 9 Posted by landzone on 27 Jul, 2021 12:20 PM

    landzone's Avatar

    I have a list in a list. Can you show me please the right code snippet for that case?

    <?php
    return array(
        'fields' => array(
    'slides' => array( 'elementLabel' => 'Slide %s', 'inputType' => 'list', 'fields' => array( 'category' => array( 'inputType' => 'select', 'options_callback' => function () { $options = array(); $data = \Database::getInstance()->execute("SELECT * FROM tl_page WHERE pid = 1"); while ($data->next()) { $options[$data->id] = $data->title; } return $options; }, 'eval' => array('tl_class' => 'w50', 'submitOnChange'=>true, 'includeBlankOption' => true), ), 'element' => array( 'inputType' => 'select', 'options_callback' => function ($dc) { $options2 = array(); $index = explode('__', $dc->field); if ($index[1] !== 'rsce_dummy') { $currentCategory = json_decode($dc->activeRecord->rsce_data) ->slides[$index[1]] ->category_page; } if ($currentCategory) { $data2 = \Database::getInstance()->execute("SELECT * FROM tl_example WHERE category = ".$currentCategory); while ($data2->next()) { $options2[$data2->id] = $data2->title; } } return $options2; }, ), ), ), ), );
    I tried a load_callback, but it did'nt work.
    If I copy an element and choose a new category, then there is first a select "unknown option" in the dependent field. After save I have the correct choose. Is there a better solution for this?
    Thanks in advance.
  10. Support Staff 10 Posted by RockSolid Theme... on 28 Jul, 2021 09:19 AM

    RockSolid Themes's Avatar

    After save I have the correct choose.

    As you have set 'submitOnChange'=>true on the category field it should not be necessary to manually save the element. Does the browser reload after you change the selected category?

  11. 11 Posted by landzone on 28 Jul, 2021 11:32 AM

    landzone's Avatar

    Yes, the browser reload. But then the select-field element shows the unknown option, becouse this is still the choose of the previous category. How can I solve this?

  12. Support Staff 12 Posted by RockSolid Theme... on 29 Jul, 2021 07:48 AM

    RockSolid Themes's Avatar

    In addition to the options_callback you can add a load_callback that checks if the value is valid. The code could look something like this:

    'load_callback' => array(
        function ($value, $dc) {
            $options = $GLOBALS['TL_DCA'][$dc->table]['fields'][$dc->field]['options_callback']($dc);
            return array_key_exists($value, $options) ? $value : '';
        }
    ),
    
  13. 13 Posted by landzone on 29 Jul, 2021 08:36 AM

    landzone's Avatar

    Besten Dank, so funktioniert es. Den load_callback hatte ich schon versucht, allerdings war mein Funktionsinhalt falsch.
    Nur die Duplizieren-Funktion der einzelnen Elemente funktioniert damit nicht, diese würde ich daher gern ausbelnden.
    Dazu hab ich eine eigene Anfrage gestellt: https://help.rocksolidthemes.com/discussions/contao/67980-verwendun...

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac