You are here

public function AssetMoveActionForm::submitForm in farmOS 2.x

Form submission handler.

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 FormInterface::submitForm

File

modules/core/location/src/Form/AssetMoveActionForm.php, line 176

Class

AssetMoveActionForm
Provides an asset move confirmation form.

Namespace

Drupal\farm_location\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {

  // Filter out entities the user doesn't have access to.
  $inaccessible_entities = [];
  $accessible_entities = [];
  foreach ($this->entities as $entity) {
    if (!$entity
      ->access('update', $this
      ->currentUser())) {
      $inaccessible_entities[] = $entity;
      continue;
    }
    $accessible_entities[] = $entity;
  }

  // Create an activity log to move the assets.
  if ($form_state
    ->getValue('confirm') && !empty($accessible_entities)) {

    // Load location assets.
    $locations = [];
    $location_ids = array_column($form_state
      ->getValue('location', []), 'target_id');
    if (!empty($location_ids)) {
      $locations = $this->entityTypeManager
        ->getStorage('asset')
        ->loadMultiple($location_ids);
    }

    /** @var \Drupal\Core\Datetime\DrupalDateTime $date */
    $date = $form_state
      ->getValue('date');
    $done = (bool) $form_state
      ->getValue('done', FALSE);

    // Generate a name for the log.
    $asset_names = farm_log_asset_names_summary($accessible_entities);
    $location_names = farm_log_asset_names_summary($locations);
    $log_name = $this
      ->t('Move :assets to :locations', [
      ':assets' => $asset_names,
      ':locations' => $location_names,
    ]);

    // Create the log.
    $log = Log::create([
      'name' => $log_name,
      'type' => 'activity',
      'timestamp' => $date
        ->getTimestamp(),
      'asset' => $accessible_entities,
      'is_movement' => TRUE,
      'location' => $locations,
    ]);

    // Mark as done.
    if ($done !== FALSE) {
      $log
        ->get('status')
        ->first()
        ->applyTransitionById('done');
    }
    $log
      ->save();
    $this
      ->messenger()
      ->addMessage($this
      ->t('Log created: <a href=":uri">%log_label</a>', [
      ':uri' => $log
        ->toUrl()
        ->toString(),
      '%log_label' => $log
        ->label(),
    ]));
  }

  // Add warning message for inaccessible entities.
  if (!empty($inaccessible_entities)) {
    $inaccessible_count = count($inaccessible_entities);
    $this
      ->messenger()
      ->addWarning($this
      ->formatPlural($inaccessible_count, 'Could not move @count @item because you do not have the necessary permissions.', 'Could not move @count @items because you do not have the necessary permissions.', [
      '@item' => $this->entityType
        ->getSingularLabel(),
      '@items' => $this->entityType
        ->getPluralLabel(),
    ]));
  }
  $this->tempStore
    ->delete($this
    ->currentUser()
    ->id());
  $form_state
    ->setRedirectUrl($this
    ->getCancelUrl());
}