PageRenderTime 22ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/views_ui/src/ParamConverter/ViewUIConverter.php

https://gitlab.com/geeta7/drupal
PHP | 107 lines | 48 code | 12 blank | 47 comment | 9 complexity | 6ea2d59de6dff5c3d55a18a90adfab2a MD5 | raw file
  1. <?php
  2. /**
  3. * @file
  4. * Contains \Drupal\views_ui\ParamConverter\ViewUIConverter.
  5. */
  6. namespace Drupal\views_ui\ParamConverter;
  7. use Drupal\Core\Config\ConfigFactoryInterface;
  8. use Drupal\Core\Entity\EntityManagerInterface;
  9. use Drupal\Core\ParamConverter\AdminPathConfigEntityConverter;
  10. use Drupal\Core\Routing\AdminContext;
  11. use Symfony\Component\Routing\Route;
  12. use Drupal\Core\ParamConverter\ParamConverterInterface;
  13. use Drupal\user\SharedTempStoreFactory;
  14. use Drupal\views_ui\ViewUI;
  15. /**
  16. * Provides upcasting for a view entity to be used in the Views UI.
  17. *
  18. * Example:
  19. *
  20. * pattern: '/some/{view}/and/{bar}'
  21. * options:
  22. * parameters:
  23. * view:
  24. * type: 'entity:view'
  25. * tempstore: TRUE
  26. *
  27. * The value for {view} will be converted to a view entity prepared for the
  28. * Views UI and loaded from the views temp store, but it will not touch the
  29. * value for {bar}.
  30. */
  31. class ViewUIConverter extends AdminPathConfigEntityConverter implements ParamConverterInterface {
  32. /**
  33. * Stores the tempstore factory.
  34. *
  35. * @var \Drupal\user\SharedTempStoreFactory
  36. */
  37. protected $tempStoreFactory;
  38. /**
  39. * Constructs a new ViewUIConverter.
  40. *
  41. * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
  42. * The entity manager.
  43. * @param \Drupal\user\SharedTempStoreFactory $temp_store_factory
  44. * The factory for the temp store object.
  45. */
  46. public function __construct(EntityManagerInterface $entity_manager, SharedTempStoreFactory $temp_store_factory, ConfigFactoryInterface $config_factory = NULL, AdminContext $admin_context = NULL) {
  47. // The config factory and admin context are new arguments due to changing
  48. // the parent. Avoid an error on updated sites by falling back to getting
  49. // them from the container.
  50. // @todo Remove in 8.2.x in https://www.drupal.org/node/2674328.
  51. if (!$config_factory) {
  52. $config_factory = \Drupal::configFactory();
  53. }
  54. if (!$admin_context) {
  55. $admin_context = \Drupal::service('router.admin_context');
  56. }
  57. parent::__construct($entity_manager, $config_factory, $admin_context);
  58. $this->tempStoreFactory = $temp_store_factory;
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function convert($value, $definition, $name, array $defaults) {
  64. if (!$entity = parent::convert($value, $definition, $name, $defaults)) {
  65. return;
  66. }
  67. // Get the temp store for this variable if it needs one. Attempt to load the
  68. // view from the temp store, synchronize its status with the existing view,
  69. // and store the lock metadata.
  70. $store = $this->tempStoreFactory->get('views');
  71. if ($view = $store->get($value)) {
  72. if ($entity->status()) {
  73. $view->enable();
  74. }
  75. else {
  76. $view->disable();
  77. }
  78. $view->lock = $store->getMetadata($value);
  79. }
  80. // Otherwise, decorate the existing view for use in the UI.
  81. else {
  82. $view = new ViewUI($entity);
  83. }
  84. return $view;
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. public function applies($definition, $name, Route $route) {
  90. if (parent::applies($definition, $name, $route)) {
  91. return !empty($definition['tempstore']) && $definition['type'] === 'entity:view';
  92. }
  93. return FALSE;
  94. }
  95. }