public function AssetGroupActionForm::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/
asset/ group/ src/ Form/ AssetGroupActionForm.php, line 177
Class
- AssetGroupActionForm
- Provides an asset group confirmation form.
Namespace
Drupal\farm_group\FormCode
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 observation log to group the assets.
if ($form_state
->getValue('confirm') && !empty($accessible_entities)) {
// Load group assets.
$groups = [];
$group_ids = array_column($form_state
->getValue('group', []), 'target_id');
if (!empty($group_ids)) {
$groups = $this->entityTypeManager
->getStorage('asset')
->loadMultiple($group_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);
$group_names = farm_log_asset_names_summary($groups);
$log_name = $this
->t('Group :assets into :groups', [
':assets' => $asset_names,
':groups' => $group_names,
]);
// Create the log.
$log = Log::create([
'name' => $log_name,
'type' => 'observation',
'timestamp' => $date
->getTimestamp(),
'asset' => $accessible_entities,
'is_group_assignment' => TRUE,
'group' => $groups,
]);
// 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 group @count @item because you do not have the necessary permissions.', 'Could not group @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());
}