ViewUIConverter.php 2.49 KB
Newer Older
1
2
3
4
5
6
7
8
9
<?php

/**
 * @file
 * Contains \Drupal\views_ui\ParamConverter\ViewUIConverter.
 */

namespace Drupal\views_ui\ParamConverter;

10
use Drupal\Core\Entity\EntityManagerInterface;
11
use Drupal\Core\ParamConverter\EntityConverter;
12
13
use Symfony\Component\Routing\Route;
use Drupal\Core\ParamConverter\ParamConverterInterface;
14
use Drupal\user\SharedTempStoreFactory;
15
16
17
18
use Drupal\views_ui\ViewUI;

/**
 * Provides upcasting for a view entity to be used in the Views UI.
19
20
21
22
23
24
25
26
27
28
29
30
31
 *
 * Example:
 *
 * pattern: '/some/{view}/and/{bar}'
 * options:
 *   parameters:
 *     view:
 *       type: 'entity:view'
 *       tempstore: TRUE
 *
 * The value for {view} will be converted to a view entity prepared for the
 * Views UI and loaded from the views temp store, but it will not touch the
 * value for {bar}.
32
 */
33
class ViewUIConverter extends EntityConverter implements ParamConverterInterface {
34
35
36
37

  /**
   * Stores the tempstore factory.
   *
38
   * @var \Drupal\user\SharedTempStoreFactory
39
40
41
42
43
44
   */
  protected $tempStoreFactory;

  /**
   * Constructs a new ViewUIConverter.
   *
45
46
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
   *   The entity manager.
47
   * @param \Drupal\user\SharedTempStoreFactory $temp_store_factory
48
49
   *   The factory for the temp store object.
   */
50
  public function __construct(EntityManagerInterface $entity_manager, SharedTempStoreFactory $temp_store_factory) {
51
52
    parent::__construct($entity_manager);

53
54
55
56
    $this->tempStoreFactory = $temp_store_factory;
  }

  /**
57
   * {@inheritdoc}
58
   */
59
60
  public function convert($value, $definition, $name, array $defaults) {
    if (!$entity = parent::convert($value, $definition, $name, $defaults)) {
61
62
63
      return;
    }

64
65
66
67
68
69
70
    // Get the temp store for this variable if it needs one. Attempt to load the
    // view from the temp store, synchronize its status with the existing view,
    // and store the lock metadata.
    $store = $this->tempStoreFactory->get('views');
    if ($view = $store->get($value)) {
      if ($entity->status()) {
        $view->enable();
71
      }
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
      else {
        $view->disable();
      }
      $view->lock = $store->getMetadata($value);
    }
    // Otherwise, decorate the existing view for use in the UI.
    else {
      $view = new ViewUI($entity);
    }

    return $view;
  }

  /**
   * {@inheritdoc}
   */
  public function applies($definition, $name, Route $route) {
    if (parent::applies($definition, $name, $route)) {
      return !empty($definition['tempstore']) && $definition['type'] === 'entity:view';
91
    }
92
    return FALSE;
93
94
95
  }

}