Adding new fields to Post form

Started by Senkusha, February 21, 2025, 01:38:17 PM

Previous topic - Next topic

Senkusha

I've spent the last three hours searching, and I'm going around in circles.

I want to add a select (dropdown) field, and pre-popluate it with some options.  I've been looking around at some mods, and I've seen the following:

$context['posting_fields']['<name-of-field>'] = [
'label' => [
  'text' => $txt['<customized language text>'],
],
'input' => 'text',
'attributes' => [
...
];

I can't seem to locate any sort of guide that details SMF's mechanic for defining custom fields or what values are appropriate.  Perhaps I'm just not using the correct keywords in my searching?
-- Senkusha
The Kawaii Klub
The Creative Anime Role Playing Community.
(SMF v. 2.1.4, PHP v. 8.0)

Kindred

Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

Senkusha

Ah!  That would explain why I'm running into a dead end.
-- Senkusha
The Kawaii Klub
The Creative Anime Role Playing Community.
(SMF v. 2.1.4, PHP v. 8.0)

Doug Heffernan

Quote from: Senkusha on February 21, 2025, 01:46:13 PMAh!  That would explain why I'm running into a dead end.

I hope you don't take this the wrong way, but to do this you must know php and mysql. You just can't copy/paste code taken from here and there and hope that it works.

mickjav

You'll need to use hooks, The mod below does this how you want but uses a select list you might be able to learn how it's done from it?

https://custom.simplemachines.org/index.php?mod=4038



Senkusha

Quote from: Doug Heffernan on February 21, 2025, 04:06:37 PM
Quote from: Senkusha on February 21, 2025, 01:46:13 PMAh!  That would explain why I'm running into a dead end.

I hope you don't take this the wrong way, but to do this you must know php and mysql. You just can't copy/paste code taken from here and there and hope that it works.
LOL, thanks!  I know enough PHP and MySQL just enough to be dangerous.  Now I'm trying to learn SMF as well.
-- Senkusha
The Kawaii Klub
The Creative Anime Role Playing Community.
(SMF v. 2.1.4, PHP v. 8.0)

live627

The only guide is hidden in the code.            https://github.com/live627/SMF2.1/blob/35154f99a5a2895c6b7e5819a73c4db1d48ed441/Sources/Post.php#L1378-L1499

/* Now let's set up the fields for the posting form header...

Each item in $context['posting_fields'] is an array similar to one of
the following:

$context['posting_fields']['foo'] = array(
'label' => array(
'text' => $txt['foo'], // required
'class' => 'foo', // optional
),
'input' => array(
'type' => 'text', // required
'attributes' => array(
'name' => 'foo', // optional, defaults to posting field's key
'value' => $foo,
'size' => 80,
),
),
);

$context['posting_fields']['bar'] = array(
'label' => array(
'text' => $txt['bar'], // required
'class' => 'bar', // optional
),
'input' => array(
'type' => 'select', // required
'attributes' => array(
'name' => 'bar', // optional, defaults to posting field's key
),
'options' => array(
'option_1' => array(
'label' => $txt['option_1'],
'value' => '1',
'selected' => true,
),
'option_2' => array(
'label' => $txt['option_2'],
'value' => '2',
'selected' => false,
),
'opt_group_1' => array(
'label' => $txt['opt_group_1'],
'options' => array(
'option_3' => array(
'label' => $txt['option_3'],
'value' => '3',
'selected' => false,
),
'option_4' => array(
'label' => $txt['option_4'],
'value' => '4',
'selected' => false,
),
),
),
),
),
);

$context['posting_fields']['baz'] = array(
'label' => array(
'text' => $txt['baz'], // required
'class' => 'baz', // optional
),
'input' => array(
'type' => 'radio_select', // required
'attributes' => array(
'name' => 'baz', // optional, defaults to posting field's key
),
'options' => array(
'option_1' => array(
'label' => $txt['option_1'],
'value' => '1',
'selected' => true,
),
'option_2' => array(
'label' => $txt['option_2'],
'value' => '2',
'selected' => false,
),
),
),
);

The label and input elements are required. The label text and input
type are also required. Other elements may be required or optional
depending on the situation.

The input type can be one of the following:

- text, password, color, date, datetime-local, email, month, number,
  range, tel, time, url, or week
- textarea
- checkbox
- select
- radio_select

When the input type is text (etc.), textarea, or checkbox, the
'attributes' element is used to specify the initial value and any
other HTML attributes that might be necessary for the input field.

When the input type is select or radio_select, the options element
is required in order to list the options that the user can select.
For the select type, these will be used to generate a typical select
menu. For the radio_select type, they will be used to make a div with
some radio buttons in it.

Each option in the options array is itself an array of attributes. If
an option contains a sub-array of more options, then it will be
turned into an optgroup in the generated select menu. Note that the
radio_select type only supports simple options, not grouped ones.

Both the label and the input can have a 'before' and/or 'after'
element. If used, these define literal HTML strings to be inserted
before or after the rest of the content of the label or input.

Finally, it is possible to define an 'html' element for the label
and/or the input. If used, this will override the HTML that would
normally be generated in the template file using the other
information in the array. This should be avoided if at all possible.
*/

I've asked ChatGPT to generate documentation based on that. https://gist.github.com/live627/241cead15fb688097ea1be77b30336b1

# Documentation: Posting Form Fields Configuration

## Overview
This document describes the structure and setup of the `$context['posting_fields']` array, which is used to define fields in a posting form header. Each item in this array represents a field configuration with specific attributes that dictate its behavior and appearance.

## Structure of `$context['posting_fields']`
Each field in `$context['posting_fields']` follows this structure:

```php
$context['posting_fields']['field_name'] = array(
    'label' => array(
        'text' => $txt['field_name'], // Required: Label text
        'class' => 'custom_class',    // Optional: CSS class
    ),
    'input' => array(
        'type' => 'input_type',       // Required: Input type (e.g., text, select, checkbox)
        'attributes' => array(
            'name' => 'field_name',   // Optional: Defaults to field key
            'value' => 'default_value',
            'size' => 80,             // Example attribute
        ),
        'options' => array(           // Required for select and radio_select types
            'option_key' => array(
                'label' => $txt['option_label'],
                'value' => 'option_value',
                'selected' => true,  // Marks the default selected option
            ),
        ),
    ),
);
```

## Field Types and Attributes
### Supported Input Types
The `type` attribute in the `input` array can take one of the following values:

- **Standard HTML Input Types:** `text`, `password`, `color`, `date`, `datetime-local`, `email`, `month`, `number`, `range`, `tel`, `time`, `url`, `week`
- **Other Input Types:** `textarea`, `checkbox`, `select`, `radio_select`

### Attribute Behavior
- **Text, Textarea, Checkbox:** Utilize the `attributes` element to define properties such as `value`, `size`, and other HTML attributes.
- **Select and Radio_Select:** Require an `options` element to define available choices.
- **Select:** Generates a dropdown menu, supports grouped options.
- **Radio_Select:** Creates a group of radio buttons, does not support grouped options.
- **Subject Field:** The `subject` field is mandatory and must always be present in the form.

## Example Field Configurations
### Text Input Field
```php
$context['posting_fields']['foo'] = array(
    'label' => array(
        'text' => $txt['foo'],
        'class' => 'foo',
    ),
    'input' => array(
        'type' => 'text',
        'attributes' => array(
            'name' => 'foo',
            'value' => $foo,
            'size' => 80,
        ),
    ),
);
```

### Select Dropdown Field
```php
$context['posting_fields']['bar'] = array(
    'label' => array(
        'text' => $txt['bar'],
        'class' => 'bar',
    ),
    'input' => array(
        'type' => 'select',
        'attributes' => array(
            'name' => 'bar',
        ),
        'options' => array(
            'option_1' => array(
                'label' => $txt['option_1'],
                'value' => '1',
                'selected' => true,
            ),
            'opt_group_1' => array(
                'label' => $txt['opt_group_1'],
                'options' => array(
                    'option_3' => array(
                        'label' => $txt['option_3'],
                        'value' => '3',
                    ),
                ),
            ),
        ),
    ),
);
```

### Radio Button Field
```php
$context['posting_fields']['baz'] = array(
    'label' => array(
        'text' => $txt['baz'],
        'class' => 'baz',
    ),
    'input' => array(
        'type' => 'radio_select',
        'attributes' => array(
            'name' => 'baz',
        ),
        'options' => array(
            'option_1' => array(
                'label' => $txt['option_1'],
                'value' => '1',
                'selected' => true,
            ),
            'option_2' => array(
                'label' => $txt['option_2'],
                'value' => '2',
            ),
        ),
    ),
);
```

## Additional Customization
### Pre/Post Elements
- **`before` and `after` attributes** allow inserting custom HTML before or after the label/input.

### Overriding Default HTML
- **`html` attribute** can be used to define custom HTML, but it is recommended to avoid this for maintainability, as it overrides default rendering.

Senkusha

I never even thought to ask Chat GPT to parse through the code to create documentation!  Genius!!  Thank you!  This gives me the information I need, I think.
-- Senkusha
The Kawaii Klub
The Creative Anime Role Playing Community.
(SMF v. 2.1.4, PHP v. 8.0)

Sesquipedalian

Quote from: live627 on February 22, 2025, 07:34:53 PM- **Standard HTML Input Types:** `text`, `password`, `color`, `date`, `datetime-local`, `email`, `month`, `number`, `range`, `tel`, `time`, `url`, `week`
- **Other Input Types:** `textarea`, `checkbox`, `select`, `radio_select`

As usual, AI doesn't understand what it is reading. ::)

I recommend following the original documentation text, not the AI generated version.
I promise you nothing.

Sesqu... Sesqui... what?
Sesquipedalian, the best word in the English language.

Diego Andrés

Quote from: Senkusha on February 22, 2025, 09:04:30 PMI never even thought to ask Chat GPT to parse through the code to create documentation!  Genius!!  Thank you!  This gives me the information I need, I think.

Just proof read the replies, you never know when it's going to hallucinate. You first need to know what you're doing  :P

SMF Tricks - Free & Premium Responsive Themes for SMF.

Senkusha

Quote from: Sesquipedalian on February 22, 2025, 09:06:18 PM
Quote from: live627 on February 22, 2025, 07:34:53 PM- **Standard HTML Input Types:** `text`, `password`, `color`, `date`, `datetime-local`, `email`, `month`, `number`, `range`, `tel`, `time`, `url`, `week`
- **Other Input Types:** `textarea`, `checkbox`, `select`, `radio_select`

As usual, AI doesn't understand what it is reading. ::)

I recommend following the original documentation text, not the AI generated version.
Where did you find this?  Now I know I missed something as I was searching, and I'm gonna be upset if it was under my nose the entire time!  (There is a reason why I wear eyeglasses--bifocals even!)
-- Senkusha
The Kawaii Klub
The Creative Anime Role Playing Community.
(SMF v. 2.1.4, PHP v. 8.0)

Diego Andrés

Post.php
It doesn't have official developer documentation, hence why live suggested mixing the code blocks in the AI blender.

SMF Tricks - Free & Premium Responsive Themes for SMF.

live627

it's in my post, link to github or expand the first code block

Advertisement: