1

I want to add a form select input on the user account page / add user page with a dynamic list of options / values.

I know how to get the dynamic list form a database table and create a dynamic list for the select options.

echo $form->select('clientsID', $indexed_array, '0');

Question: Where and how to add this to the account/add user page and save this to the user table?

moonshard
  • 66
  • 1
  • 10

1 Answers1

0

If I understand you well you need a User Attribute. It will be saved automatically in the UserInfo and if you set the options correctly it will be displayed on the user account page / add user page.

Note that all the following code should go in a Controller file in /application/controllers or even better inside a package. How to create a package is well documented here. That way your site can be updated without any problems.

Your associative array with dynamic values from a DB table or other sources to be filled in the User Attribute (a Dropdown in this case):

$options = array(
    'foo' => 'Foo',
    'bar' => 'Bar',
    'baz' => 'Baz'
);

Attributes parameters:

$selectArgs = array(
    'akHandle' => 'my_select',
    'akName' => t('my Select'), // t() is for translation
    'uakProfileDisplay' => true, // Will be displayed on the Users Profile page
    'uakMemberListDisplay' => true, // Will be displayed on the dashboard members page
    'uakProfileEdit' => true, // A memeber is able to edit the attribute
    'uakProfileEditRequired' => true, // The attribute MUST be filled with a value
    'uakRegisterEdit' => true, // Will be displayed on the Register Page
    'uakRegisterEditRequired' => true, // The attribute MUST be filled with a value upon registration
    'akIsSearchableIndexed' => true, // The attribute will be indexed (searchable)
    'akSelectAllowOtherValues' => false, // A user may add/remove other options to the select attribute
    'akSelectOptionDisplayOrder' => 'alpha_asc', // the display order
    'akIsSearchable' => true // one can search by these options
);

Classes used:

use \Concrete\Core\Attribute\Type as AttributeType;
use UserAttributeKey;
use Concrete\Attribute\Select\Option;

Define the Attribute Type:

$attrSelect = AttributeType::getByHandle('select');

Check if the Attribute already exists and if not, create it:

$mySelect = UserAttributeKey::getByHandle($selectArgs['akHandle'])

if(!is_object($mySelect)) {
    UserAttributeKey::add($attrSelect, $selectArgs);
    $mySelect = UserKey::getByHandle($args_address['akHandle']);
}

And finally fill the options into the select:

foreach($options as $value) {
    Option::add($mySelect, t($value));        
}
toesslab
  • 5,092
  • 8
  • 43
  • 62