You are here

function social_event_form_alter in Open Social 8.9

Same name and namespace in other branches
  1. 8.3 modules/social_features/social_event/social_event.module \social_event_form_alter()
  2. 8.4 modules/social_features/social_event/social_event.module \social_event_form_alter()
  3. 8.5 modules/social_features/social_event/social_event.module \social_event_form_alter()
  4. 8.6 modules/social_features/social_event/social_event.module \social_event_form_alter()
  5. 8.7 modules/social_features/social_event/social_event.module \social_event_form_alter()
  6. 8.8 modules/social_features/social_event/social_event.module \social_event_form_alter()
  7. 10.3.x modules/social_features/social_event/social_event.module \social_event_form_alter()
  8. 10.0.x modules/social_features/social_event/social_event.module \social_event_form_alter()
  9. 10.1.x modules/social_features/social_event/social_event.module \social_event_form_alter()
  10. 10.2.x modules/social_features/social_event/social_event.module \social_event_form_alter()

Implements hook_form_alter().

File

modules/social_features/social_event/social_event.module, line 622
The Social event module.

Code

function social_event_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  // Form alters for the event add and edit forms.
  if ($form_id === 'node_event_edit_form' || $form_id === 'node_event_form') {

    // Hide the default checkbox in favor of new enrollment options.
    $form['field_event_enroll']['#attributes']['class'][] = 'hidden';
    $form['field_event_enroll']['#states'] = [
      'unchecked' => [
        ':input[name="field_enroll_method"]' => [
          'value' => '3',
        ],
      ],
    ];

    // Alter visibility of enrollment methods.
    social_event_field_visibility($form, 'field_event_enroll', [
      'field_event_enroll_outside_group',
    ]);
    $form['field_event_enroll_outside_group']['#states'] = [
      'visible' => [
        ':input[name="groups"]' => [
          '!value' => '_none',
        ],
        ':input[name="field_enroll_method"]' => [
          '!value' => '3',
        ],
      ],
    ];

    // Hide invite enroll method if the submodule is not enabled.
    $moduleHandler = \Drupal::service('module_handler');
    if (!$moduleHandler
      ->moduleExists('social_event_invite')) {
      unset($form['field_enroll_method']['widget']['#options'][3]);
    }

    // Add the all day event checkbox.
    $form['event_all_day'] = [
      '#type' => 'checkbox',
      '#title' => t('All day'),
    ];

    // Set default value and fieldgroup for all day checkbox.
    if ($date = $form['field_event_date']['widget'][0]['value']['#default_value']) {
      $all_day_value = $date instanceof DrupalDateTime && social_event_date_is_all_day($date);
      $form['event_all_day']['#default_value'] = $all_day_value;
    }

    // Get user timezone.
    $user_timezone = _social_event_user_timezone(date_default_timezone_get());
    $timezone_label = t('Time zone');
    $markup = '<div class="control-label margin-bottom-s">' . $timezone_label . '</div>';
    $markup .= '<div class="bg-gray-lightest btn btn-lg">' . $user_timezone . '</div>';

    // Check if user has access to change his timezone.
    $date_config = Drupal::config('system.date');
    if ($date_config
      ->get('timezone.user.configurable')) {
      $description = t('The event date and time are set based on your timezone. @change_timezone.', [
        '@change_timezone' => Link::createFromRoute(t('Change your timezone here'), 'entity.user.edit_form', [
          'user' => \Drupal::currentUser()
            ->id(),
        ], [
          'attributes' => [
            'target' => '_blank',
          ],
          'fragment' => 'edit-group-locale-settings',
        ])
          ->toString(),
      ]);
    }
    else {
      $description = t("The event date and time are set based on the site's default timezone.");
    }
    $form['timezone_indication'] = [
      '#type' => 'container',
      '#weight' => 99,
      'content' => [
        '#type' => 'item',
        '#markup' => $markup,
        '#description' => $description,
      ],
    ];
    $form['#fieldgroups']['group_date_time']->children[] = 'event_all_day';
    $form['#fieldgroups']['group_date_time']->children[] = 'timezone_indication';
    $form['#group_children']['event_all_day'] = 'group_date_time';
    $form['#group_children']['timezone_indication'] = 'group_date_time';
    $form['#after_build'][] = 'social_event_date_after_build';

    // Attach styling for the event date fields.
    $form['#attached']['library'][] = 'social_event/admin';
  }

  // Specific alter for the event edit form.
  if ($form_id === 'node_event_edit_form') {

    // Set allow event enrollment checked by default for existing events.
    $entity = $form_state
      ->getFormObject()
      ->getEntity();
    if ($entity
      ->hasField('field_event_enroll') && $entity->field_event_enroll
      ->isEmpty()) {
      $form['field_event_enroll']['widget']['value']['#default_value'] = TRUE;
    }
  }

  // Alters for the Enroll Action form.
  if ($form_id === 'enroll_action_form') {
    $node = \Drupal::routeMatch()
      ->getParameter('node');
    if (!$node instanceof NodeInterface) {
      $node = \Drupal::entityTypeManager()
        ->getStorage('node')
        ->load($node);
    }
    if ($node instanceof NodeInterface) {
      $form['enroll_for_this_event']['#access'] = \Drupal::service('social_event.enroll')
        ->isEnabled($node);
    }
  }
}