You are here

function _social_album_post_submit in Open Social 10.2.x

Same name and namespace in other branches
  1. 10.3.x modules/social_features/social_album/social_album.module \_social_album_post_submit()
  2. 10.0.x modules/social_features/social_album/social_album.module \_social_album_post_submit()
  3. 10.1.x modules/social_features/social_album/social_album.module \_social_album_post_submit()

Add post to an album and add the album to a group.

Parameters

array $form: An associative array containing the structure of the form.

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

1 string reference to '_social_album_post_submit'
social_album_form_social_post_entity_form_alter in modules/social_features/social_album/social_album.module
Implements hook_form_FORM_ID_alter().

File

modules/social_features/social_album/social_album.module, line 458
The Social Album module.

Code

function _social_album_post_submit(array $form, FormStateInterface $form_state) {
  if ($form_state
    ->hasValue('field_album')) {
    return;
  }
  $input = $form_state
    ->getUserInput()['field_album'];

  /** @var \Drupal\social_post\Entity\PostInterface $post */
  $post = $form_state
    ->getFormObject()
    ->getEntity();
  if ($input['value'] === '_add') {
    if (empty($input['title'])) {
      return;
    }

    // Add default content visibility based on post visibility.
    if ($form_state
      ->hasValue('field_visibility')) {
      $post_visibility = $form_state
        ->getValue([
        'field_visibility',
        0,
        'value',
      ]);
      $default_visibility = SocialAlbumOptionsSelectWidget::VISIBILITY_MAPPING[$post_visibility];
    }
    else {
      $default_visibility = 'community';
    }
    $node = \Drupal::entityTypeManager()
      ->getStorage('node')
      ->create([
      'type' => 'album',
      'title' => $input['title'],
      'field_content_visibility' => $default_visibility,
    ]);
    $node
      ->save();
    \Drupal::service('social_group.set_groups_for_node_service')
      ->addGroupContent($node, $post->field_recipient_group->entity);
    $post
      ->set('field_album', $node
      ->id());
  }
  elseif ($input['value'] !== '_none') {
    $post
      ->set('field_album', $input['value']);
  }
  $post
    ->save();
}