Issue #3290993 by paul121: Add "Move asset" button next to the current location field
This commit is contained in:
commit
28c32843c1
|
@ -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
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -0,0 +1,114 @@
|
|||
<?php
|
||||
|
||||
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.
|
||||
*
|
||||
* @FieldFormatter(
|
||||
* id = "asset_current_location",
|
||||
* label = @Translation("Asset current location"),
|
||||
* description = @Translation("Display the label of the referenced entities."),
|
||||
* field_types = {
|
||||
* "entity_reference"
|
||||
* }
|
||||
* )
|
||||
*/
|
||||
class AssetCurrentLocationFormatter extends EntityReferenceLabelFormatter {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function defaultSettings() {
|
||||
return [
|
||||
'render_without_location' => 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;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue