View source
<?php
namespace Drupal\farm_group\Form;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\TempStore\PrivateTempStoreFactory;
use Drupal\Core\Url;
use Drupal\log\Entity\Log;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
class AssetGroupActionForm extends ConfirmFormBase {
protected $tempStore;
protected $entityTypeManager;
protected $user;
protected $entityType;
protected $entities;
public function __construct(PrivateTempStoreFactory $temp_store_factory, EntityTypeManagerInterface $entity_type_manager, AccountInterface $user) {
$this->tempStore = $temp_store_factory
->get('asset_group_confirm');
$this->entityTypeManager = $entity_type_manager;
$this->user = $user;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('tempstore.private'), $container
->get('entity_type.manager'), $container
->get('current_user'));
}
public function getFormId() {
return 'asset_group_action_confirm_form';
}
public function getQuestion() {
return $this
->formatPlural(count($this->entities), 'Are you sure you want to group this @item?', 'Are you sure you want to group these @items?', [
'@item' => $this->entityType
->getSingularLabel(),
'@items' => $this->entityType
->getPluralLabel(),
]);
}
public function getCancelUrl() {
if ($this->entityType
->hasLinkTemplate('collection')) {
return new Url('entity.' . $this->entityType
->id() . '.collection');
}
else {
return new Url('<front>');
}
}
public function getDescription() {
return '';
}
public function getConfirmText() {
return $this
->t('Group');
}
public function buildForm(array $form, FormStateInterface $form_state) {
$this->entityType = $this->entityTypeManager
->getDefinition('asset');
$this->entities = $this->tempStore
->get($this->user
->id());
if (empty($this->entityType) || empty($this->entities)) {
return new RedirectResponse($this
->getCancelUrl()
->setAbsolute()
->toString());
}
$form['date'] = [
'#type' => 'datelist',
'#title' => $this
->t('Date'),
'#default_value' => new DrupalDateTime(),
'#date_part_order' => [
'year',
'month',
'day',
],
'#date_year_range' => '-15:+15',
'#required' => TRUE,
];
$form['group'] = [
'#type' => 'entity_autocomplete',
'#title' => $this
->t('Group'),
'#description' => $this
->t('The groups to assign the asset to. Leave blank to un-assign an asset from groups.'),
'#target_type' => 'asset',
'#selection_handler' => 'views',
'#selection_settings' => [
'view' => [
'view_name' => 'farm_group_reference',
'display_name' => 'entity_reference',
],
'match_operator' => 'CONTAINS',
'match_limit' => 10,
],
'#tags' => TRUE,
'#validate_reference' => FALSE,
'#maxlength' => 1024,
];
$form['done'] = [
'#type' => 'checkbox',
'#title' => $this
->t('This membership change has taken place (mark the log as done)'),
];
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$inaccessible_entities = [];
$accessible_entities = [];
foreach ($this->entities as $entity) {
if (!$entity
->access('update', $this
->currentUser())) {
$inaccessible_entities[] = $entity;
continue;
}
$accessible_entities[] = $entity;
}
if ($form_state
->getValue('confirm') && !empty($accessible_entities)) {
$groups = [];
$group_ids = array_column($form_state
->getValue('group', []), 'target_id');
if (!empty($group_ids)) {
$groups = $this->entityTypeManager
->getStorage('asset')
->loadMultiple($group_ids);
}
$date = $form_state
->getValue('date');
$done = (bool) $form_state
->getValue('done', FALSE);
$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,
]);
$log = Log::create([
'name' => $log_name,
'type' => 'observation',
'timestamp' => $date
->getTimestamp(),
'asset' => $accessible_entities,
'is_group_assignment' => TRUE,
'group' => $groups,
]);
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(),
]));
}
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());
}
}