Commit 8caad964 authored by alexpott's avatar alexpott

Issue #2098011 by larowlan: Support dynamic route parameters to enhance...

Issue #2098011 by larowlan: Support dynamic route parameters to enhance comment admin paths with entity type and field name from comment bundle.
parent ac235ef8
......@@ -913,7 +913,8 @@ function comment_form_field_ui_field_instance_edit_form_alter(&$form, $form_stat
*/
function comment_form_field_ui_field_overview_form_alter(&$form, $form_state) {
if ($form['#entity_type'] == 'comment') {
$form['#title'] = \Drupal::service('comment.manager')->getFieldUIPageTitle($form['#bundle']);
$request = \Drupal::request();
$form['#title'] = \Drupal::service('comment.manager')->getFieldUIPageTitle($request->attributes->get('commented_entity_type'), $request->attributes->get('field_name'));
}
}
......@@ -922,7 +923,8 @@ function comment_form_field_ui_field_overview_form_alter(&$form, $form_state) {
*/
function comment_form_field_ui_form_display_overview_form_alter(&$form, $form_state) {
if ($form['#entity_type'] == 'comment') {
$form['#title'] = \Drupal::service('comment.manager')->getFieldUIPageTitle($form['#bundle']);
$request = \Drupal::request();
$form['#title'] = \Drupal::service('comment.manager')->getFieldUIPageTitle($request->attributes->get('commented_entity_type'), $request->attributes->get('field_name'));
}
}
......@@ -931,7 +933,8 @@ function comment_form_field_ui_form_display_overview_form_alter(&$form, $form_st
*/
function comment_form_field_ui_display_overview_form_alter(&$form, $form_state) {
if ($form['#entity_type'] == 'comment') {
$form['#title'] = \Drupal::service('comment.manager')->getFieldUIPageTitle($form['#bundle']);
$request = \Drupal::request();
$form['#title'] = \Drupal::service('comment.manager')->getFieldUIPageTitle($request->attributes->get('commented_entity_type'), $request->attributes->get('field_name'));
}
}
......
......@@ -73,7 +73,7 @@ comment.bundle_list:
_permission: 'administer comments'
comment.bundle:
path: '/admin/structure/comments/manage/{field_name}'
path: '/admin/structure/comments/manage/{bundle}'
defaults:
_content: 'Drupal\comment\Controller\AdminController::bundleInfo'
_title_callback: 'Drupal\comment\Controller\AdminController::bundleTitle'
......
......@@ -8,3 +8,9 @@ services:
comment.manager:
class: Drupal\comment\CommentManager
arguments: ['@field.info', '@entity.manager']
comment.route_enhancer:
class: Drupal\comment\Routing\CommentBundleEnhancer
arguments: ['@entity.manager']
tags:
- { name: route_enhancer}
......@@ -190,12 +190,11 @@ public function addBodyField($entity_type, $field_name) {
/**
* {@inheritdoc}
*/
public function getFieldUIPageTitle($field_name) {
list($entity_type, $field) = explode('__', $field_name, 2);
$field_info = $this->fieldInfo->getField($entity_type, $field);
public function getFieldUIPageTitle($commented_entity_type, $field_name) {
$field_info = $this->fieldInfo->getField($commented_entity_type, $field_name);
$bundles = $field_info->getBundles();
$sample_bundle = reset($bundles);
$sample_instance = $this->fieldInfo->getInstance($entity_type, $sample_bundle, $field);
$sample_instance = $this->fieldInfo->getInstance($commented_entity_type, $sample_bundle, $field_name);
return String::checkPlain($sample_instance->label);
}
......
......@@ -76,12 +76,14 @@ public function addBodyField($entity_type, $field_name);
/**
* Builds human readable page title for field_ui management screens.
*
* @param string $commented_entity_type
* The entity type to which the comment field is attached.
* @param string $field_name
* The comment field for which the overview is to be displayed.
*
* @return string
* The human readable field name.
*/
public function getFieldUIPageTitle($field_name);
public function getFieldUIPageTitle($commented_entity_type, $field_name);
}
......@@ -169,6 +169,8 @@ public function overviewBundles() {
/**
* Returns an overview of the entity types a comment field is attached to.
*
* @param string $commented_entity_type
* The entity type to which the comment field is attached.
* @param string $field_name
* The comment field for which the overview is to be displayed.
*
......@@ -176,17 +178,14 @@ public function overviewBundles() {
* A renderable array containing the list of entity types and bundle
* combinations on which the comment field is in use.
*/
public function bundleInfo($field_name) {
public function bundleInfo($commented_entity_type, $field_name) {
// Add a link to manage entity fields if the Field UI module is enabled.
$field_ui_enabled = $this->moduleHandler()->moduleExists('field_ui');
// @todo Provide dynamic routing to get entity type and field name.
// https://drupal.org/node/2098011.
list($entity_type, $field) = explode('__', $field_name, 2);
$field_info = $this->fieldInfo->getField($entity_type, $field);
$field_info = $this->fieldInfo->getField($commented_entity_type, $field_name);
$entity_type_info = $this->entityManager()->getDefinition($entity_type);
$entity_bundle_info = $this->entityManager()->getBundleInfo($entity_type);
$entity_type_info = $this->entityManager()->getDefinition($commented_entity_type);
$entity_bundle_info = $this->entityManager()->getBundleInfo($commented_entity_type);
$build['usage'] = array(
'#theme' => 'item_list',
......@@ -196,7 +195,7 @@ public function bundleInfo($field_name) {
// Loop over all of bundles to which this comment field is attached.
foreach ($field_info->getBundles() as $bundle) {
// Add the current instance to the list of bundles.
if ($field_ui_enabled && ($route_info = $this->entityManager()->getAdminRouteInfo($entity_type, $bundle))) {
if ($field_ui_enabled && ($route_info = $this->entityManager()->getAdminRouteInfo($commented_entity_type, $bundle))) {
// Add a link to configure the fields on the given bundle and entity
// type combination.
$build['usage']['#items'][] = $this->l($entity_bundle_info[$bundle]['label'], $route_info['route_name'], $route_info['route_parameters']);
......@@ -214,14 +213,16 @@ public function bundleInfo($field_name) {
/**
* Route title callback.
*
* @param string $commented_entity_type
* The entity type to which the comment field is attached.
* @param string $field_name
* The comment field for which the overview is to be displayed.
*
* @return string
* The human readable field name.
*/
public function bundleTitle($field_name) {
return $this->commentManager->getFieldUIPageTitle($field_name);
public function bundleTitle($commented_entity_type, $field_name) {
return $this->commentManager->getFieldUIPageTitle($commented_entity_type, $field_name);
}
}
<?php
/**
* @file
* Contains \Drupal\comment\Routing\CommentBundleEnhancer.
*/
namespace Drupal\comment\Routing;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Cmf\Component\Routing\Enhancer\RouteEnhancerInterface;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Drupal\Core\Entity\EntityManager;
/**
* Constructs a route enhancer to extract values from comment bundles.
*
* Comment bundle names are of the form {entity_type}__{field_name}. This
* enhancer extracts them from the path and makes them available as arguments
* to controllers.
*/
class CommentBundleEnhancer implements RouteEnhancerInterface {
/**
* The entity manager service.
*
* @var \Drupal\Core\Entity\EntityManager
*/
protected $entityManager;
/**
* Constructs a CommentBundleEnhancer object.
*
* @param \Drupal\Core\Entity\EntityManager $entity_manager
* The entity manager service.
*/
public function __construct(EntityManager $entity_manager) {
$this->entityManager = $entity_manager;
}
/**
* {@inheritdoc}
*/
public function enhance(array $defaults, Request $request) {
if (isset($defaults['bundle']) && ($bundles = $this->entityManager->getBundleInfo('comment')) && isset($bundles[$defaults['bundle']])) {
list($entity_type, $field_name) = explode('__', $defaults['bundle'], 2);
$defaults['commented_entity_type'] = $entity_type;
$defaults['field_name'] = $field_name;
}
return $defaults;
}
}
<?php
/**
* @file
* Contains \Drupal\comment\Tests\Routing\CommentBundleEnhancerTest.
*/
namespace Drupal\comment\Tests\Routing;
use Drupal\comment\Routing\CommentBundleEnhancer;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\HttpFoundation\Request;
/**
* Tests the comment bundle enhancer route enhancer.
*
* @see \Drupal\comment\Routing\CommentBundleEnhancer
*/
class CommentBundleEnhancerTest extends UnitTestCase {
public static function getInfo() {
return array(
'name' => 'Comment route enhancer test',
'description' => 'Tests the comment route enhancer.',
'group' => 'Comment',
);
}
/**
* Data provider for testEnhancer().
*
* @see testEnhancer()
*
* @return array
* An array of arrays containing strings:
* - The bundle name.
* - The commented entity type.
* - The field name.
*/
public function providerTestEnhancer() {
return array(
array(
'node__comment',
'comment',
'node',
),
array(
'node__comment_forum__schnitzel',
'comment_forum__schnitzel',
'node',
),
array(
'node__field_foobar',
FALSE,
FALSE
),
);
}
/**
* Tests the enhancer method.
*
* @param string $bundle
* The bundle name to test.
* @param string $field_name
* The field name expected to be extracted from the bundle.
* @param string $commented_entity_type
* The entity type expected to be extracted from the bundle.
*
* @see \Drupal\comment\Routing\CommentBundleEnhancer::enhancer()
*
* @group Drupal
* @group Routing
*
* @dataProvider providerTestEnhancer
*/
public function testEnhancer($bundle, $field_name, $commented_entity_type) {
$entityManager = $this->getMockBuilder('\Drupal\Core\Entity\EntityManager')
->disableOriginalConstructor()
->getMock();
$entityManager->expects($this->any())
->method('getBundleInfo')
->will($this->returnValue(array(
'node__comment' => array(),
// Test two sets of __.
'node__comment_forum__schnitzel' => array(),
)));
$route_enhancer = new CommentBundleEnhancer($entityManager);
// Test the enhancer.
$request = new Request();
$defaults = array('bundle' => $bundle);
$new_defaults = $route_enhancer->enhance($defaults, $request);
if ($commented_entity_type) {
// A valid comment bundle.
$this->assertEquals($new_defaults['field_name'], $field_name);
$this->assertEquals($new_defaults['commented_entity_type'], $commented_entity_type);
}
else {
// Non-comment bundle.
$this->assertTrue(empty($new_defaults['field_name']));
$this->assertTrue(empty($new_defaults['commented_entity_type']));
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment