You are here

class RouteSubscriber in Entity Browser 8

Same name and namespace in other branches
  1. 8.2 src/RouteSubscriber.php \Drupal\entity_browser\RouteSubscriber

Generates routes for entity browsers.

Hierarchy

Expanded class hierarchy of RouteSubscriber

1 string reference to 'RouteSubscriber'
entity_browser.services.yml in ./entity_browser.services.yml
entity_browser.services.yml
1 service uses RouteSubscriber
entity_browser.route_subscriber in ./entity_browser.services.yml
Drupal\entity_browser\RouteSubscriber

File

src/RouteSubscriber.php, line 11

Namespace

Drupal\entity_browser
View source
class RouteSubscriber {

  /**
   * The entity browser storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $browserStorage;

  /**
   * Display plugin manager.
   *
   * @var \Drupal\entity_browser\DisplayManager
   */
  protected $displayManager;

  /**
   * Constructs a \Drupal\views\EventSubscriber\RouteSubscriber instance.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\entity_browser\DisplayManager $display_manager
   *   The display manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, DisplayManager $display_manager) {
    $this->browserStorage = $entity_type_manager
      ->getStorage('entity_browser');
    $this->displayManager = $display_manager;
  }

  /**
   * Returns a set of route objects.
   *
   * @return \Symfony\Component\Routing\RouteCollection
   *   A route collection.
   */
  public function routes() {
    $collection = new RouteCollection();
    foreach ($this
      ->getBrowserIDsWithRoute() as $id) {

      /** @var $browser \Drupal\entity_browser\EntityBrowserInterface */
      $browser = $this->browserStorage
        ->load($id);
      if ($route = $browser
        ->route()) {
        $collection
          ->add('entity_browser.' . $browser
          ->id(), $route);
      }
    }
    return $collection;
  }

  /**
   * Gets entity browser IDs that use routes.
   *
   * @return array
   *   Array of browser IDs.
   */
  protected function getBrowserIDsWithRoute() {

    // Get all display plugins which provides the type.
    $display_plugins = $this->displayManager
      ->getDefinitions();
    $ids = [];
    foreach ($display_plugins as $id => $definition) {
      if (!empty($definition['uses_route'])) {
        $ids[$id] = $id;
      }
    }
    return $this->browserStorage
      ->getQuery()
      ->condition('status', TRUE)
      ->condition("display", $ids, 'IN')
      ->execute();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RouteSubscriber::$browserStorage protected property The entity browser storage.
RouteSubscriber::$displayManager protected property Display plugin manager.
RouteSubscriber::getBrowserIDsWithRoute protected function Gets entity browser IDs that use routes.
RouteSubscriber::routes public function Returns a set of route objects.
RouteSubscriber::__construct public function Constructs a \Drupal\views\EventSubscriber\RouteSubscriber instance.