You are here

function opigno_calendar_form_alter in Opigno calendar 8

Same name and namespace in other branches
  1. 3.x opigno_calendar.module \opigno_calendar_form_alter()

Implements hook_form_alter().

File

./opigno_calendar.module, line 183
Contains opigno_calendar.module.

Code

function opigno_calendar_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if (in_array($form_id, [
    'opigno_calendar_event_opigno_calendar_event_form',
    'opigno_calendar_event_opigno_calendar_event_edit_form',
  ])) {
    $current_user = \Drupal::currentUser();
    $uid = $current_user
      ->id();
    $current_user = User::load($uid);

    /** @var \Drupal\Core\Entity\EntityFormInterface $form_object */
    $form_object = $form_state
      ->getFormObject();

    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    $entity = $form_object
      ->getEntity();
    if (!$entity
      ->hasField('field_calendar_event_members')) {
      return;
    }

    // Save in storage current user id.
    $storage = $form_state
      ->getStorage();
    $storage['current_user_id'] = $uid;
    $form_state
      ->setStorage($storage);
    $plugin_service = \Drupal::service('opigno_learning_path.members.manager');
    $plugin_instance = $plugin_service
      ->createInstance('members_plugin');
    $plugin_instance
      ->getMembersForm($form, $form_state, $current_user);
    $form['#validate'][] = 'opigno_calendar_event_opigno_calendar_event_form_validate';
    if ($form_id == 'opigno_calendar_event_opigno_calendar_event_form') {

      // Add redirect handler to form.
      $form['actions']['submit']['#submit'][] = 'opigno_calendar_event_form_submit_handler';
      $members =& $form['members']['field_calendar_event_members'];
      if (!empty($members["widget"]["#options"]) && array_key_exists($uid, $members["widget"]["#options"])) {

        // Remove current user from options.
        unset($members["widget"]["#options"][$uid]);
      }
      $form['#attached']['library'][] = 'opigno_calendar/form';
    }
  }
}