tag:help.rocksolidthemes.com,2012-07-23:/discussions/contao/14508-verwendung-options-callback-function-with-dependencyRockSolid Themes: Discussion 2021-07-29T08:38:34Ztag:help.rocksolidthemes.com,2012-07-23:Comment/390394672016-01-29T13:44:23Z2016-01-29T13:44:23ZVerwendung: Options Callback Function with dependency<div><p>Thank you for submitting your question!</p>
<p>Instead of</p>
<pre>
<code>$currentCategory = $dc->activeRecord->filterCategory1;</code>
</pre>
<p>you can use the following code:</p>
<pre>
<code>$currentCategory = json_decode($dc->activeRecord->rsce_data)->filterCategory1;</code>
</pre></div>RockSolid Themestag:help.rocksolidthemes.com,2012-07-23:Comment/390394672016-01-29T14:19:25Z2016-01-29T14:19:25ZVerwendung: Options Callback Function with dependency<div><p>Ok, this works.. now it gets just a little bit more
complicated.. ;)<br>
Its inside a list ... :)</p>
<p>Can I find out inside wich element I currently am? So that I can
set it accordingly?</p></div>Christian Romenitag:help.rocksolidthemes.com,2012-07-23:Comment/390394672016-01-29T14:34:15Z2016-01-29T14:34:15ZVerwendung: Options Callback Function with dependency<div><p>If your list field is named <code>list</code> you can use the
following code:</p>
<pre>
<code>$index = explode('__', $dc->field);
if ($index[1] !== 'rsce_dummy') {
$currentCategory = json_decode($dc->activeRecord->rsce_data)
->list[$index[1]]
->filterCategory1;
}</code>
</pre></div>RockSolid Themestag:help.rocksolidthemes.com,2012-07-23:Comment/390394672016-01-29T14:51:43Z2016-01-29T14:51:46ZVerwendung: Options Callback Function with dependency<div><p>Nice.. Works like a Charm! ;)</p>
<p>Thank you.</p></div>Christian Romenitag:help.rocksolidthemes.com,2012-07-23:Comment/390394672017-01-10T13:51:27Z2017-01-10T14:38:11ZVerwendung: Options Callback Function with dependency<div><p>I played around with this callbackfunctions and I'm able to read
out some values of my Custom Element</p>
<pre>
<code>$PlayListElement = explode('__', $dc->field);
if ($PlayListElement[1] !== 'rsce_dummy')
{
$thumb = (json_decode($dc->activeRecord->rsce_data)->playlist[$PlayListElement[1]]->VideoNumber);
}</code>
</pre>
<p>How can I write this String back to another field in my custom
element ?</p></div>Reinertag:help.rocksolidthemes.com,2012-07-23:Comment/390394672017-01-10T14:46:13Z2017-01-10T14:46:13ZVerwendung: Options Callback Function with dependency<div><p>The <code>options_callback</code> 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.</p></div>RockSolid Themestag:help.rocksolidthemes.com,2012-07-23:Comment/390394672017-01-10T15:27:34Z2017-01-11T08:12:31ZVerwendung: Options Callback Function with dependency<div><p>I know, but isn't there a load_callback or input_field_callback.
As far as I know they should do the Job.<br>
My Problem is that I dont know how to return the value,</p>
<p>normally it looks like</p>
<pre>
<code>'thumb' => array(
'label' => array('Videothumbnail', ''),
'inputType' => 'text',
'input_field_callback' => function($dc){return VARIABLE},
),</code>
</pre>
<p>If I return <code>"**"</code> , I can see those <code>**</code>
in my Backend instead the whole thumb field with label, field and
explanation.</p></div>Reinertag:help.rocksolidthemes.com,2012-07-23:Comment/390394672017-01-11T08:15:16Z2017-01-11T08:15:16ZVerwendung: Options Callback Function with dependency<div><p>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: <a href="https://docs.contao.org/books/api/dca/callbacks.html">https://docs.contao.org/books/api/dca/callbacks.html</a></p></div>RockSolid Themestag:help.rocksolidthemes.com,2012-07-23:Comment/390394672021-07-27T12:20:22Z2021-07-27T12:41:55ZVerwendung: Options Callback Function with dependency<div><p>I have a list in a list. Can you show me please the right code snippet for that case?<br></p>
<pre>
<code><?php
return array(
'fields' => array(<br> '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;
},
),
),
),
),
);</code>
</pre>
I tried a load_callback, but it did'nt work.<br>
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?<br>
Thanks in advance.</div>landzonetag:help.rocksolidthemes.com,2012-07-23:Comment/390394672021-07-28T09:19:14Z2021-07-28T09:19:14ZVerwendung: Options Callback Function with dependency<div><blockquote>
<p>After save I have the correct choose.</p>
</blockquote>
<p>As you have set <code>'submitOnChange'=>true</code> on the <code>category</code> field it should not be necessary to manually save the element. Does the browser reload after you change the selected category?</p></div>RockSolid Themestag:help.rocksolidthemes.com,2012-07-23:Comment/390394672021-07-28T11:32:27Z2021-07-28T11:32:30ZVerwendung: Options Callback Function with dependency<div><p>Yes, the browser reload. But then the select-field <code>element</code> shows the <code>unknown option</code>, becouse this is still the choose of the previous category. How can I solve this?</p></div>landzonetag:help.rocksolidthemes.com,2012-07-23:Comment/390394672021-07-29T07:48:50Z2021-07-29T07:48:50ZVerwendung: Options Callback Function with dependency<div><p>In addition to the <code>options_callback</code> you can add a <code>load_callback</code> that checks if the value is valid. The code could look something like this:</p>
<pre>
<code>'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 : '';
}
),</code>
</pre></div>RockSolid Themestag:help.rocksolidthemes.com,2012-07-23:Comment/390394672021-07-29T08:36:08Z2021-07-29T08:36:11ZVerwendung: Options Callback Function with dependency<div><p>Besten Dank, so funktioniert es. Den <code>load_callback</code> hatte ich schon versucht, allerdings war mein Funktionsinhalt falsch.<br>
Nur die Duplizieren-Funktion der einzelnen Elemente funktioniert damit nicht, diese würde ich daher gern ausbelnden.<br>
Dazu hab ich eine eigene Anfrage gestellt: <a href="https://help.rocksolidthemes.com/discussions/contao/67980-verwendung-duplizieren-von-elementen-ausblenden">https://help.rocksolidthemes.com/discussions/contao/67980-verwendun...</a></p></div>landzone