You are here

public function MeetingForm::buildForm in Opigno Moxtra 8

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

Class

MeetingForm
Provides a form for creating/editing a opigno_moxtra_meeting entity.

Namespace

Drupal\opigno_moxtra\Form

Code

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

  /** @var \Drupal\opigno_moxtra\MeetingInterface $entity */
  $entity = $this->entity;
  $owner_id = $entity
    ->getOwnerId();
  $session_key = $entity
    ->getSessionKey();
  if (!empty($session_key)) {
    $info = $this->moxtraService
      ->getMeetingInfo($owner_id, $session_key);
    $status = !empty($info['data']) ? $info['data']['status'] : FALSE;
    if (!empty($status) && $status !== 'SESSION_SCHEDULED') {
      $form[] = [
        '#markup' => $this
          ->t('You can edit only a scheduled live meeting.'),
      ];
      return $form;
    }
  }
  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);
  $training = $entity
    ->getTraining();
  if ($training !== NULL) {
    $form['members_autocomplete'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Members restriction'),
      '#autocomplete_route_name' => 'opigno_moxtra.meeting_members_autocomplete',
      '#autocomplete_route_parameters' => [
        'group' => $training
          ->id(),
      ],
      '#placeholder' => $this
        ->t('Enter a user’s name or email'),
      '#attributes' => [
        'id' => 'meeting_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' => 'meeting_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('Live Meeting 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_moxtra/meeting_form';
  return $form;
}