diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f5cbeacc..c11b7e67c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Changed + +- [Refactor Move and Group actions into quick forms #736](https://github.com/farmOS/farmOS/pull/736) + ### Fixed - [Fix planting quick form creating empty quantities #737](https://github.com/farmOS/farmOS/pull/737) diff --git a/docs/development/module/quick.md b/docs/development/module/quick.md index d5a5d5e77..0f52b12f3 100644 --- a/docs/development/module/quick.md +++ b/docs/development/module/quick.md @@ -456,10 +456,12 @@ selected entities can then be used in the quick form code in various ways. ### Providing a quick form action -To add a quick form action, two additional files are added to the module: +To add a quick form action, three additional files are added to the module: 1. a PHP class in `src/Plugin/Action` that extends from `QuickFormActionBase` 2. an action config entity in `config/install/system.action.*.yml` +3. a `config/schema/[mymodule].schema.yml` file that describes action schema + (see example below). For example, an action that redirects to the "Harvest" quick form defined above for prepopulating the "Asset" field would be provided as follows: @@ -511,6 +513,15 @@ plugin: harvest configuration: { } ``` +`/config/schema/farm_quick_harvest.schema.yml`: + +```yml +# Schema for actions. +action.configuration.harvest: + type: action_configuration_default + label: 'Configuration for the harvest action' +``` + Note that config entities are only created when the module is installed. In order to add a config entity to a module that is already installed, an update hook must be used to manually create the config entity. 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/core/location/config/optional/system.action.asset_move_action.yml b/modules/core/location/config/optional/system.action.asset_move_action.yml deleted file mode 100644 index 3b2f9a60d..000000000 --- a/modules/core/location/config/optional/system.action.asset_move_action.yml +++ /dev/null @@ -1,12 +0,0 @@ -langcode: en -status: true -dependencies: - module: - - asset - - farm_activity - - farm_location -id: asset_move_action -label: 'Move asset' -type: asset -plugin: 'asset_move_action' -configuration: { } diff --git a/modules/core/location/config/schema/farm_location.schema.yml b/modules/core/location/config/schema/farm_location.schema.yml index 963ecb078..2b75c6108 100644 --- a/modules/core/location/config/schema/farm_location.schema.yml +++ b/modules/core/location/config/schema/farm_location.schema.yml @@ -18,8 +18,3 @@ log.type.*.third_party.farm_location: is_movement: type: boolean label: 'Is a movement' - -# Schema for actions. -action.configuration.asset_move_action: - type: action_configuration_default - label: 'Configuration for the asset move action' diff --git a/modules/core/location/farm_location.base_fields.inc b/modules/core/location/farm_location.base_fields.inc index 0d9079ed5..02c3b1e82 100644 --- a/modules/core/location/farm_location.base_fields.inc +++ b/modules/core/location/farm_location.base_fields.inc @@ -29,7 +29,6 @@ function farm_location_asset_base_fields() { 'settings' => [ 'link' => TRUE, 'render_without_location' => TRUE, - 'move_asset_button' => TRUE, ], 'weight' => 95, ], diff --git a/modules/core/location/farm_location.post_update.php b/modules/core/location/farm_location.post_update.php new file mode 100644 index 000000000..9607a797d --- /dev/null +++ b/modules/core/location/farm_location.post_update.php @@ -0,0 +1,16 @@ +delete(); +} diff --git a/modules/core/location/farm_location.routing.yml b/modules/core/location/farm_location.routing.yml deleted file mode 100644 index d642ba82c..000000000 --- a/modules/core/location/farm_location.routing.yml +++ /dev/null @@ -1,6 +0,0 @@ -farm_location.asset_move_action_form: - path: '/asset/move' - defaults: - _form: 'Drupal\farm_location\Form\AssetMoveActionForm' - requirements: - _user_is_logged_in: 'TRUE' diff --git a/modules/core/location/src/Form/AssetMoveActionForm.php b/modules/core/location/src/Form/AssetMoveActionForm.php deleted file mode 100644 index b96f3b149..000000000 --- a/modules/core/location/src/Form/AssetMoveActionForm.php +++ /dev/null @@ -1,289 +0,0 @@ -tempStore = $temp_store_factory->get('asset_move_confirm'); - $this->entityTypeManager = $entity_type_manager; - $this->user = $user; - $this->request = $request; - } - - /** - * {@inheritdoc} - */ - public static function create(ContainerInterface $container) { - return new static( - $container->get('tempstore.private'), - $container->get('entity_type.manager'), - $container->get('current_user'), - $container->get('request_stack')->getCurrentRequest(), - ); - } - - /** - * {@inheritdoc} - */ - public function getFormId() { - return 'asset_move_action_confirm_form'; - } - - /** - * {@inheritdoc} - */ - public function getQuestion() { - return $this->formatPlural(count($this->entities), 'Are you sure you want to move this @item?', 'Are you sure you want to move 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('Move'); - } - - /** - * {@inheritdoc} - */ - public function buildForm(array $form, FormStateInterface $form_state) { - - // Check if asset IDs were provided in the asset query param. - if ($asset_ids = $this->request->get('asset')) { - - // Wrap in an array, if necessary. - if (!is_array($asset_ids)) { - $asset_ids = [$asset_ids]; - } - - // Add each asset the user has view access to. - $this->entities = array_filter($this->entityTypeManager->getStorage('asset')->loadMultiple($asset_ids), function (AssetInterface $asset) { - return $asset->access('view', $this->user); - }); - } - // Else load entities from the tempStore state. - else { - $this->entities = $this->tempStore->get($this->user->id()); - } - - $this->entityType = $this->entityTypeManager->getDefinition('asset'); - 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['location'] = [ - '#type' => 'entity_autocomplete', - '#title' => $this->t('Location'), - '#target_type' => 'asset', - '#selection_handler' => 'views', - '#selection_settings' => [ - 'view' => [ - 'view_name' => 'farm_location_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 movement 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 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); - } - - $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 - $location_names = farm_log_asset_names_summary($locations); - $log_name = $this->t('Clear location of @assets', ['@assets' => Markup::create($asset_names)]); - if (!empty($location_names)) { - $log_name = $this->t('Move @assets to @locations', ['@assets' => Markup::create($asset_names), '@locations' => Markup::create($location_names)]); - } - - // Create the log. - $log = Log::create([ - 'name' => $log_name, - 'type' => 'activity', - 'timestamp' => $form_state->getValue('date')->getTimestamp(), - 'asset' => $accessible_entities, - 'is_movement' => TRUE, - 'location' => $locations, - ]); - - // 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 move 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 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()); - } - -} diff --git a/modules/core/location/src/Plugin/Action/AssetMove.php b/modules/core/location/src/Plugin/Action/AssetMove.php deleted file mode 100644 index eb8074c84..000000000 --- a/modules/core/location/src/Plugin/Action/AssetMove.php +++ /dev/null @@ -1,106 +0,0 @@ -currentUser = $current_user; - $this->tempStore = $temp_store_factory->get('asset_move_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_activity for activity logs. - $dependencies['module'][] = 'farm_activity'; - 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/core/location/src/Plugin/Field/FieldFormatter/AssetCurrentLocationFormatter.php b/modules/core/location/src/Plugin/Field/FieldFormatter/AssetCurrentLocationFormatter.php index 0408a4895..8b1befea3 100644 --- a/modules/core/location/src/Plugin/Field/FieldFormatter/AssetCurrentLocationFormatter.php +++ b/modules/core/location/src/Plugin/Field/FieldFormatter/AssetCurrentLocationFormatter.php @@ -5,7 +5,6 @@ namespace Drupal\farm_location\Plugin\Field\FieldFormatter; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceLabelFormatter; use Drupal\Core\Form\FormStateInterface; -use Drupal\Core\Url; /** * Field formatter for the current location asset field. @@ -27,7 +26,6 @@ class AssetCurrentLocationFormatter extends EntityReferenceLabelFormatter { public static function defaultSettings() { return [ 'render_without_location' => FALSE, - 'move_asset_button' => FALSE, ] + parent::defaultSettings(); } @@ -42,12 +40,6 @@ class AssetCurrentLocationFormatter extends EntityReferenceLabelFormatter { '#type' => 'checkbox', '#default_value' => $this->getSetting('render_without_location'), ]; - $elements['move_asset_button'] = [ - '#title' => $this->t('Move asset button'), - '#description' => $this->t('Include a button to move the asset.'), - '#type' => 'checkbox', - '#default_value' => $this->getSetting('move_asset_button'), - ]; return $elements; } @@ -57,7 +49,6 @@ class AssetCurrentLocationFormatter extends EntityReferenceLabelFormatter { public function settingsSummary() { $summary = parent::settingsSummary(); $summary[] = $this->getSetting('render_without_location') ? $this->t('Render without current location') : $this->t('Do not render without current location'); - $summary[] = $this->getSetting('move_asset_button') ? $this->t('Include move asset button') : $this->t('No move asset button'); return $summary; } @@ -89,25 +80,6 @@ class AssetCurrentLocationFormatter extends EntityReferenceLabelFormatter { $elements[] = ['#markup' => 'N/A']; } - // Add the move asset button if configured. - if ($this->getSetting('move_asset_button')) { - - // Append a "Move asset" link. - $options = [ - 'query' => [ - 'asset' => $asset->id(), - 'destination' => $asset->toUrl()->toString(), - ], - ]; - $elements[] = [ - '#type' => 'link', - '#title' => $this->t('Move asset'), - '#url' => Url::fromRoute('farm_location.asset_move_action_form', [], $options), - '#attributes' => [ - 'class' => ['button', 'button--small'], - ], - ]; - } return $elements; } 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. diff --git a/modules/quick/movement/config/install/system.action.quick_movement.yml b/modules/quick/movement/config/install/system.action.quick_movement.yml new file mode 100644 index 000000000..ba5f917b3 --- /dev/null +++ b/modules/quick/movement/config/install/system.action.quick_movement.yml @@ -0,0 +1,11 @@ +langcode: en +status: true +dependencies: + module: + - asset + - farm_quick_movement +id: quick_movement +label: 'Record movement' +type: asset +plugin: quick_movement +configuration: { } diff --git a/modules/quick/movement/config/schema/farm_quick_movement.schema.yml b/modules/quick/movement/config/schema/farm_quick_movement.schema.yml new file mode 100644 index 000000000..e5ff01fc2 --- /dev/null +++ b/modules/quick/movement/config/schema/farm_quick_movement.schema.yml @@ -0,0 +1,4 @@ +# Schema for actions. +action.configuration.quick_movement: + type: action_configuration_default + label: 'Configuration for the quick movement action' diff --git a/modules/quick/movement/farm_quick_movement.module b/modules/quick/movement/farm_quick_movement.module new file mode 100644 index 000000000..99dda6e56 --- /dev/null +++ b/modules/quick/movement/farm_quick_movement.module @@ -0,0 +1,24 @@ +id() == 'asset' && !empty($fields['location'])) { + $display_options = $fields['location']->getDisplayOptions('view'); + $display_options['type'] = 'asset_current_location_move'; + $display_options['settings']['move_asset_button'] = TRUE; + $fields['location']->setDisplayOptions('view', $display_options); + } +} diff --git a/modules/quick/movement/farm_quick_movement.post_update.php b/modules/quick/movement/farm_quick_movement.post_update.php new file mode 100644 index 000000000..782f9fdf7 --- /dev/null +++ b/modules/quick/movement/farm_quick_movement.post_update.php @@ -0,0 +1,27 @@ + 'quick_movement', + 'label' => 'Record movement', + 'type' => 'asset', + 'plugin' => 'quick_movement', + 'dependencies' => [ + 'module' => [ + 'asset', + 'farm_quick_movement', + ], + ], + ]); + $config->save(); +} diff --git a/modules/quick/movement/js/farmOS.map.behaviors.quick_movement.js b/modules/quick/movement/js/farmOS.map.behaviors.quick_movement.js index 60f0502d4..3e1eb32f8 100644 --- a/modules/quick/movement/js/farmOS.map.behaviors.quick_movement.js +++ b/modules/quick/movement/js/farmOS.map.behaviors.quick_movement.js @@ -8,6 +8,11 @@ color: 'blue', }; instance.currentLocationLayer = instance.addLayer('vector', opts); + + // If an asset geometry was pre-populated, add it to the layer. + if (instance.farmMapSettings.behaviors.quick_movement.asset_geometry) { + this.updateAssetGeometry(instance, instance.farmMapSettings.behaviors.quick_movement.asset_geometry) + } }, // When updating asset geometry, update the current location layer. diff --git a/modules/quick/movement/src/Plugin/Action/Movement.php b/modules/quick/movement/src/Plugin/Action/Movement.php new file mode 100644 index 000000000..9089288ed --- /dev/null +++ b/modules/quick/movement/src/Plugin/Action/Movement.php @@ -0,0 +1,26 @@ + FALSE, + ] + parent::defaultSettings(); + } + + /** + * {@inheritdoc} + */ + public function settingsForm(array $form, FormStateInterface $form_state) { + $elements = parent::settingsForm($form, $form_state); + $elements['move_asset_button'] = [ + '#title' => $this->t('Move asset button'), + '#description' => $this->t('Include a button to move the asset.'), + '#type' => 'checkbox', + '#default_value' => $this->getSetting('move_asset_button'), + ]; + return $elements; + } + + /** + * {@inheritdoc} + */ + public function settingsSummary() { + $summary = parent::settingsSummary(); + $summary[] = $this->getSetting('move_asset_button') ? $this->t('Include move asset button') : $this->t('No move asset button'); + return $summary; + } + + /** + * {@inheritdoc} + */ + public function viewElements(FieldItemListInterface $items, $langcode) { + + // Build labels in parent. + $elements = parent::viewElements($items, $langcode); + + // Get the asset. + $asset = $items->getEntity(); + + // If the asset is fixed don't render additional information. + if ($asset->get('is_fixed')->value) { + return $elements; + } + + // If there are no current locations only render if configured to. + if (empty($elements) && !$this->getSetting('render_without_location')) { + return $elements; + } + + // Add the move asset button if configured. + if ($this->getSetting('move_asset_button')) { + + // Append a "Move asset" link. + $options = [ + 'query' => [ + 'asset' => $asset->id(), + 'destination' => $asset->toUrl()->toString(), + ], + ]; + $elements[] = [ + '#type' => 'link', + '#title' => $this->t('Move asset'), + '#url' => Url::fromRoute('farm.quick.movement', [], $options), + '#attributes' => [ + 'class' => ['button', 'button--small'], + ], + ]; + } + return $elements; + } + +} diff --git a/modules/quick/movement/src/Plugin/QuickForm/Movement.php b/modules/quick/movement/src/Plugin/QuickForm/Movement.php index 9e9a717ef..489a7793e 100644 --- a/modules/quick/movement/src/Plugin/QuickForm/Movement.php +++ b/modules/quick/movement/src/Plugin/QuickForm/Movement.php @@ -15,6 +15,7 @@ use Drupal\farm_quick\Plugin\QuickForm\QuickFormBase; use Drupal\farm_quick\Plugin\QuickForm\QuickFormInterface; use Drupal\farm_quick\Traits\QuickFormElementsTrait; use Drupal\farm_quick\Traits\QuickLogTrait; +use Drupal\farm_quick\Traits\QuickPrepopulateTrait; use Drupal\farm_quick\Traits\QuickStringTrait; use Psr\Container\ContainerInterface; @@ -35,6 +36,7 @@ class Movement extends QuickFormBase implements QuickFormInterface { use QuickLogTrait; use QuickFormElementsTrait; + use QuickPrepopulateTrait; use QuickStringTrait; use WktTrait; @@ -114,6 +116,7 @@ class Movement extends QuickFormBase implements QuickFormInterface { ]; // Assets. + $prepopulated_assets = $this->getPrepopulatedEntities('asset', $form_state); $form['asset'] = [ '#type' => 'entity_autocomplete', '#title' => $this->t('Assets'), @@ -133,6 +136,7 @@ class Movement extends QuickFormBase implements QuickFormInterface { 'wrapper' => 'asset-geometry', 'event' => 'autocompleteclose change', ], + '#default_value' => $prepopulated_assets, ]; // Locations. @@ -167,6 +171,13 @@ class Movement extends QuickFormBase implements QuickFormInterface { '#behaviors' => [ 'quick_movement', ], + '#map_settings' => [ + 'behaviors' => [ + 'quick_movement' => [ + 'asset_geometry' => $this->combinedAssetGeometries($prepopulated_assets), + ], + ], + ], '#display_raw_geometry' => TRUE, ];