Completed
Pull Request — 8.x-1.x (#130)
by
unknown
07:20
created

StandalonePage::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4286
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
/**
4
 * @file
5
 * Contains \Drupal\entity_browser\Controllers\StandalonePage.
6
 */
7
8
namespace Drupal\entity_browser\Controllers;
9
10
use Drupal\Component\Utility\Xss;
11
use Drupal\Core\Controller\ControllerBase;
12
use Drupal\Core\Entity\EntityManagerInterface;
13
use Drupal\Core\Routing\RouteMatchInterface;
14
use Symfony\Component\DependencyInjection\ContainerInterface;
15
use Symfony\Component\HttpFoundation\Request;
16
17
/**
18
 * Standalone entity browser page.
19
 */
20
class StandalonePage extends ControllerBase {
21
22
  /**
23
   * Current route match service.
24
   *
25
   * @var \Drupal\Core\Routing\RouteMatchInterface
26
   */
27
  protected $currentRouteMatch;
28
29
  /**
30
   * The browser storage.
31
   *
32
   * @var \Drupal\Core\Entity\EntityStorageInterface
33
   */
34
  protected $browserStorage;
35
36
  /**
37
   * Current request.
38
   *
39
   * @var \Symfony\Component\HttpFoundation\Request
40
   */
41
  protected $request;
42
43
  /**
44
   * Constructs StandalonePage route controller.
45
   *
46
   * @param RouteMatchInterface $route_match
47
   *   Current route match service.
48
   * @param EntityManagerInterface $entity_manager
49
   *   Entity manager service.
50
   * @param \Symfony\Component\HttpFoundation\Request $request
51
   *   Current request.
52
   */
53
  public function __construct(RouteMatchInterface $route_match, EntityManagerInterface $entity_manager, Request $request) {
54
    $this->currentRouteMatch = $route_match;
55
    $this->browserStorage = $entity_manager->getStorage('entity_browser');
56
    $this->request = $request;
57
  }
58
59
  /**
60
   * {@inheritdoc}
61
   */
62
  public static function create(ContainerInterface $container) {
63
    return new static(
64
      $container->get('current_route_match'),
65
      $container->get('entity.manager'),
66
      $container->get('request_stack')->getCurrentRequest()
67
    );
68
  }
69
70
  /**
71
   * Test implementation of standalone entity browser page.
72
   */
73
  public function page() {
74
    $browser = $this->loadBrowser();
75
76
    // The original path is sometimes needed: ie for views arguments.
77
    if ($original_path = $this->request->get('original_path')) {
78
      $browser->addAdditionalWidgetParameters(['path_parts' => explode('/', $original_path)]);
79
    }
80
81
    return $this->entityFormBuilder()->getForm($browser, 'entity_browser');
82
  }
83
84
  /**
85
   * Standalone entity browser title callback.
86
   */
87
  public function title() {
88
    $browser = $this->loadBrowser();
89
    return Xss::filter($browser->label());
90
  }
91
92
  /**
93
   * Loads entity browser object for this page.
94
   *
95
   * @return \Drupal\entity_browser\EntityBrowserInterface
96
   *   Loads the entity browser object
97
   */
98
  protected function loadBrowser() {
99
    /** @var $route \Symfony\Component\Routing\Route */
100
    $route = $this->currentRouteMatch->getRouteObject();
101
    /** @var $browser \Drupal\entity_browser\EntityBrowserInterface */
102
    return $this->browserStorage->load($route->getDefault('entity_browser_id'));
103
  }
104
105
}
106