3 namespace Drupal\views_ui\ParamConverter;
5 use Drupal\Core\Config\ConfigFactoryInterface;
6 use Drupal\Core\Entity\EntityManagerInterface;
7 use Drupal\Core\ParamConverter\AdminPathConfigEntityConverter;
8 use Drupal\Core\Routing\AdminContext;
9 use Symfony\Component\Routing\Route;
10 use Drupal\Core\ParamConverter\ParamConverterInterface;
11 use Drupal\user\SharedTempStoreFactory;
12 use Drupal\views_ui\ViewUI;
15 * Provides upcasting for a view entity to be used in the Views UI.
19 * pattern: '/some/{view}/and/{bar}'
26 * The value for {view} will be converted to a view entity prepared for the
27 * Views UI and loaded from the views temp store, but it will not touch the
30 class ViewUIConverter extends AdminPathConfigEntityConverter implements ParamConverterInterface {
33 * Stores the tempstore factory.
35 * @var \Drupal\user\SharedTempStoreFactory
37 protected $tempStoreFactory;
40 * Constructs a new ViewUIConverter.
42 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
44 * @param \Drupal\user\SharedTempStoreFactory $temp_store_factory
45 * The factory for the temp store object.
47 public function __construct(EntityManagerInterface $entity_manager, SharedTempStoreFactory $temp_store_factory, ConfigFactoryInterface $config_factory = NULL, AdminContext $admin_context = NULL) {
48 // The config factory and admin context are new arguments due to changing
49 // the parent. Avoid an error on updated sites by falling back to getting
50 // them from the container.
51 // @todo Remove in 8.2.x in https://www.drupal.org/node/2674328.
52 if (!$config_factory) {
53 $config_factory = \Drupal::configFactory();
55 if (!$admin_context) {
56 $admin_context = \Drupal::service('router.admin_context');
58 parent::__construct($entity_manager, $config_factory, $admin_context);
60 $this->tempStoreFactory = $temp_store_factory;
66 public function convert($value, $definition, $name, array $defaults) {
67 if (!$entity = parent::convert($value, $definition, $name, $defaults)) {
71 // Get the temp store for this variable if it needs one. Attempt to load the
72 // view from the temp store, synchronize its status with the existing view,
73 // and store the lock metadata.
74 $store = $this->tempStoreFactory->get('views');
75 if ($view = $store->get($value)) {
76 if ($entity->status()) {
82 $view->lock = $store->getMetadata($value);
84 // Otherwise, decorate the existing view for use in the UI.
86 $view = new ViewUI($entity);
95 public function applies($definition, $name, Route $route) {
96 if (parent::applies($definition, $name, $route)) {
97 return !empty($definition['tempstore']) && $definition['type'] === 'entity:view';