ViewUIConverter.php 3.07 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\views_ui\ParamConverter;

5
use Drupal\Core\Config\ConfigFactoryInterface;
6
use Drupal\Core\Entity\EntityManagerInterface;
7 8
use Drupal\Core\ParamConverter\AdminPathConfigEntityConverter;
use Drupal\Core\Routing\AdminContext;
9 10
use Symfony\Component\Routing\Route;
use Drupal\Core\ParamConverter\ParamConverterInterface;
11
use Drupal\user\SharedTempStoreFactory;
12 13 14 15
use Drupal\views_ui\ViewUI;

/**
 * Provides upcasting for a view entity to be used in the Views UI.
16 17 18 19 20 21 22 23 24 25 26 27 28
 *
 * 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}.
29
 */
30
class ViewUIConverter extends AdminPathConfigEntityConverter implements ParamConverterInterface {
31 32 33 34

  /**
   * Stores the tempstore factory.
   *
35
   * @var \Drupal\user\SharedTempStoreFactory
36 37 38 39 40 41
   */
  protected $tempStoreFactory;

  /**
   * Constructs a new ViewUIConverter.
   *
42 43
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
   *   The entity manager.
44
   * @param \Drupal\user\SharedTempStoreFactory $temp_store_factory
45 46
   *   The factory for the temp store object.
   */
47 48 49 50 51 52 53 54 55 56 57 58
  public function __construct(EntityManagerInterface $entity_manager, SharedTempStoreFactory $temp_store_factory, ConfigFactoryInterface $config_factory = NULL, AdminContext $admin_context = NULL) {
    // The config factory and admin context are new arguments due to changing
    // the parent. Avoid an error on updated sites by falling back to getting
    // them from the container.
    // @todo Remove in 8.2.x in https://www.drupal.org/node/2674328.
    if (!$config_factory) {
      $config_factory = \Drupal::configFactory();
    }
    if (!$admin_context) {
      $admin_context = \Drupal::service('router.admin_context');
    }
    parent::__construct($entity_manager, $config_factory, $admin_context);
59

60 61 62 63
    $this->tempStoreFactory = $temp_store_factory;
  }

  /**
64
   * {@inheritdoc}
65
   */
66 67
  public function convert($value, $definition, $name, array $defaults) {
    if (!$entity = parent::convert($value, $definition, $name, $defaults)) {
68 69 70
      return;
    }

71 72 73 74 75 76 77
    // 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();
78
      }
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
      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';
98
    }
99
    return FALSE;
100 101 102
  }

}