diff --git a/CHANGELOG.md b/CHANGELOG.md index c640940f2..6cb95c9a5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - [Issue #3290929: Provide a farmOS map form element](https://www.drupal.org/project/farm/issues/3290929) +- [Issue #3290993: Add "Move asset" button next to the current location field](https://www.drupal.org/project/farm/issues/3290993) ### Security diff --git a/modules/core/location/farm_location.base_fields.inc b/modules/core/location/farm_location.base_fields.inc index 73968ce4c..0d9079ed5 100644 --- a/modules/core/location/farm_location.base_fields.inc +++ b/modules/core/location/farm_location.base_fields.inc @@ -23,8 +23,15 @@ function farm_location_asset_base_fields() { 'multiple' => TRUE, 'computed' => AssetLocationItemList::class, 'hidden' => 'form', - 'weight' => [ - 'view' => 95, + 'view_display_options' => [ + 'label' => 'inline', + 'type' => 'asset_current_location', + 'settings' => [ + 'link' => TRUE, + 'render_without_location' => TRUE, + 'move_asset_button' => TRUE, + ], + 'weight' => 95, ], ]; $fields['location'] = \Drupal::service('farm_field.factory')->baseFieldDefinition($options); diff --git a/modules/core/location/src/Form/AssetMoveActionForm.php b/modules/core/location/src/Form/AssetMoveActionForm.php index d2796fa4f..a991ecb89 100644 --- a/modules/core/location/src/Form/AssetMoveActionForm.php +++ b/modules/core/location/src/Form/AssetMoveActionForm.php @@ -2,6 +2,7 @@ namespace Drupal\farm_location\Form; +use Drupal\asset\Entity\AssetInterface; use Drupal\Core\Datetime\DrupalDateTime; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Form\ConfirmFormBase; @@ -127,8 +128,27 @@ class AssetMoveActionForm extends ConfirmFormBase { * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { + + // Check if asset IDs were provided in the asset query param. + $request = \Drupal::request(); + if ($asset_ids = $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'); - $this->entities = $this->tempStore->get($this->user->id()); if (empty($this->entityType) || empty($this->entities)) { return new RedirectResponse($this->getCancelUrl() ->setAbsolute() diff --git a/modules/core/location/src/Plugin/Field/FieldFormatter/AssetCurrentLocationFormatter.php b/modules/core/location/src/Plugin/Field/FieldFormatter/AssetCurrentLocationFormatter.php new file mode 100644 index 000000000..0408a4895 --- /dev/null +++ b/modules/core/location/src/Plugin/Field/FieldFormatter/AssetCurrentLocationFormatter.php @@ -0,0 +1,114 @@ + FALSE, + 'move_asset_button' => FALSE, + ] + parent::defaultSettings(); + } + + /** + * {@inheritdoc} + */ + public function settingsForm(array $form, FormStateInterface $form_state) { + $elements = parent::settingsForm($form, $form_state); + $elements['render_without_location'] = [ + '#title' => $this->t('Render without location'), + '#description' => $this->t('Include this field when the asset has no current location.'), + '#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; + } + + /** + * {@inheritdoc} + */ + 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; + } + + /** + * {@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 N/A if there are no current locations. + if (empty($elements)) { + + // Render N/A if configured. + $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; + } + +}