View source
<?php
namespace Drupal\lti_tool_provider_attributes\Form;
use Drupal;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
class LtiToolProviderAttributesSettingsForm extends ConfigFormBase {
public function getFormId() : string {
return 'lti_tool_provider_attributes_form';
}
public function buildForm(array $form, FormStateInterface $form_state, $filter = '') : array {
$settings = $this
->config('lti_tool_provider_attributes.settings');
$mapped_attributes = $settings
->get('mapped_attributes');
$lti_launch = $this
->config('lti_tool_provider.settings')
->get('lti_launch');
$form['mapped_attributes'] = [
'#type' => 'table',
'#tree' => TRUE,
'#caption' => t('This page allows you to map LTI attrubutes to Drupal user attributes. This is applied every time a user logs in via LTI.'),
'#header' => [
t('User Field'),
t('LTI Attribute'),
],
];
$entityManager = Drupal::service('entity_field.manager');
$userFieldDefinitions = $entityManager
->getFieldDefinitions('user', 'user');
foreach ($userFieldDefinitions as $key => $field) {
$type = $field
->getType();
if ($type === 'string') {
$form['mapped_attributes'][$key] = [
'user_attribute' => [
'#type' => 'item',
'#title' => $field
->getLabel(),
],
'lti_attribute' => [
'#type' => 'select',
'#required' => FALSE,
'#empty_option' => t('None'),
'#empty_value' => TRUE,
'#default_value' => $mapped_attributes[$key],
'#options' => array_combine($lti_launch, $lti_launch),
],
];
}
}
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$settings = $this
->config('lti_tool_provider_attributes.settings');
$lti_launch = $this
->config('lti_tool_provider.settings')
->get('lti_launch');
$mapped_attributes = [];
foreach ($form_state
->getValue('mapped_attributes') as $key => $value) {
if (in_array($value['lti_attribute'], $lti_launch)) {
$mapped_attributes[$key] = $value['lti_attribute'];
}
}
$settings
->set('mapped_attributes', $mapped_attributes)
->save();
}
protected function getEditableConfigNames() : array {
return [
'lti_tool_provider_attributes.settings',
];
}
}