Created
September 1, 2013 20:58
-
-
Save andypost/6407260 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module | |
index 51f3509..613d6c6 100644 | |
--- a/core/modules/comment/comment.module | |
+++ b/core/modules/comment/comment.module | |
@@ -123,10 +123,12 @@ function comment_help($path, $arg) { | |
*/ | |
function comment_entity_bundle_info() { | |
$bundles = array(); | |
- foreach (Drupal::service('comment.manager')->getFields() as $field_name => $field_info) { | |
- $bundles['comment'][$field_name] = array( | |
- 'label' => $field_name, | |
- ); | |
+ foreach (Drupal::service('comment.manager')->getAllFields() as $entity_type => $fields) { | |
+ foreach ($fields as $field_name => $field_info) { | |
+ $bundles['comment'][$entity_type . '_' . $field_name] = array( | |
+ 'label' => $entity_type . '_' . $field_name, | |
+ ); | |
+ } | |
} | |
return $bundles; | |
} | |
@@ -140,6 +142,8 @@ function comment_entity_bundle_info() { | |
* @return string|false | |
* The field name as of the comment field entity or FALSE if $field_name | |
* is not a comment field. | |
+ * | |
+ * @todo remove as unused. | |
*/ | |
function comment_field_name_load($field_name) { | |
if (($field = entity_load('field_entity', $field_name)) && $field->getFieldType() == 'comment') { | |
@@ -164,21 +168,23 @@ function comment_uri(Comment $comment) { | |
*/ | |
function comment_field_extra_fields() { | |
$return = array(); | |
- foreach (Drupal::service('comment.manager')->getFields() as $field_name => $field_info) { | |
- $return['comment'][$field_name] = array( | |
- 'form' => array( | |
- 'author' => array( | |
- 'label' => t('Author'), | |
- 'description' => t('Author textfield'), | |
- 'weight' => -2, | |
+ foreach (Drupal::service('comment.manager')->getAllFields() as $entity_type => $fields) { | |
+ foreach ($fields as $field_name => $field_info) { | |
+ $return['comment'][$entity_type . '_' . $field_name] = array( | |
+ 'form' => array( | |
+ 'author' => array( | |
+ 'label' => t('Author'), | |
+ 'description' => t('Author textfield'), | |
+ 'weight' => -2, | |
+ ), | |
+ 'subject' => array( | |
+ 'label' => t('Subject'), | |
+ 'description' => t('Subject textfield'), | |
+ 'weight' => -1, | |
+ ), | |
), | |
- 'subject' => array( | |
- 'label' => t('Subject'), | |
- 'description' => t('Subject textfield'), | |
- 'weight' => -1, | |
- ), | |
- ), | |
- ); | |
+ ); | |
+ } | |
} | |
return $return; | |
@@ -353,20 +359,6 @@ function comment_reply_access(EntityInterface $entity) { | |
return !in_array(COMMENT_ACCESS_DENY, $access, TRUE); | |
} | |
- /** | |
- * Creates a comment_body field instance. | |
- * | |
- * @param string $entity_type | |
- * Entity type to which the comment field is attached. | |
- * @param string $bundle_name | |
- * Bundle of entity type to which comment field is attached | |
- * @param string $field_name | |
- * Name of the comment field attached to the entity type and bundle. | |
- */ | |
-function _comment_body_field_create($entity_type, $bundle_name, $field_name) { | |
- Drupal::service('comment.manager')->addBodyField($field_name); | |
-} | |
- | |
/** | |
* Implements hook_permission(). | |
*/ | |
@@ -1022,8 +1014,7 @@ function comment_translation_configuration_element_submit($form, &$form_state) { | |
* Implements hook_entity_load(). | |
*/ | |
function comment_entity_load($entities, $entity_type) { | |
- $fields = Drupal::service('comment.manager')->getFields($entity_type); | |
- if (!$fields) { | |
+ if (!Drupal::service('comment.manager')->getFields($entity_type)) { | |
// Do not query database when entity has no comment fields. | |
return; | |
} | |
@@ -1824,7 +1815,7 @@ function comment_library_info() { | |
*/ | |
function comment_field_instance_create(FieldInstanceInterface $instance) { | |
if ($instance->getFieldType() == 'comment') { | |
- Drupal::service('comment.manager')->addBodyField($instance->getFieldName()); | |
+ Drupal::service('comment.manager')->addBodyField($instance->entity_type, $instance->getFieldName()); | |
Drupal::cache()->delete('comment_entity_info'); | |
} | |
} | |
diff --git a/core/modules/comment/lib/Drupal/comment/CommentManager.php b/core/modules/comment/lib/Drupal/comment/CommentManager.php | |
index 9447a31..9368bd6 100644 | |
--- a/core/modules/comment/lib/Drupal/comment/CommentManager.php | |
+++ b/core/modules/comment/lib/Drupal/comment/CommentManager.php | |
@@ -62,12 +62,11 @@ public function getParentEntityUri(CommentInterface $comment) { | |
* Utility function to return an array of comment fields. | |
* | |
* @param string $entity_type | |
- * (optional) Specify a entity type if you want to just return fields which | |
- * are attached on a certain entity type. Defaults to NULL. | |
+ * The entity type to return fields which are attached on. | |
* | |
* @return array | |
- * An array of comment field map definitions, keyed by field name. Each value | |
- * is an array with two entries: | |
+ * An array of comment field map definitions, keyed by field name. Each | |
+ * value is an array with two entries: | |
* - type: The field type. | |
* - bundles: The bundles in which the field appears, as an array with entity | |
* types as keys and the array of bundle names as values. | |
@@ -75,14 +74,29 @@ public function getParentEntityUri(CommentInterface $comment) { | |
* @see field_info_field_map(). | |
*/ | |
public function getFields($entity_type = NULL) { | |
- return array_filter($this->fieldInfo->getFieldMap(), function ($value) use($entity_type) { | |
- if ($value['type'] == 'comment') { | |
- if (isset($entity_type)) { | |
- return isset($value['bundles'][$entity_type]); | |
+ $map = $this->getAllFields(); | |
+ if (!isset($map[$entity_type])) { | |
+ return array(); | |
+ } | |
+ return $map[$entity_type]; | |
+ } | |
+ | |
+ /** | |
+ * Utility function to return all comment fields. | |
+ */ | |
+ public function getAllFields() { | |
+ $map = $this->fieldInfo->getFieldMap(); | |
+ // Build a list of comment fields only. | |
+ $comment_fields = array(); | |
+ foreach ($map as $entity_type => $data) { | |
+ // $comment_fields[$entity_type] = array_filter($data, function ($value) { return ($value['type'] == 'comment'); }); | |
+ foreach ($data as $field_name => $field_info) { | |
+ if ($field_info['type'] == 'comment') { | |
+ $comment_fields[$entity_type][$field_name] = $field_info; | |
} | |
- return TRUE; | |
} | |
- }); | |
+ } | |
+ return $comment_fields; | |
} | |
/** | |
@@ -103,10 +117,11 @@ public function getFields($entity_type = NULL) { | |
*/ | |
public function addDefaultField($entity_type, $bundle, $field_name = 'comment', $default_value = COMMENT_OPEN) { | |
// Make sure field doesn't already exist. | |
- if (!$this->fieldInfo->getField($field_name)) { | |
+ if (!$this->fieldInfo->getField($entity_type, $field_name)) { | |
// Add a default comment field for existing node comments. | |
$field = $this->entityManager->getStorageController('field_entity')->create(array( | |
- 'field_name' => $field_name, | |
+ 'entity_type' => $entity_type, | |
+ 'name' => $field_name, | |
'type' => 'comment', | |
'translatable' => '0', | |
)); | |
@@ -143,48 +158,54 @@ public function addDefaultField($entity_type, $bundle, $field_name = 'comment', | |
)) | |
->save(); | |
} | |
- $this->addBodyField($field_name); | |
+ $this->addBodyField($entity_type, $field_name); | |
} | |
/** | |
* Creates a comment_body field instance. | |
* | |
+ * @param string $entity_type | |
+ * The type of the entity to which the comment field attached. | |
* @param string $field_name | |
- * Name of the comment field, a bundle to add comment_body field. | |
+ * Name of the comment field to add comment_body field. | |
*/ | |
- public function addBodyField($field_name) { | |
+ public function addBodyField($entity_type, $field_name) { | |
// Create the field if needed. | |
- $field = $this->entityManager->getStorageController('field_entity')->load('comment_body'); | |
+ $field = $this->entityManager->getStorageController('field_entity')->load('comment.comment_body'); | |
if (!$field) { | |
$field = $this->entityManager->getStorageController('field_entity')->create(array( | |
- 'field_name' => 'comment_body', | |
+ 'name' => 'comment_body', | |
'type' => 'text_long', | |
+ 'entity_type' => 'comment', | |
)); | |
$field->save(); | |
} | |
// Create the instance if needed, field name defaults to 'comment'. | |
- $field_instance = $this->entityManager->getStorageController('field_instance')->load("comment.$field_name.comment_body"); | |
+ $comment_bundle = $entity_type . '_' . $field_name; | |
+ $field_instance = $this->entityManager | |
+ ->getStorageController('field_instance') | |
+ ->load("comment.$comment_bundle.comment_body"); | |
if (!$field_instance) { | |
// Attaches the body field by default. | |
$field_instance = $this->entityManager->getStorageController('field_instance')->create(array( | |
'field_name' => 'comment_body', | |
'label' => 'Comment', | |
'entity_type' => 'comment', | |
- 'bundle' => $field_name, | |
+ 'bundle' => $comment_bundle, | |
'settings' => array('text_processing' => 1), | |
'required' => TRUE, | |
)); | |
$field_instance->save(); | |
// Assign widget settings for the 'default' form mode. | |
- entity_get_form_display('comment', $field_name, 'default') | |
+ entity_get_form_display('comment', $comment_bundle, 'default') | |
->setComponent('comment_body', array( | |
'type' => 'text_textarea', | |
)) | |
->save(); | |
// Assign display settings for the 'default' view mode. | |
- entity_get_display('comment', $field_name, 'default') | |
+ entity_get_display('comment', $comment_bundle, 'default') | |
->setComponent('comment_body', array( | |
'label' => 'hidden', | |
'type' => 'text_default', | |
diff --git a/core/modules/comment/lib/Drupal/comment/Controller/AdminController.php b/core/modules/comment/lib/Drupal/comment/Controller/AdminController.php | |
index 5713b95..2690cf8 100644 | |
--- a/core/modules/comment/lib/Drupal/comment/Controller/AdminController.php | |
+++ b/core/modules/comment/lib/Drupal/comment/Controller/AdminController.php | |
@@ -7,16 +7,17 @@ | |
namespace Drupal\comment\Controller; | |
-use Drupal\Core\Controller\ControllerInterface; | |
+use Drupal\Core\DependencyInjection\ContainerInjectionInterface; | |
use Drupal\Core\Entity\EntityManager; | |
use Drupal\Core\Extension\ModuleHandler; | |
+use Drupal\comment\CommentManager; | |
use Drupal\field\FieldInfo; | |
use Symfony\Component\DependencyInjection\ContainerInterface; | |
/** | |
* Returns responses for comment module administrative routes. | |
*/ | |
-class AdminController implements ControllerInterface { | |
+class AdminController implements ContainerInjectionInterface { | |
/** | |
* The entity manager service. | |
@@ -40,13 +41,21 @@ class AdminController implements ControllerInterface { | |
protected $fieldInfo; | |
/** | |
+ * The comment manager service. | |
+ * | |
+ * @var \Drupal\comment\CommentManager | |
+ */ | |
+ protected $commentManager; | |
+ | |
+ /** | |
* {@inheritdoc} | |
*/ | |
public static function create(ContainerInterface $container) { | |
return new static( | |
$container->get('plugin.manager.entity'), | |
$container->get('module_handler'), | |
- $container->get('field.info') | |
+ $container->get('field.info'), | |
+ $container->get('comment.manager') | |
); | |
} | |
@@ -60,10 +69,11 @@ public static function create(ContainerInterface $container) { | |
* @param \Drupal\field\FieldInfo $field_info | |
* The field info service. | |
*/ | |
- public function __construct(EntityManager $entity_manager, ModuleHandler $module_handler, FieldInfo $field_info) { | |
+ public function __construct(EntityManager $entity_manager, ModuleHandler $module_handler, FieldInfo $field_info, CommentManager $comment_manager) { | |
$this->entityManager = $entity_manager; | |
$this->moduleHandler = $module_handler; | |
$this->fieldInfo = $field_info; | |
+ $this->commentManager = $comment_manager; | |
} | |
/** | |
@@ -75,8 +85,6 @@ public function __construct(EntityManager $entity_manager, ModuleHandler $module | |
* operation links for configuring each field. | |
*/ | |
public function overviewBundles() { | |
- // @todo Remove when http://drupal.org/node/1981644 is in. | |
- drupal_set_title(t('Comment forms')); | |
$header = array( | |
'field_name' => t('Field name'), | |
'usage' => array( | |
@@ -97,59 +105,53 @@ public function overviewBundles() { | |
$rows = array(); | |
// Fetch a list of all comment fields. | |
- $fields = array_filter($this->fieldInfo->getFieldMap(), function ($value) { | |
- if ($value['type'] == 'comment') { | |
- return TRUE; | |
- } | |
- }); | |
- | |
- foreach ($fields as $field_name => $field_info_map) { | |
- $field_info = $this->fieldInfo->getField($field_name); | |
- // Initialize the row. | |
- $rows[$field_name]['class'] = $field_info['locked'] ? array('field-disabled') : array(''); | |
- $rows[$field_name]['data']['field_name']['data'] = $field_info['locked'] ? t('@field_name (Locked)', array('@field_name' => $field_name)) : $field_name; | |
- | |
- $rows[$field_name]['data']['usage']['data'] = array( | |
- '#theme' => 'item_list', | |
- '#items' => array(), | |
- ); | |
- foreach ($field_info['bundles'] as $entity_type => $field_bundles) { | |
- $bundles = array(); | |
- foreach ($field_bundles as $bundle) { | |
+ $fields = $this->commentManager->getAllFields(); | |
+ | |
+ foreach ($fields as $entity_type => $data) { | |
+ foreach ($data as $field_name => $field_info_map) { | |
+ $field_info = $this->fieldInfo->getField($entity_type, $field_name); | |
+ // Initialize the row. | |
+ $row = array( | |
+ 'class' => $field_info->get('locked') ? array('field-disabled') : array(''), | |
+ ); | |
+ $row['data']['field_name']['data'] = $field_info->get('locked') ? t('@field_name (Locked)', array('@field_name' => $field_name)) : check_plain($field_name); | |
+ | |
+ $row['data']['usage']['data'] = array( | |
+ '#theme' => 'item_list', | |
+ '#title' => check_plain($entity_types[$entity_type]['label']), | |
+ '#items' => array(), | |
+ ); | |
+ foreach ($field_info_map['bundles'] as $bundle) { | |
if (isset($entity_bundles[$entity_type][$bundle])) { | |
// Add the current instance. | |
if ($field_ui_enabled && ($path = $this->entityManager->getAdminPath($entity_type, $bundle))) { | |
- $bundles[] = l($entity_bundles[$entity_type][$bundle]['label'], $path . '/fields'); | |
+ $row['data']['usage']['data']['#items'][] = l($entity_bundles[$entity_type][$bundle]['label'], $path . '/fields'); | |
} | |
else { | |
- $bundles[] = $entity_bundles[$entity_type][$bundle]['label']; | |
+ $row['data']['usage']['data']['#items'][] = $entity_bundles[$entity_type][$bundle]['label']; | |
} | |
} | |
} | |
- // Format used entity bundles. | |
- $rows[$field_name]['data']['usage']['data']['#items'][] = t('@entity_type: !bundles', array( | |
- '@entity_type' => $entity_types[$entity_type]['label'], | |
- '!bundles' => implode(', ', $bundles), | |
- )); | |
- } | |
- if ($field_ui_enabled) { | |
- // @todo Check proper permissions for operations. | |
- $links['fields'] = array( | |
- 'title' => t('Manage fields'), | |
- 'href' => 'admin/structure/comments/manage/' . $field_name . '/fields', | |
- 'weight' => 5, | |
- ); | |
- $links['display'] = array( | |
- 'title' => t('Manage display'), | |
- 'href' => 'admin/structure/comments/manage/' . $field_name . '/display', | |
- 'weight' => 10, | |
- ); | |
- | |
- $rows[$field_name]['data']['operations']['data'] = array( | |
- '#type' => 'operations', | |
- '#links' => $links, | |
- ); | |
+ if ($field_ui_enabled) { | |
+ // @todo Check proper permissions for operations. | |
+ $links['fields'] = array( | |
+ 'title' => t('Manage fields'), | |
+ 'href' => 'admin/structure/comments/manage/' . $entity_type . '_' . $field_name . '/fields', | |
+ 'weight' => 5, | |
+ ); | |
+ $links['display'] = array( | |
+ 'title' => t('Manage display'), | |
+ 'href' => 'admin/structure/comments/manage/' . $entity_type . '_' . $field_name . '/display', | |
+ 'weight' => 10, | |
+ ); | |
+ | |
+ $row['data']['operations']['data'] = array( | |
+ '#type' => 'operations', | |
+ '#links' => $links, | |
+ ); | |
+ } | |
+ $rows[$entity_type . '_' . $field_name] = $row; | |
} | |
} | |
@@ -159,6 +161,7 @@ public function overviewBundles() { | |
'#rows' => $rows, | |
'#empty' => t('No comment forms available.'), | |
); | |
+ $build['#title'] = t('Comment forms'); | |
return $build; | |
} | |
@@ -174,44 +177,37 @@ public function overviewBundles() { | |
* combinations on which the comment field is in use. | |
*/ | |
public function bundleInfo($field_name) { | |
- // @todo Decide on better UX http://drupal.org/node/1901110 | |
- $build['usage'] = array( | |
- '#theme' => 'item_list', | |
- '#items' => array(), | |
- ); | |
// @todo Remove when entity_get_bundles() is a method on the entity manager. | |
$entity_bundles = entity_get_bundles(); | |
$entity_types = $this->entityManager->getDefinitions(); | |
// Add a link to manage entity fields if the Field UI module is enabled. | |
$field_ui_enabled = $this->moduleHandler->moduleExists('field_ui'); | |
- $field_info = $this->fieldInfo->getField($field_name); | |
+ // @todo Provide dynamic routing to get entity type and field name. | |
+ list($entity_type, $field) = explode('_', $field_name); | |
+ $field_info = $this->fieldInfo->getField($entity_type, $field); | |
+ // @todo Decide on better UX http://drupal.org/node/1901110 | |
+ $build['usage'] = array( | |
+ '#theme' => 'item_list', | |
+ '#title' => check_plain($entity_types[$entity_type]['label']), | |
+ '#items' => array(), | |
+ ); | |
// Loop over all of the entity types to which this comment field is | |
// attached. | |
- foreach ($field_info['bundles'] as $entity_type => $field_bundles) { | |
- $bundles = array(); | |
- // Loop over all of the bundles of this entity type to which this comment | |
- // field is attached. | |
- foreach ($field_bundles as $bundle) { | |
- if (isset($entity_bundles[$entity_type][$bundle])) { | |
- // Add the current instance to the list of bundles. | |
- if ($field_ui_enabled && ($path = $this->entityManager->getAdminPath($entity_type, $bundle))) { | |
- // Add a link to configure the fields on the given bundle and entity | |
- // type combination. | |
- $bundles[] = l($entity_bundles[$entity_type][$bundle]['label'], $path . '/fields'); | |
- } | |
- else { | |
- // Field UI is disabled so fallback to a list of bundle labels | |
- // instead of links to configure fields. | |
- $bundles[] = $entity_bundles[$entity_type][$bundle]['label']; | |
- } | |
+ foreach ($field_info->getBundles() as $bundle) { | |
+ if (isset($entity_bundles[$entity_type][$bundle])) { | |
+ // Add the current instance to the list of bundles. | |
+ if ($field_ui_enabled && ($path = $this->entityManager->getAdminPath($entity_type, $bundle))) { | |
+ // Add a link to configure the fields on the given bundle and entity | |
+ // type combination. | |
+ $build['usage']['#items'][] = l($entity_bundles[$entity_type][$bundle]['label'], $path . '/fields'); | |
+ } | |
+ else { | |
+ // Field UI is disabled so fallback to a list of bundle labels | |
+ // instead of links to configure fields. | |
+ $build['usage']['#items'][] = check_plain($entity_bundles[$entity_type][$bundle]['label']); | |
} | |
} | |
- // Format used entity bundles for this comment field. | |
- $build['usage']['#items'][] = t('@entity_type: !bundles', array( | |
- '@entity_type' => $entity_types[$entity_type]['label'], | |
- '!bundles' => implode(', ', $bundles), | |
- )); | |
} | |
return $build; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment