You are here

public function ILTForm::buildForm in Opigno Instructor-led Trainings 8

Same name and namespace in other branches
  1. 3.x src/Form/ILTForm.php \Drupal\opigno_ilt\Form\ILTForm::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/ILTForm.php, line 68

Class

ILTForm
Provides a form for creating/editing a opigno_ilt entity.

Namespace

Drupal\opigno_ilt\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form = parent::buildForm($form, $form_state);

  /** @var \Drupal\opigno_ilt\ILTInterface $entity */
  $entity = $this->entity;
  if ($entity
    ->getTraining() === NULL) {
    $group = $this
      ->getRequest()
      ->get('group');
    if ($group !== NULL) {
      $group_type = $group
        ->getGroupType()
        ->id();
      if ($group_type === 'learning_path') {

        // If creating entity on a group page, set that group as a related.
        $entity
          ->setTraining($group);
      }
    }
  }
  $form['title'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Title'),
    '#default_value' => $entity
      ->label(),
    '#required' => TRUE,
  ];
  $date_field_def = $entity
    ->getFieldDefinition('date');
  $date_field_item_list = $entity
    ->get('date');
  $date_range_plugin_id = 'opigno_daterange';
  $date_range = new OpignoDateRangeWidget($date_range_plugin_id, $this->pluginManager
    ->getDefinition($date_range_plugin_id), $date_field_def, array_merge(OpignoDateRangeWidget::defaultSettings(), [
    'value_format' => 'Y-m-d H:i:s',
    'value_timezone' => date_default_timezone_get(),
    'value_placeholder' => t('mm/dd/yyyy'),
  ]), []);
  $form['date'] = $date_range
    ->form($date_field_item_list, $form, $form_state);
  $form['place'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Place'),
    '#default_value' => $entity
      ->getPlace(),
    '#placeholder' => $this
      ->t('Enter here the address where the instructor-led training will take place'),
    '#required' => TRUE,
  ];
  $training = $entity
    ->getTraining();
  if ($training !== NULL) {
    $trainer_id = $entity
      ->getTrainerId();
    $trainer_name = '';
    if ($trainer_id) {
      $trainer = \Drupal::entityTypeManager()
        ->getStorage('user')
        ->load($trainer_id);
      if ($trainer) {
        $trainer_name = $trainer
          ->getAccountName();
      }
    }
    $form['trainer'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Trainer'),
      '#default_value' => $trainer_name,
      '#autocomplete_route_name' => 'opigno_ilt.opigno_ilt_trainer_autocomplete',
      '#autocomplete_route_parameters' => [
        'group' => $training
          ->id(),
      ],
      '#placeholder' => $this
        ->t('Enter a user’s name or email'),
    ];
    $form['members_autocomplete'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Members restriction'),
      '#autocomplete_route_name' => 'opigno_ilt.opigno_ilt_members_autocomplete',
      '#autocomplete_route_parameters' => [
        'group' => $training
          ->id(),
      ],
      '#placeholder' => $this
        ->t('Enter a user’s name or email'),
      '#attributes' => [
        'id' => 'members_autocomplete',
      ],
    ];
    $options = [];
    $members = $entity
      ->getMembers();
    foreach ($members as $member) {
      $options['user_' . $member
        ->id()] = $this
        ->t("@name (User #@id)", [
        '@name' => $member
          ->getDisplayName(),
        '@id' => $member
          ->id(),
      ]);
    }
    $form['members'] = [
      '#type' => 'multiselect',
      '#attributes' => [
        'id' => 'members',
        'class' => [
          'row',
        ],
      ],
      '#options' => $options,
      '#default_value' => array_keys($options),
      // Allow modifying option with AJAX.
      '#validated' => TRUE,
      // Fixes multiselect issue 2852654.
      '#process' => [
        [
          'Drupal\\multiselect\\Element\\MultiSelect',
          'processSelect',
        ],
      ],
    ];
  }
  else {
    $form['members'] = [
      '#markup' => $this
        ->t('Instructor-Led Training should have a related training to add a members restriction.'),
    ];
  }
  $form['status_messages'] = [
    '#type' => 'status_messages',
  ];
  $form['#attached']['library'][] = 'multiselect/drupal.multiselect';
  $form['#attached']['library'][] = 'opigno_ilt/form';
  return $form;
}