diff --git a/modules/asset/group/config/optional/system.action.asset_group_action.yml b/modules/asset/group/config/optional/system.action.asset_group_action.yml deleted file mode 100644 index f90c92254..000000000 --- a/modules/asset/group/config/optional/system.action.asset_group_action.yml +++ /dev/null @@ -1,12 +0,0 @@ -langcode: en -status: true -dependencies: - module: - - asset - - farm_group - - farm_observation -id: asset_group_action -label: 'Group asset' -type: asset -plugin: 'asset_group_action' -configuration: { } diff --git a/modules/asset/group/config/schema/farm_group.schema.yml b/modules/asset/group/config/schema/farm_group.schema.yml deleted file mode 100644 index 41f85dbff..000000000 --- a/modules/asset/group/config/schema/farm_group.schema.yml +++ /dev/null @@ -1,3 +0,0 @@ -action.configuration.asset_group_action: - type: action_configuration_default - label: 'Configuration for the asset group action' diff --git a/modules/asset/group/farm_group.post_update.php b/modules/asset/group/farm_group.post_update.php new file mode 100644 index 000000000..bf1746aa3 --- /dev/null +++ b/modules/asset/group/farm_group.post_update.php @@ -0,0 +1,16 @@ +delete(); +} diff --git a/modules/asset/group/src/Form/AssetGroupActionForm.php b/modules/asset/group/src/Form/AssetGroupActionForm.php deleted file mode 100644 index 298a55e86..000000000 --- a/modules/asset/group/src/Form/AssetGroupActionForm.php +++ /dev/null @@ -1,259 +0,0 @@ -tempStore = $temp_store_factory->get('asset_group_confirm'); - $this->entityTypeManager = $entity_type_manager; - $this->user = $user; - } - - /** - * {@inheritdoc} - */ - public static function create(ContainerInterface $container) { - return new static( - $container->get('tempstore.private'), - $container->get('entity_type.manager'), - $container->get('current_user') - ); - } - - /** - * {@inheritdoc} - */ - public function getFormId() { - return 'asset_group_action_confirm_form'; - } - - /** - * {@inheritdoc} - */ - 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(), - ]); - } - - /** - * {@inheritdoc} - */ - public function getCancelUrl() { - if ($this->entityType->hasLinkTemplate('collection')) { - return new Url('entity.' . $this->entityType->id() . '.collection'); - } - else { - return new Url(''); - } - } - - /** - * {@inheritdoc} - */ - public function getDescription() { - return ''; - } - - /** - * {@inheritdoc} - */ - public function getConfirmText() { - return $this->t('Group'); - } - - /** - * {@inheritdoc} - */ - 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' => 'datetime', - '#title' => $this->t('Date'), - '#default_value' => new DrupalDateTime('midnight', $this->user->getTimeZone()), - '#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); - } - - /** - * {@inheritdoc} - */ - 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); - } - - $done = (bool) $form_state->getValue('done', FALSE); - - // Generate a name for the log. - // @phpstan-ignore-next-line - $asset_names = farm_log_asset_names_summary($accessible_entities); - // @phpstan-ignore-next-line - $group_names = farm_log_asset_names_summary($groups); - $log_name = $this->t('Clear group membership of @assets', ['@assets' => Markup::create($asset_names)]); - if (!empty($group_names)) { - $log_name = $this->t('Group @assets into @groups', ['@assets' => Markup::create($asset_names), '@groups' => Markup::create($group_names)]); - } - - // Create the log. - $log = Log::create([ - 'name' => $log_name, - 'type' => 'observation', - 'timestamp' => $form_state->getValue('date')->getTimestamp(), - 'asset' => $accessible_entities, - 'is_group_assignment' => TRUE, - 'group' => $groups, - ]); - - // Mark as done. - if ($done !== FALSE) { - $log->get('status')->first()->applyTransitionById('done'); - } - - // Validate the log before saving. - $violations = $log->validate(); - if ($violations->count() > 0) { - $this->messenger()->addWarning( - $this->t('Could not group assets: @bundle @entity_type validation failed.', - [ - '@bundle' => $log->getBundleLabel(), - '@entity_type' => $log->getEntityType()->getSingularLabel(), - ], - ), - ); - $this->tempStore->delete($this->currentUser()->id()); - $form_state->setRedirectUrl($this->getCancelUrl()); - return; - } - - $log->save(); - $this->messenger()->addMessage($this->t('Log created: %log_label', [':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()); - } - -} diff --git a/modules/asset/group/src/Plugin/Action/AssetGroup.php b/modules/asset/group/src/Plugin/Action/AssetGroup.php deleted file mode 100644 index dea3b05c5..000000000 --- a/modules/asset/group/src/Plugin/Action/AssetGroup.php +++ /dev/null @@ -1,106 +0,0 @@ -currentUser = $current_user; - $this->tempStore = $temp_store_factory->get('asset_group_confirm'); - - parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager); - } - - /** - * {@inheritdoc} - */ - public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { - return new static( - $configuration, - $plugin_id, - $plugin_definition, - $container->get('entity_type.manager'), - $container->get('tempstore.private'), - $container->get('current_user') - ); - } - - /** - * {@inheritdoc} - */ - public function calculateDependencies() { - $dependencies = parent::calculateDependencies(); - // Add dependency on farm_observation for observation logs. - $dependencies['module'][] = 'farm_observation'; - return $dependencies; - } - - /** - * {@inheritdoc} - */ - public function executeMultiple(array $entities) { - /** @var \Drupal\Core\Entity\EntityInterface[] $entities */ - $this->tempStore->set($this->currentUser->id(), $entities); - } - - /** - * {@inheritdoc} - */ - public function execute($object = NULL) { - $this->executeMultiple([$object]); - } - - /** - * {@inheritdoc} - */ - public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) { - return $object->access('update', $account, $return_as_object); - } - -} diff --git a/modules/quick/group/config/install/system.action.quick_group.yml b/modules/quick/group/config/install/system.action.quick_group.yml new file mode 100644 index 000000000..853a55abd --- /dev/null +++ b/modules/quick/group/config/install/system.action.quick_group.yml @@ -0,0 +1,11 @@ +langcode: en +status: true +dependencies: + module: + - asset + - farm_quick_group +id: quick_group +label: 'Assign group membership' +type: asset +plugin: quick_group +configuration: { } diff --git a/modules/quick/group/config/schema/farm_quick_group.schema.yml b/modules/quick/group/config/schema/farm_quick_group.schema.yml new file mode 100644 index 000000000..dd83e7c80 --- /dev/null +++ b/modules/quick/group/config/schema/farm_quick_group.schema.yml @@ -0,0 +1,4 @@ +# Schema for actions. +action.configuration.quick_group: + type: action_configuration_default + label: 'Configuration for the quick group action' diff --git a/modules/quick/group/farm_quick_group.post_update.php b/modules/quick/group/farm_quick_group.post_update.php new file mode 100644 index 000000000..f0913d882 --- /dev/null +++ b/modules/quick/group/farm_quick_group.post_update.php @@ -0,0 +1,27 @@ + 'quick_group', + 'label' => 'Assign group membership', + 'type' => 'asset', + 'plugin' => 'quick_group', + 'dependencies' => [ + 'module' => [ + 'asset', + 'farm_quick_group', + ], + ], + ]); + $config->save(); +} diff --git a/modules/quick/group/src/Plugin/Action/Group.php b/modules/quick/group/src/Plugin/Action/Group.php new file mode 100644 index 000000000..1494761be --- /dev/null +++ b/modules/quick/group/src/Plugin/Action/Group.php @@ -0,0 +1,26 @@ +getPrepopulatedEntities('asset', $form_state); $form['asset'] = [ '#type' => 'entity_autocomplete', '#title' => $this->t('Assets'), @@ -126,6 +129,7 @@ class Group extends QuickFormBase implements QuickFormInterface { '#maxlength' => 1024, '#tags' => TRUE, '#required' => TRUE, + '#default_value' => $prepopulated_assets, ]; // Groups.