Last active
October 11, 2017 19:48
-
-
Save iboved/1c41dcebf7ed94eae61e5105e112ec24 to your computer and use it in GitHub Desktop.
Extend form fields with Yaml Parser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Vitae\Vita; | |
use System\Classes\PluginBase; | |
use Backend\Models\User as BackendUserModel; | |
use Backend\Controllers\Users as BackendUsersController; | |
use Vitae\Vita\Controllers\MyAccount; | |
use Yaml; | |
use Event; | |
class Plugin extends PluginBase | |
{ | |
public function boot() | |
{ | |
BackendUserModel::extend(function ($model) | |
{ | |
$model->addDynamicMethod('getCountryOptions', function() use ($model) { | |
return [ | |
1 => 'Ukraine', | |
2 => 'Germany', | |
]; | |
}); | |
$model->addDynamicMethod('getFamilystatusOptions', function() use ($model) { | |
return [ | |
1 => 'Married', | |
2 => 'Single', | |
]; | |
}); | |
$model->addDynamicMethod('getDenominationOptions', function() use ($model) { | |
return [ | |
1 => 'One', | |
2 => 'Two', | |
]; | |
}); | |
}); | |
Event::listen('backend.form.extendFields', function ($widget) { | |
if (!$widget->getController() instanceof BackendUsersController && !$widget->getController() instanceof MyAccount) { | |
return; | |
} | |
if (!$widget->model instanceof BackendUserModel) { | |
return; | |
} | |
// Ensures that Profile always exists | |
ProfileModel::getFromUser($widget->model); | |
$configFile = Yaml::parseFile(__DIR__ . '/models/profile/fields.yaml'); | |
$widget->addTabFields($configFile); | |
$widget->removeTab('backend::lang.user.groups'); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment