You are here

public static function SocialAlbumOptionsSelectWidget::validateElement in Open Social 10.3.x

Same name and namespace in other branches
  1. 10.0.x modules/social_features/social_album/src/Plugin/Field/FieldWidget/SocialAlbumOptionsSelectWidget.php \Drupal\social_album\Plugin\Field\FieldWidget\SocialAlbumOptionsSelectWidget::validateElement()
  2. 10.1.x modules/social_features/social_album/src/Plugin/Field/FieldWidget/SocialAlbumOptionsSelectWidget.php \Drupal\social_album\Plugin\Field\FieldWidget\SocialAlbumOptionsSelectWidget::validateElement()
  3. 10.2.x modules/social_features/social_album/src/Plugin/Field/FieldWidget/SocialAlbumOptionsSelectWidget.php \Drupal\social_album\Plugin\Field\FieldWidget\SocialAlbumOptionsSelectWidget::validateElement()

Form validation handler for widget elements.

Parameters

array $element: The form element.

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

Overrides OptionsWidgetBase::validateElement

File

modules/social_features/social_album/src/Plugin/Field/FieldWidget/SocialAlbumOptionsSelectWidget.php, line 96

Class

SocialAlbumOptionsSelectWidget
Plugin implementation of the 'social_album_options_select' widget.

Namespace

Drupal\social_album\Plugin\Field\FieldWidget

Code

public static function validateElement(array $element, FormStateInterface $form_state) {
  $field = $element['#parents'][0];
  $has_images = $form_state
    ->hasValue([
    'field_post_image',
    0,
    'fids',
    0,
  ]);
  if ($element['#value'] === '_add' && ($title = $form_state
    ->getValue([
    $field,
    'title',
  ]))) {
    if ($form_state
      ->getTriggeringElement()['#name'] === 'op' && $has_images) {

      // Add default content visibility based on post visibility.
      if ($form_state
        ->hasValue('field_visibility')) {
        $post_visibility = $form_state
          ->getValue([
          'field_visibility',
          0,
        ]);

        // Lets try and map it if possible.
        $default_visibility = self::VISIBILITY_MAPPING[$post_visibility];
      }
      else {
        $default_visibility = 'community';
      }
      $node = \Drupal::entityTypeManager()
        ->getStorage('node')
        ->create([
        'type' => 'album',
        'title' => $title,
        'field_content_visibility' => $default_visibility,
      ]);
      $node
        ->save();
      $element['#value'] = $node
        ->id();
      $form_state
        ->set('album', TRUE);
    }
    else {
      $element['#value'] = '_none';
    }
  }
  elseif ($element['#value'] !== '_none' && !$has_images) {
    $element['#value'] = '_none';
  }
  parent::validateElement($element, $form_state);
  $form_state
    ->setValue($field, $form_state
    ->getValue([
    $field,
    'value',
  ]));
}