You are here

public function LtiToolProviderAttributesSettingsForm::buildForm in LTI Tool Provider 8

Same name and namespace in other branches
  1. 2.x modules/lti_tool_provider_attributes/src/Form/LtiToolProviderAttributesSettingsForm.php \Drupal\lti_tool_provider_attributes\Form\LtiToolProviderAttributesSettingsForm::buildForm()

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides ConfigFormBase::buildForm

File

modules/lti_tool_provider_attributes/src/Form/LtiToolProviderAttributesSettingsForm.php, line 30

Class

LtiToolProviderAttributesSettingsForm

Namespace

Drupal\lti_tool_provider_attributes\Form

Code

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'),
    ],
  ];

  /* @var $entityManager Drupal\Core\Entity\EntityFieldManagerInterface */
  $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);
}