5

Within a plugin I'm working on, I'll be adding user synchronisation with a third-party database to enable SSO, so will be creating users within Craft based on an interaction from this other service.

I'd also like to associate the user ID from the other service against the Craft User model. Obviously you can create custom fields from within the CP - but is there a way for a plugin to automatically add a custom field so the admin doesn't need to do it themselves?

6

First of all, you will need the Pro package to be able to customise the user profile.

Then you would have to use the onAfterInstall and onBeforeUninstall events in your plugin file. These events are called when installing and uninstalling the plugin.

After the install, you need to create a new field and add it to the user field layout. And before uninstall, you have to remove the field from the layout and delete the field itself.

The code below should work, but it doesn't keep the required fields (I didn't find a way to get them using the available functions).

<?php

namespace Craft;

class UserfieldPlugin extends BasePlugin
{
    public function getName()
    {
        return Craft::t('Userfield');
    }

    public function getVersion()
    {
        return '0.9.1';
    }

    public function getDeveloper()
    {
        return 'Mario';
    }

    public function getDeveloperUrl()
    {
        return 'http://craftshake.com';
    }

    public function onAfterInstall()
    {
        // Create the third party field

        $thirdPartyField = new FieldModel();
        $thirdPartyField->groupId      = 1;
        $thirdPartyField->name         = Craft::t('Third Party');
        $thirdPartyField->handle       = 'thirdParty';
        $thirdPartyField->translatable = false;
        $thirdPartyField->type         = 'PlainText';

        craft()->fields->saveField($thirdPartyField);

        // Create the new user field layout

        $fieldLayout = craft()->fields->getLayoutByType(ElementType::User);
        $fieldsIds = $fieldLayout->getFieldIds();
        $fieldsIds[] = $thirdPartyField->id;

        craft()->fields->deleteLayoutsByType(ElementType::User);

        $fieldLayout = craft()->fields->assembleLayout(
            array(
                Craft::t('Profile') => $fieldsIds,
            ),
            array(),
            false
        );
        $fieldLayout->type = ElementType::User;

        craft()->fields->saveLayout($fieldLayout, false);
    }

    public function onBeforeUninstall()
    {
        // Get third party field
        $thirdPartyField = craft()->fields->getFieldByHandle('thirdParty');

        // Remove field from layout
        $fieldLayout = craft()->fields->getLayoutByType(ElementType::User);
        $fieldsIds = $fieldLayout->getFieldIds();
        $fieldsIds = array_diff($fieldsIds, array($thirdPartyField->id));

        craft()->fields->deleteLayoutsByType(ElementType::User);

        $fieldLayout = craft()->fields->assembleLayout(
            array(
                Craft::t('Profile') => $fieldsIds,
            ),
            array(),
            false
        );
        $fieldLayout->type = ElementType::User;

        craft()->fields->saveLayout($fieldLayout, false);

        // Delete field
        craft()->fields->deleteField($thirdPartyField);
    }

}

I hope this helps.

Mario

| improve this answer | |
  • Mario - that's great, thank you! I'm assuming this would then display as a field within the layout for users that a CP admin could then delete if they wanted to? If possible I'd like to make it a hidden field, or at least one they couldn't remove and was hoping I could somehow extend the User model - but this approach would work still – Adam Jul 6 '14 at 3:52
  • Indeed this adds a field to the user field layout and it could be remove by admin. To hide a field or extend User, you would have to ask @brandon-kelly or Brad Bell if this is possible. – Mario Jul 6 '14 at 12:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.