You are here

protected function PostForm::configureVisibilityField in Open Social 8

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_post/src/Form/PostForm.php \Drupal\social_post\Form\PostForm::configureVisibilityField()
  2. 8.2 modules/social_features/social_post/src/Form/PostForm.php \Drupal\social_post\Form\PostForm::configureVisibilityField()
  3. 8.3 modules/social_features/social_post/src/Form/PostForm.php \Drupal\social_post\Form\PostForm::configureVisibilityField()
  4. 8.4 modules/social_features/social_post/src/Form/PostForm.php \Drupal\social_post\Form\PostForm::configureVisibilityField()
  5. 8.5 modules/social_features/social_post/src/Form/PostForm.php \Drupal\social_post\Form\PostForm::configureVisibilityField()
  6. 8.6 modules/social_features/social_post/src/Form/PostForm.php \Drupal\social_post\Form\PostForm::configureVisibilityField()
  7. 8.7 modules/social_features/social_post/src/Form/PostForm.php \Drupal\social_post\Form\PostForm::configureVisibilityField()
  8. 8.8 modules/social_features/social_post/src/Form/PostForm.php \Drupal\social_post\Form\PostForm::configureVisibilityField()
  9. 10.3.x modules/social_features/social_post/src/Form/PostForm.php \Drupal\social_post\Form\PostForm::configureVisibilityField()
  10. 10.0.x modules/social_features/social_post/src/Form/PostForm.php \Drupal\social_post\Form\PostForm::configureVisibilityField()
  11. 10.1.x modules/social_features/social_post/src/Form/PostForm.php \Drupal\social_post\Form\PostForm::configureVisibilityField()
  12. 10.2.x modules/social_features/social_post/src/Form/PostForm.php \Drupal\social_post\Form\PostForm::configureVisibilityField()

Configures the allows values for the visiblity field.

Uses whether this is a new entity or an existing entity and which view mode is used to configure the allowed values of the visibility field. This is different for posts made to a profile or in a group.

When editing a visibility is shown but can not be altered.

For the meaning of the numbers in this function check the field definition in the field.storage.post.field_visibility configuration object.

Parameters

array $form: The post form being rendered.

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

1 call to PostForm::configureVisibilityField()
PostForm::buildForm in modules/social_features/social_post/src/Form/PostForm.php
Form constructor.

File

modules/social_features/social_post/src/Form/PostForm.php, line 98

Class

PostForm
Form controller for Post edit forms.

Namespace

Drupal\social_post\Form

Code

protected function configureVisibilityField(array &$form, FormStateInterface $form_state) {
  $form['#attached']['library'][] = 'social_post/visibility-settings';

  // Default is create/add mode.
  $form['field_visibility']['widget'][0]['#edit_mode'] = FALSE;
  $display_id = $this
    ->getFormDisplay($form_state)
    ->id();

  // For the explanation of the numbers see
  // field.storage.post.field_visibility.
  if ($display_id === $this->postViewDefault) {

    // Set default value to community.
    unset($form['field_visibility']['widget'][0]['#options'][0]);
    if (isset($form['field_visibility']['widget'][0]['#default_value'])) {
      $default_value = $form['field_visibility']['widget'][0]['#default_value'];
      if ((string) $default_value !== '1') {
        $form['field_visibility']['widget'][0]['#default_value'] = '2';
      }
    }
    else {
      $form['field_visibility']['widget'][0]['#default_value'] = '2';
    }
    unset($form['field_visibility']['widget'][0]['#options'][3]);
  }
  else {
    $form['field_visibility']['widget'][0]['#default_value'] = "0";
    unset($form['field_visibility']['widget'][0]['#options'][2]);
    $current_group = NULL;
    if ($this->operation === 'edit' && $this->entity
      ->hasField('field_recipient_group') && !$this->entity
      ->get('field_recipient_group')
      ->isEmpty()) {
      $current_group = $this->entity
        ->get('field_recipient_group')
        ->first()
        ->get('entity')
        ->getTarget()
        ->getValue();
    }
    else {
      $current_group = _social_group_get_current_group();
    }

    // We unset the group visibility if we don't have a group.
    if (empty($current_group)) {
      unset($form['field_visibility']['widget'][0]['#options'][3]);
    }
    else {
      $group_type_id = $current_group
        ->getGroupType()
        ->id();
      $allowed_options = social_group_get_allowed_visibility_options_per_group_type($group_type_id);
      if ($allowed_options['community'] !== TRUE) {
        unset($form['field_visibility']['widget'][0]['#options'][0]);
      }
      if ($allowed_options['public'] !== TRUE) {
        unset($form['field_visibility']['widget'][0]['#options'][1]);
      }
      else {
        $form['field_visibility']['widget'][0]['#default_value'] = "1";
      }
      if ($allowed_options['group'] !== TRUE) {
        unset($form['field_visibility']['widget'][0]['#options'][3]);
      }
      else {
        $form['field_visibility']['widget'][0]['#default_value'] = "3";
      }
    }
  }

  // When a post is being edited we configure the visibility to be shown as a
  // read-only value.
  if ($this->operation == 'edit') {

    /** @var \Drupal\social_post\Entity\Post $post */
    $post = $this->entity;
    $form['#post_id'] = $post
      ->id();

    // In edit mode we don't want people to actually change visibility
    // setting of the post.
    if ($current_value = $this->entity
      ->get('field_visibility')->value) {

      // We set the default value.
      $form['field_visibility']['widget'][0]['#default_value'] = $current_value;
    }

    // Unset the other options, because we do not want to be able to change
    // it but we do want to use the button for informing the user.
    foreach ($form['field_visibility']['widget'][0]['#options'] as $key => $option) {
      if ($option['value'] != $form['field_visibility']['widget'][0]['#default_value']) {
        unset($form['field_visibility']['widget'][0]['#options'][$key]);
      }
    }

    // Set button to disabled in our template, users have no option anyway.
    $form['field_visibility']['widget'][0]['#edit_mode'] = TRUE;
  }
}