diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module index 1081ed4..6b4d193 100644 --- a/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -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')); } } diff --git a/core/modules/comment/comment.routing.yml b/core/modules/comment/comment.routing.yml index e2343fb..46089bf 100644 --- a/core/modules/comment/comment.routing.yml +++ b/core/modules/comment/comment.routing.yml @@ -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' diff --git a/core/modules/comment/comment.services.yml b/core/modules/comment/comment.services.yml index a82bbe4..084f9b8 100644 --- a/core/modules/comment/comment.services.yml +++ b/core/modules/comment/comment.services.yml @@ -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} diff --git a/core/modules/comment/lib/Drupal/comment/CommentManager.php b/core/modules/comment/lib/Drupal/comment/CommentManager.php index 5454a1b..262d417 100644 --- a/core/modules/comment/lib/Drupal/comment/CommentManager.php +++ b/core/modules/comment/lib/Drupal/comment/CommentManager.php @@ -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); } diff --git a/core/modules/comment/lib/Drupal/comment/CommentManagerInterface.php b/core/modules/comment/lib/Drupal/comment/CommentManagerInterface.php index b4c16e3..84a3589 100644 --- a/core/modules/comment/lib/Drupal/comment/CommentManagerInterface.php +++ b/core/modules/comment/lib/Drupal/comment/CommentManagerInterface.php @@ -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); } diff --git a/core/modules/comment/lib/Drupal/comment/Controller/AdminController.php b/core/modules/comment/lib/Drupal/comment/Controller/AdminController.php index 38260ec..451a62d 100644 --- a/core/modules/comment/lib/Drupal/comment/Controller/AdminController.php +++ b/core/modules/comment/lib/Drupal/comment/Controller/AdminController.php @@ -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); } } diff --git a/core/modules/comment/lib/Drupal/comment/Routing/CommentBundleEnhancer.php b/core/modules/comment/lib/Drupal/comment/Routing/CommentBundleEnhancer.php new file mode 100644 index 0000000..025e6ca --- /dev/null +++ b/core/modules/comment/lib/Drupal/comment/Routing/CommentBundleEnhancer.php @@ -0,0 +1,54 @@ +entityManager = $entity_manager; + } + + /** + * {@inheritdoc} + */ + public function enhance(array $defaults, Request $request) { + $bundles = $this->entityManager->getBundleInfo('comment'); + if (isset($defaults['bundle']) && 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; + } + +} diff --git a/core/modules/comment/tests/Drupal/comment/Tests/Routing/CommentBundleEnhancerTest.php b/core/modules/comment/tests/Drupal/comment/Tests/Routing/CommentBundleEnhancerTest.php new file mode 100644 index 0000000..d86d1ba --- /dev/null +++ b/core/modules/comment/tests/Drupal/comment/Tests/Routing/CommentBundleEnhancerTest.php @@ -0,0 +1,101 @@ + '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() + * + * @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'])); + } + } + +}