How to add custom user attribute programmatically from package

Permalink
Updated: problem solved with this code.
Adding "Membership Type" custom user attribute and populate this with 2 options (Publisher and Reader). Also, this attr is set as required on register.

public static function installUserMembershipTypeKey($pkg)
    {
        $app = \Concrete\Core\Support\Facade\Application::getFacadeApplication();
        $service = $app->make('Concrete\Core\Attribute\Category\CategoryService');
        $categoryEntity = $service->getByHandle('user');
        $category = $categoryEntity->getController();
        $key = $category->getAttributeKeyByHandle('membership_type');
        if (!is_object($key)) {
            $key = new UserKey();
            $key->setAttributeKeyHandle('membership_type');
            $key->setAttributeKeyName('Membership Type');
            $key->setIsAttributeKeySearchable(false);
            $key->setAttributeKeyEditableOnRegister(true);
            $key = $category->add('select', $key, self::setSelectSettings(['Publisher', 'Reader']) , $pkg);
        }


Regards, Nick

brutalnv
 
hutman replied on at Permalink Reply
hutman
The easiest way to do this is going to be using the CIF stuff, have a look at the documentation here:

https://documentation.concrete5.org/developers/packages/concrete5-ci...

https://documentation.concrete5.org/developers/appendix/concrete5-ci...
brutalnv replied on at Permalink Best Answer Reply
brutalnv
Thank you! I think that the solution you proposed is the best way.
dbuerer replied on at Permalink Reply
this was helpful today thank you
jasteele12 replied on at Permalink Reply
jasteele12
It seems the first URL has strangely changed to:

https://documentation.concrete5.org/developers/packages/concrete5-ci...

Note the '-1' ad the end. The original returns a page forbidden after logging in :(