This array inside option_template_censorWords is confusing

TheBigK

Well-known member
I'm totally embarrassed to post so many noob questions here. I'm however refusing to give up on coding. Being shameless and asking questions seems to be my only option.

The following array inside the template that handles censor words is beyond my comprehension.

PHP:
<xen:require js="js/xenforo/options_censor.js" />

<xen:controlunit label="{$preparedOption.title}" hint="{$preparedOption.hint}">
    <xen:explain>{xen:raw $preparedOption.explain}</xen:explain>
    <xen:html>
        <ul>
            <xen:foreach loop="$choices" key="$counter" value="$choice">
            <li>
                <xen:textbox name="{$fieldPrefix}[{$preparedOption.option_id}][{$counter}][word]" value="{$choice.word}" placeholder="{xen:phrase word_or_phrase}" size="20" />
                <xen:textbox name="{$fieldPrefix}[{$preparedOption.option_id}][{$counter}][replace]" value="{$choice.replace}" placeholder="{xen:phrase replacement_optional}" size="20" />
            </li>
            </xen:foreach>
       
            <li class="CensorWordOptionListener">
                <xen:textbox name="{$fieldPrefix}[{$preparedOption.option_id}][{$nextCounter}][word]" value="" placeholder="{xen:phrase word_or_phrase}" size="20" />
                <xen:textbox name="{$fieldPrefix}[{$preparedOption.option_id}][{$nextCounter}][replace]" value="" placeholder="{xen:phrase replacement_optional}" size="20" />
            </li>
        </ul>
       
        <input type="hidden" name="{$listedFieldName}" value="{$preparedOption.option_id}" />
        {xen:raw $editLink}
    </xen:html>
</xen:controlunit>

I've no clue what's happening here:-

1. name="{$fieldPrefix}[{$preparedOption.option_id}][{$counter}][word]"

and similarly for the second input field as well. I looked at the abstract class XenForo_Option_CensorWords to figure out what's going on and what fetches the right values; but with no use.

I'd really appreciate if anyone could spend a few minutes explaining this. And again, I'm sorry for so many questions.
 
You're basically looking at the exact same construct that we had in the other thread.
  • {$fieldPrefix} is used by XenForo. It should default to "xenOptions" or something
  • {$preparedOption.option_id} is the unique ID of the option. I don't know what it is exactly, but likely something like "censored_words" or so
  • {$counter} serves to simply number the fields, so that they're stored back correctly when saving. So does {$nextCounter}
  • word or replace are simply the values at the very end.
You gotta see this as a multidimensional array. It would look somewhat like this in php: $xenOptions['censored_words'] which is an array with all your entries that hold word and replace as values.
 
@katsulynx - I'm still struggling to understand this. I referred to the template user_title_ladder_list and it defines the repeating fields as follows:-

PHP:
<xen:foreach loop="$titles" value="$title">
            <tr class="dataRow">
                <td><input type="checkbox" name="delete[]" value="{$title.minimum_level}" /></td>
                <td><input type="text" class="textCtrl" name="update[{$title.minimum_level}][title]" value="{$title.title}" style="width: 180px" /></td>
                <td><input type="text" class="textCtrl" name="update[{$title.minimum_level}][minimum_level]" value="{$title.minimum_level}" style="width: 30px" /></td>
            </tr>
        </xen:foreach>

It looks like
update[{$title.minimum_level}][title]
has :
1. update : the name of the current action
2. $title.minimum_level -> looks like the array index; but I'm not sure.
3. [title] and [minimum_level] -> the fields which we're updating.

Is that correct?
 
Last edited:
What you see there will be a multidimensional array in php later.

This is what it could look like in php:
PHP:
array(2) {
  [1]=>
  array(2) {
    ["title"]=>
    string(10) "Some Title"
    ["minimum_level"]=>
    int(1)
  }
  [10]=>
  array(2) {
    ["title"]=>
    string(13) "Another Title"
    ["minimum_level"]=>
    int(10)
  }
}
 
@katsulynx - Thank you for your reply. I really appreciate your help. I am familiar with multidimensional arrays. The array structure you wrote does not have 'update', which is what bothers me the most. Where is this 'update' thing coming from and what's its significance?
 
Oh, it's not visible in my print-out. Update is just the name of the array in php. So in XenForo you'd get it by:
PHP:
$xyz = $this->_input->filterSingle('update', XenForo_Input::ARRAY_SIMPLE);
 
gvb
Oh, it's not visible in my print-out. Update is just the name of the array in php. So in XenForo you'd get it by:
PHP:
$xyz = $this->_input->filterSingle('update', XenForo_Input::ARRAY_SIMPLE);
Thanks a lot. I did find that can get that array in my controller using the code you've written. But it looks like it's not used anywhere! The values of the individual array elements are obtained directly.

ActionUpdate() has this:

PHP:
$input = $this->_input->filter(array(
'minimum_level' => XenForo_Input::UINT,
'title' => XenForo_Input::STRING
));
 
Line 41
PHP:
$update = $this->_input->filterSingle('update', XenForo_Input::ARRAY_SIMPLE);

What you quoted is the additional option at the very end, the last field that lets you create a new title. Found in the template as:
HTML:
        <tr class="dataRow">
            <td>{xen:phrase new}:</td>
            <td><input type="text" class="textCtrl" name="title" style="width: 180px" /></td>
            <td><input type="text" class="textCtrl" name="minimum_level" value="0" style="width: 30px" /></td>
        </tr>

That's where "title" and "minimum_level" come from.
 
Back
Top Bottom