You are here

public function SocialAlbumImageForm::submitForm in Open Social 10.3.x

Same name and namespace in other branches
  1. 10.0.x modules/social_features/social_album/src/Form/SocialAlbumImageForm.php \Drupal\social_album\Form\SocialAlbumImageForm::submitForm()
  2. 10.1.x modules/social_features/social_album/src/Form/SocialAlbumImageForm.php \Drupal\social_album\Form\SocialAlbumImageForm::submitForm()
  3. 10.2.x modules/social_features/social_album/src/Form/SocialAlbumImageForm.php \Drupal\social_album\Form\SocialAlbumImageForm::submitForm()

This is the default entity object builder function. It is called before any other submit handler to build the new entity object to be used by the following submit handlers. At this point of the form workflow the entity is validated and the form state can be updated, this way the subsequently invoked handlers can retrieve a regular entity object to act on. Generally this method should not be overridden unless the entity requires the same preparation for two actions, see \Drupal\comment\CommentForm for an example with the save and preview actions.

Parameters

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

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

Overrides ContentEntityDeleteForm::submitForm

File

modules/social_features/social_album/src/Form/SocialAlbumImageForm.php, line 29

Class

SocialAlbumImageForm
Provides a form for deleting album post entities.

Namespace

Drupal\social_album\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $form_state
    ->setRedirectUrl($this
    ->getRedirectUrl());
  if (!($fid = $form_state
    ->get('fid'))) {
    return;
  }

  /** @var \Drupal\social_post\Entity\PostInterface $entity */
  $entity = $this
    ->getEntity();

  /** @var \Drupal\file\Plugin\Field\FieldType\FileFieldItemList $field */
  $field = $entity->field_post_image;
  foreach ($field
    ->getValue() as $index => $item) {
    if ($item['target_id'] === $fid) {
      break;
    }
  }
  $field
    ->removeItem($index);
  if ($field
    ->isEmpty()) {
    $entity
      ->set('field_album', NULL);
  }
  $entity
    ->save();
}