Skip to content
This repository has been archived by the owner on Jan 5, 2018. It is now read-only.

Commit

Permalink
by slashrsm: Make StandalonePage::page return form that entity browse…
Browse files Browse the repository at this point in the history
…r builds + dynamically generate permissions to access generated routes.
  • Loading branch information
slashrsm committed Oct 2, 2014
1 parent 8e096a3 commit 2614825
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 36 deletions.
28 changes: 14 additions & 14 deletions config/schema/entity_browser.schema.yml
Expand Up @@ -26,21 +26,21 @@ entity_browser.browser.*:
widget_selector_configuration:
type: entity_browser.browser.widget_selector.[%widget_selector]
widgets:
type: sequence
type: mapping
label: 'Widgets'
sequence:
- type: mapping
mapping:
id:
type: string
uuid:
type: string
label:
type: string
weight:
type: integer
settings:
type: entity_browser.browser.widget.[%parent.id]
mapping:
type: mapping
mapping:
id:
type: string
uuid:
type: string
label:
type: string
weight:
type: integer
settings:
type: entity_browser.browser.widget.[%parent.id]

entity_browser.browser.display.standalone:
type: mapping
Expand Down
2 changes: 2 additions & 0 deletions entity_browser.permissions.yml
Expand Up @@ -2,3 +2,5 @@ administer entity browsers:
title: 'Administer entity browsers'
description: 'Create and modify entity browsers for generating browsing, creating and selecting entities.'
restrict access: TRUE
permission_callbacks:
- \Drupal\entity_browser\Permissions:permissions
78 changes: 62 additions & 16 deletions src/Controllers/StandalonePage.php
@@ -1,32 +1,78 @@
<?php

/**
* @file
* Contains \Drupal\entity_browser\Controllers\StandalonePage.
*/
/**
* @file
* Contains \Drupal\entity_browser\Controllers\StandalonePage.
*/

namespace Drupal\entity_browser\Controllers;

use Drupal\Component\Utility\Xss;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Standalone entity browser page.
*/
/**
* Standalone entity browser page.
*/
class StandalonePage extends ControllerBase {

/**
* Test implementation of standalone entity browser page.
* Current route match service.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
protected $currentRouteMatch;

/**
* The browser storage.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The request object.
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $browserStorage;

public function __construct(RouteMatchInterface $route_match, EntityManagerInterface $entity_manager) {
$this->currentRouteMatch = $route_match;
$this->browserStorage = $entity_manager->getStorage('entity_browser');
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('current_route_match'),
$container->get('entity.manager')
);
}

/**
* Test implementation of standalone entity browser page.
*/
public function page() {
$browser = $this->loadBrowser();
return \Drupal::formBuilder()->getForm($browser);
}

/**
* Standalone entity browser title callback.
*/
public function title() {
$browser = $this->loadBrowser();
return Xss::filter($browser->label());
}

/**
* Loads entity browser object for this page.
*
* @return \Symfony\Component\HttpFoundation\Response
* The standalone entity browser page.
* @return \Drupal\entity_browser\EntityBrowserInterface
*/
public function page(Request $request) {
// @TODO Implement.
return array();
protected function loadBrowser() {
/** @var $route \Symfony\Component\Routing\Route */
$route = $this->currentRouteMatch->getRouteObject();
/** @var $browser \Drupal\entity_browser\EntityBrowserInterface */
return $this->browserStorage->load($route->getDefault('entity_browser_id'));
}

}
21 changes: 16 additions & 5 deletions src/Entity/EntityBrowser.php
Expand Up @@ -7,6 +7,7 @@

namespace Drupal\entity_browser\Entity;

use Drupal\Component\Utility\String;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Entity\EntityWithPluginBagsInterface;
use Drupal\Core\Form\FormStateInterface;
Expand Down Expand Up @@ -287,14 +288,19 @@ public function selectionCompleted() {
* {@inheritdoc}
*/
public function getFormId() {
// @TODO Implement it.
return 'entity_browser_' . $this->id() . '_form';
}

/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
// @TODO Implement it.
$form['widget_selector'] = $this->getWidgetSelector()->getForm();
$active_widget = $this->getWidgetSelector()->getCurrentWidget();
$form['widget'] = $this->getWidget($active_widget)->getForm();
$form['selection_display'] = $this->getSelectionDisplay()->getForm();

return $form;
}

/**
Expand All @@ -316,14 +322,19 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
*/
public function route() {
$defaults = array(
'_controller' => 'Drupal\entity_browser\Controllers\StandalonePage::page',
'_content' => 'Drupal\entity_browser\Controllers\StandalonePage::page',
'_title_callback' => 'Drupal\entity_browser\Controllers\StandalonePage::title',
'entity_browser_id' => $this->id(),
);

$requirements = array(
'_permission' => 'access ' . String::checkPlain($this->id()) . ' entity browser pages',
);

$display = $this->getDisplay();
if ($display instanceof DisplayRouterInterface) {
$path = $display->path();
//debug($path);
return new Route($path, $defaults);
return new Route($path, $defaults, $requirements);
}

return FALSE;
Expand Down
Expand Up @@ -8,7 +8,7 @@ selection_display_configuration: { }
widget_selector: tabs
widget_selector_configuration: { }
widgets:
-
view:
id: view
label: 'View widget'
weight: 0
Expand Down

0 comments on commit 2614825

Please sign in to comment.