News:

SMF 2.1.4 has been released! Take it for a spin! Read more.

Main Menu

Adding multiline text field to theme's options

Started by Orangine, July 14, 2012, 05:20:38 PM

Previous topic - Next topic

Orangine

I want to add Google Analytics code field to my theme, here's my code from Settings.template.php:
      array(
         'id' => 'analytics',
         'label' => $txt['analytics'],
         'description' => $txt['analytics_desc'],
         'type' => 'text',
      ), 

however it's only showing one line, when GA's code is severeal. How do I change it so it would allow me to insert multiline content?   

Arantor

Not without a significant rewrite.

Mind you, you could avoid making it a multi-line edit by looking at what was done in Google Analytics Code.

It's not really something you want to be set per theme, is it? Surely you want to set the code once and have it be run in all themes? (Which that mod does.)

Orangine

Actually that was the plan, as I wanted to use the setting for Analytics and from within the <head> to call some extra code (i.e. jquery), but fair enough I will think about using a mod for this. I'm wondering why it's not possible to use sth like textarea?

Arantor

Because the theme settings code is only built to support what SMF itself uses, which is single-line textboxes, checkboxes and selects.

Also, why do you need a multi-line textbox for things like jQuery? Make it a checkbox or at worst a single-line textbox for stating the version of jQuery.

Orangine

I'm sorry, but you're assuming too much. I want such a functionality and I am not here discussing why or if I need it, I only want to know how to get it.
I know how to make checkboxes, what I don't know is how to make a textarea input. You have already mentioned that it's not something I could easily do and that's where you should stop.

Arantor

Here's the thing: most of the time it's actually not that practical to use such a thing, and in almost every case there is a more elegant and meaningful solution than just using what is considered 'standard'.

I'm not trying to put you off your ideas, but I'm suggesting that maybe you could take a step back and see about what would be more practical for you and anyone who uses what you make. I've made a lot of mods myself for SMF, and from experience it turns out that what is 'more convenient' for development is often not as convenient for users, and vice versa.

Ask yourself why you need a textarea input. What benefit would it *actually* provide? Is it so that you don't have to use something else later?

This is something I come up against a lot on this forum and I'm hoping that I can help you find a good path, too many people here are too fixated on how they want to do something, not what they actually want to do, and that seems to be the case here.

But still, you're telling me to stop, fine.

Gary

Like Arantor said, textarea's aren't available to themers by default. They should, in my opinion, but mostly just for the sake of completion at least. When used properly, anything can have a use. At least that's the way I see it. Others would disagree, which is okay. It's not really something for discussion here.

Anyway, to add textarea support of your own, it's not really that hard. You just need to make a small tweak to Themes.template.php:

Find
elseif ($setting['type'] == 'list')
{
echo '
<dt>
<label for="', $setting['id'], '">', $setting['label'], '</label>:';

if (isset($setting['description']))
echo '<br />
<span class="smalltext">', $setting['description'], '</span>';

echo '
</dt>
<dd>
<select name="', !empty($setting['default']) ? 'default_' : '', 'options[', $setting['id'], ']" id="', $setting['id'], '">';

foreach ($setting['options'] as $value => $label)
echo '
<option value="', $value, '"', $value == $setting['value'] ? ' selected="selected"' : '', '>', $label, '</option>';

echo '
</select>
</dd>';
}


and add after it:

// A Text Area?
elseif ($setting['type'] == 'textarea')
{
echo '
<dt>
                                                <label for="', $setting['id'], '">', $setting['label'], ':</label>';
                                                       
      if (isset($setting['description']))
          {
            echo '
              <br />
<span class="smalltext">', $setting['description'], '</span>';
          }
echo'
            </dt>
            <dd>
              <textarea style="width: 98%;" rows="4" cols="40" name="', !empty($setting['default']) ? 'default_' : '', 'options[', $setting['id'], ']" id="', $setting['id'], '">', (empty($setting['value']) && isset($setting['empty'])) ? $setting['empty'] : $setting['value'], '</textarea>
            </dd>';
}


This way you can define type as textarea in the arrays you're creating in Settings.template.php.

That code was ripped from my own theme, Multi Milk 2 which I added about 50 or so new settings to.
Gary M. Gadsdon
Do NOT PM me unless I say so
War of the Simpsons
Bongo Comics Fan Forum
Youtube Let's Plays

^ YT is changing monetisation policy, help reach 1000 sub threshold.


bloc

Using textarea as a extra input type is something I have done for years in custom themes(even more mixed types lol :D ).

But, there is penalty to that: whenever you update SMF, it get rewritten - if you change the default theme Themes.template.php that is. If you change only the custom theme (which is what I did) it won't transfer to other themes - the textareas will not show if you edit that themes settings when you are in another theme.

If you only use one theme, and have it on always, it works perfectly.

Advertisement: