You are here

public function WorkspaceForm::buildForm in Opigno Moxtra 3.x

Same name and namespace in other branches
  1. 8 src/Form/WorkspaceForm.php \Drupal\opigno_moxtra\Form\WorkspaceForm::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 EntityForm::buildForm

File

src/Form/WorkspaceForm.php, line 66

Class

WorkspaceForm
Provides a form for creating/editing a opigno_moxtra_workspace entity.

Namespace

Drupal\opigno_moxtra\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $training = NULL) {

  /** @var \Drupal\opigno_moxtra\WorkspaceInterface $entity */
  $entity = $this->entity;
  $form = parent::buildForm($form, $form_state);

  // Get group of workspace.
  $workspace_id = $entity
    ->id();
  $query = \Drupal::entityQuery('group')
    ->condition('field_workspace', $workspace_id);
  $result = $query
    ->execute();
  if (!empty($result)) {
    $group_id = array_values($result)[0];
  }
  else {
    $group_id = NULL;
  }
  $form['name'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Collaborative workspace name'),
    '#default_value' => $entity
      ->label(),
    '#required' => TRUE,
  ];
  if (!$entity
    ->isNew()) {
    $form['binder_id'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Binder ID'),
      '#default_value' => $entity
        ->getBinderId(),
      '#attributes' => [
        'disabled' => TRUE,
      ],
    ];
    $current_members = $entity
      ->getMembers();
    $default_options = [];
    foreach ($current_members as $current_member) {
      $default_options[$current_member
        ->id()] = $current_member
        ->getAccountName();
    }
    $available_options = $default_options;
    if (!empty($group_id)) {

      /** @var \Drupal\group\Entity\Group $group */
      $group = Group::load($group_id);
      $group_members = $group
        ->getMembers();
      $autocomplete_route_name = 'opigno_moxtra.membership.add_user_to_group_collaborative_workspace';
      $autocomplete_route_parameters = [
        'group' => $group
          ->id(),
        'workspace' => $workspace_id,
      ];
      $form['auto_register'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Automatically register all users of that training'),
        '#default_value' => $entity
          ->getAutoRegister(),
      ];
      foreach ($group_members as $group_member) {
        $group_user = $group_member
          ->getUser();
        if (_opigno_moxtra_is_user_enabled($group_user)) {
          $available_options[$group_user
            ->id()] = $group_user
            ->getAccountName();
        }
      }
      $states = [
        'invisible' => [
          ':input[name="auto_register"]' => [
            'checked' => TRUE,
          ],
        ],
      ];
    }
    else {
      $autocomplete_route_name = 'opigno_moxtra.membership.add_user_to_all_collaborative_workspace';
      $autocomplete_route_parameters = [
        'workspace' => $workspace_id,
      ];
      $users = User::loadMultiple();
      foreach ($users as $user) {
        if (_opigno_moxtra_is_user_enabled($user)) {
          $available_options[$user
            ->id()] = $user
            ->getAccountName();
        }
      }

      // Remove Anonymous user.
      unset($available_options[0]);
      $states = [];
    }
    $form['training_users_autocomplete'] = [
      '#type' => 'textfield',
      '#title' => t('Find existing users of this group'),
      '#autocomplete_route_name' => $autocomplete_route_name,
      '#autocomplete_route_parameters' => $autocomplete_route_parameters,
      '#placeholder' => t('Enter a user’s name or email'),
      '#attributes' => [
        'id' => 'training_users_autocomplete',
      ],
      '#states' => $states,
    ];
    $form['training_users'] = [
      '#type' => 'multiselect',
      '#title' => $this
        ->t('Select users:'),
      '#attributes' => [
        'id' => 'training_users',
        'class' => [
          'row',
        ],
      ],
      '#options' => $available_options,
      '#default_value' => array_keys($default_options),
      '#validated' => TRUE,
      '#process' => [
        [
          'Drupal\\multiselect\\Element\\MultiSelect',
          'processSelect',
        ],
      ],
      '#states' => $states,
    ];
    $form['#attached']['library'][] = 'opigno_learning_path/member_add';
  }
  if ($training) {
    $form_state
      ->set('training', $training);
  }
  return $form;
}