Tôi đang sử dụng Trình duyệt thực thể (2.x-dev trong Drupal 8) làm tiện ích biểu mẫu cho trường cơ sở tham chiếu thực thể tùy chỉnh của thực thể tùy chỉnh. Trình duyệt thực thể được cấu hình
- như một màn hình hiển thị,
- với một widget
- và không có lựa chọn hiển thị,
- sử dụng chế độ xem với trường chọn hàng loạt trình duyệt thực thể làm widget và
- để nối các thực thể được chọn vào lựa chọn hiện tại của trường tham chiếu.
Chọn các thực thể là làm việc tốt. Nhưng trường tham chiếu thực thể sẽ không có bất kỳ bản sao nào.
Để dễ dàng lựa chọn các thực thể mà không trùng lặp, tôi muốn lọc các thực thể đã chọn từ kết quả xem trình duyệt thực thể. Vì vậy, người dùng sẽ chỉ nhìn thấy các thực thể không được chọn.
Với mục đích này, tôi đã tạo một plugin argument_default tùy chỉnh xem hiển thị lưu trữ lựa chọn trình duyệt thực thể làm đối số mặc định ngữ cảnh cho ID thực thể:
<?php
namespace Drupal\my_module\Plugin\views\argument_default;
use Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface;
use Drupal\views\Plugin\views\argument_default\ArgumentDefaultPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* The entity browser selection argument default handler.
*
* @ViewsArgumentDefault(
* id = "entity_browser_selection",
* title = @Translation("Entity Browser Selection")
* )
*/
class EntityBrowserSelection extends ArgumentDefaultPluginBase {
/**
* The selection storage.
*
* @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface
*/
protected $selectionStorage;
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, KeyValueStoreExpirableInterface $selection_storage) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->selectionStorage = $selection_storage;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('entity_browser.selection_storage')
);
}
/**
* {@inheritdoc}
*/
public function access() {
return $this->view->getDisplay()->pluginId === 'entity_browser';
}
/**
* {@inheritdoc}
*/
public function getArgument() {
$argument = NULL;
$current_request = $this->view->getRequest();
// Check if the widget context is available.
if ($current_request->query->has('uuid')) {
$uuid = $current_request->query->get('uuid');
if ($storage = $this->selectionStorage->get($uuid)) {
if (!empty($storage['selected_entities'])) {
$argument = $storage['selected_entities'];
}
}
}
return $argument;
}
}
Vấn đề tôi gặp phải là lựa chọn hiện tại trong bộ lưu trữ lựa chọn luôn trống, cho dù có bao nhiêu thực thể đã được chọn trong trường tham chiếu thực thể và ngay cả sau khi tôi hoàn thành lựa chọn phương thức và mở lại trình duyệt thực thể.
Tôi phải làm gì để hiển thị lựa chọn hiện tại trong bộ lưu trữ lựa chọn của trình duyệt thực thể?
#default_value
) cần được xem xét như là bộ lọc.