You are here

function social_group_flexible_group_flexible_group_add_after_build in Open Social 10.3.x

Same name and namespace in other branches
  1. 10.0.x modules/social_features/social_group/modules/social_group_flexible_group/social_group_flexible_group.module \social_group_flexible_group_flexible_group_add_after_build()
  2. 10.1.x modules/social_features/social_group/modules/social_group_flexible_group/social_group_flexible_group.module \social_group_flexible_group_flexible_group_add_after_build()
  3. 10.2.x modules/social_features/social_group/modules/social_group_flexible_group/social_group_flexible_group.module \social_group_flexible_group_flexible_group_add_after_build()

After_build function for the flexible group add form to add default states.

Parameters

array $form: The form element.

\Drupal\Core\Form\FormStateInterface $form_state: The form state.

Return value

mixed The form array containing the default values & states.

1 string reference to 'social_group_flexible_group_flexible_group_add_after_build'
social_group_flexible_group_form_alter in modules/social_features/social_group/modules/social_group_flexible_group/social_group_flexible_group.module
Implements hook_form_alter().

File

modules/social_features/social_group/modules/social_group_flexible_group/social_group_flexible_group.module, line 222
The Social Group Flexible Group module.

Code

function social_group_flexible_group_flexible_group_add_after_build(array $form, FormStateInterface $form_state) {
  $group_types = [
    'flexible_group',
  ];
  \Drupal::moduleHandler()
    ->alter('social_group_settings', $group_types);
  foreach ($group_types as $group_type) {
    $form_ids[] = 'group-' . str_replace('_', '-', $group_type) . '-add-form';
  }

  // Add states for content visibility based on group visibility.
  // Only on the add page. On edit we want users to be able to consciously
  // decide what allowed visibility to select.
  if (in_array($form['#id'], $form_ids ?? []) && isset($form['field_flexible_group_visibility']['widget']['#options'], $form['field_group_allowed_visibility']['widget']['#options'])) {

    // If group visibility is public. All content visibility is selected.
    $form['field_group_allowed_visibility']['widget']['public']['#states'] = [
      'checked' => [
        ':input[name="field_flexible_group_visibility"]' => [
          [
            'value' => 'public',
          ],
        ],
      ],
      'unchecked' => [
        ':input[name="field_flexible_group_visibility"]' => [
          [
            'value' => 'community',
          ],
          [
            'value' => 'members',
          ],
        ],
      ],
    ];

    // If group visibility is community. Communit & Group members are selected.
    $form['field_group_allowed_visibility']['widget']['community']['#states'] = [
      'checked' => [
        ':input[name="field_flexible_group_visibility"]' => [
          [
            'value' => 'public',
          ],
          [
            'value' => 'community',
          ],
        ],
      ],
      'unchecked' => [
        ':input[name="field_flexible_group_visibility"]' => [
          [
            'value' => 'members',
          ],
        ],
      ],
    ];

    // If group visibility is group. Only group members are selected.
    $form['field_group_allowed_visibility']['widget']['group']['#states'] = [
      'checked' => [
        ':input[name="field_flexible_group_visibility"]' => [
          [
            'value' => 'public',
          ],
          [
            'value' => 'community',
          ],
          [
            'value' => 'members',
          ],
        ],
      ],
    ];
  }

  // Add states for join method based on group visibility.
  // We do this for add and edit, we want to make sure if users make the
  // decision to choose to only show a group to it's Members, the
  // join method is selected to Invite only. Because there is no way
  // for users to join or request to join in that case.
  if (isset($form['field_flexible_group_visibility']['widget']['#options'], $form['field_group_allowed_join_method']['widget']['#options'])) {

    // If group visibility is members. Select invite-only.
    if (!empty($form['field_group_allowed_join_method']['widget']['added'])) {
      $form['field_group_allowed_join_method']['widget']['added']['#states'] = [
        'checked' => [
          ':input[name="field_flexible_group_visibility"]' => [
            [
              'value' => 'members',
            ],
          ],
        ],
      ];
    }

    // If group visibility is members. Disable and uncheck open to join.
    if (!empty($form['field_group_allowed_join_method']['widget']['direct'])) {
      $form['field_group_allowed_join_method']['widget']['direct']['#states'] = [
        'disabled' => [
          ':input[name="field_flexible_group_visibility"]' => [
            [
              'value' => 'members',
            ],
          ],
        ],
        'unchecked' => [
          ':input[name="field_flexible_group_visibility"]' => [
            [
              'value' => 'members',
            ],
          ],
        ],
      ];
    }

    // If group visibility is members. Disable and uncheck open to join.
    if (!empty($form['field_group_allowed_join_method']['widget']['request'])) {
      $form['field_group_allowed_join_method']['widget']['request']['#states'] = [
        'disabled' => [
          ':input[name="field_flexible_group_visibility"]' => [
            [
              'value' => 'members',
            ],
          ],
        ],
        'unchecked' => [
          ':input[name="field_flexible_group_visibility"]' => [
            [
              'value' => 'members',
            ],
          ],
        ],
      ];
    }
  }
  return $form;
}