Including security review as a submodule - with patched for Yaffs.
[yaffs-website] / web / modules / contrib / entity_browser / src / RouteSubscriber.php
1 <?php
2
3 namespace Drupal\entity_browser;
4
5 use Drupal\Core\Entity\EntityTypeManagerInterface;
6 use Drupal\Core\Entity\Query\QueryFactory;
7 use Symfony\Component\Routing\RouteCollection;
8
9 /**
10  * Generates routes for entity browsers.
11  */
12 class RouteSubscriber {
13
14   /**
15    * The entity browser storage.
16    *
17    * @var \Drupal\Core\Entity\EntityStorageInterface
18    */
19   protected $browserStorage;
20
21   /**
22    * Display plugin manager.
23    *
24    * @var \Drupal\entity_browser\DisplayManager
25    */
26   protected $displayManager;
27
28   /**
29    * Entity browser query.
30    *
31    * @var \Drupal\Core\Entity\Query\QueryInterface
32    */
33   protected $browserQuery;
34
35   /**
36    * Constructs a \Drupal\views\EventSubscriber\RouteSubscriber instance.
37    *
38    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
39    *   The entity manager.
40    * @param \Drupal\entity_browser\DisplayManager $display_manager
41    *   The display manager.
42    * @param \Drupal\Core\Entity\Query\QueryFactory $entity_query
43    *   The entity query factory.
44    */
45   public function __construct(EntityTypeManagerInterface $entity_type_manager, DisplayManager $display_manager, QueryFactory $entity_query) {
46     $this->browserStorage = $entity_type_manager->getStorage('entity_browser');
47     $this->displayManager = $display_manager;
48     $this->browserQuery = $entity_query->get('entity_browser');
49   }
50
51   /**
52    * Returns a set of route objects.
53    *
54    * @return \Symfony\Component\Routing\RouteCollection
55    *   A route collection.
56    */
57   public function routes() {
58     $collection = new RouteCollection();
59     // Return $collection;.
60     foreach ($this->getBrowserIDsWithRoute() as $id) {
61       /** @var $browser \Drupal\entity_browser\EntityBrowserInterface */
62       $browser = $this->browserStorage->load($id);
63       if ($route = $browser->route()) {
64         $collection->add('entity_browser.' . $browser->id(), $route);
65       }
66     }
67
68     return $collection;
69   }
70
71   /**
72    * Gets entity browser IDs that use routes.
73    *
74    * @return array
75    *   Array of browser IDs.
76    */
77   protected function getBrowserIDsWithRoute() {
78     // Get all display plugins which provides the type.
79     $display_plugins = $this->displayManager->getDefinitions();
80     $ids = array();
81     foreach ($display_plugins as $id => $definition) {
82       if (!empty($definition['uses_route'])) {
83         $ids[$id] = $id;
84       }
85     }
86
87     return $this->browserQuery
88       ->condition('status', TRUE)
89       ->condition("display", $ids, 'IN')
90       ->execute();
91   }
92
93 }