Newer
Older
<?php
/**
* @file
* Contains \Drupal\comment\CommentManager.
*/
namespace Drupal\comment;
use Drupal\Component\Utility\String;
Angie Byron
committed
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\field\FieldInfo;
/**
* Comment manager contains common functions to manage comment fields.
*/
class CommentManager implements CommentManagerInterface {
/**
* The field info service.
*
* @var \Drupal\field\FieldInfo
*/
protected $fieldInfo;
/**
* The entity manager service.
*
Angie Byron
committed
* @var \Drupal\Core\Entity\EntityManagerInterface
*/
protected $entityManager;
/**
* Construct the CommentManager object.
*
* @param \Drupal\field\FieldInfo $field_info
* The field info service.
Angie Byron
committed
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager service.
*/
Angie Byron
committed
public function __construct(FieldInfo $field_info, EntityManagerInterface $entity_manager) {
$this->fieldInfo = $field_info;
$this->entityManager = $entity_manager;
}
/**
*/
public function getParentEntityUri(CommentInterface $comment) {
return $this->entityManager
->getStorageController($comment->entity_type->value)
->load($comment->entity_id->value)
->uri();
}
/**
Angie Byron
committed
public function getFields($entity_type) {
$info = $this->entityManager->getDefinition($entity_type);
Alex Pott
committed
if (!$info->isSubclassOf('\Drupal\Core\Entity\ContentEntityInterface')) {
return array();
}
Angie Byron
committed
$map = $this->getAllFields();
return isset($map[$entity_type]) ? $map[$entity_type] : array();
}
/**
*/
public function getAllFields() {
$map = $this->fieldInfo->getFieldMap();
// Build a list of comment fields only.
$comment_fields = array();
foreach ($map as $entity_type => $data) {
foreach ($data as $field_name => $field_info) {
if ($field_info['type'] == 'comment') {
$comment_fields[$entity_type][$field_name] = $field_info;
}
}
}
return $comment_fields;
}
/**
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
*/
public function addDefaultField($entity_type, $bundle, $field_name = 'comment', $default_value = COMMENT_OPEN) {
// Make sure the field doesn't already exist.
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(
'entity_type' => $entity_type,
'name' => $field_name,
'type' => 'comment',
'translatable' => '0',
));
// Create the field.
$field->save();
}
// Make sure the instance doesn't already exist.
if (!$this->fieldInfo->getInstance($entity_type, $bundle, $field_name)) {
$instance = $this->entityManager->getStorageController('field_instance')->create(array(
'label' => 'Comment settings',
'description' => '',
'field_name' => $field_name,
'entity_type' => $entity_type,
'bundle' => $bundle,
'required' => 1,
'default_value' => array(
array(
'status' => $default_value,
'cid' => 0,
'last_comment_name' => '',
'last_comment_timestamp' => 0,
'last_comment_uid' => 0,
),
),
));
$instance->save();
// Assign widget settings for the 'default' form mode.
entity_get_form_display($entity_type, $bundle, 'default')
->setComponent($field_name, array(
'type' => 'comment_default',
'weight' => 20,
))
->save();
// Set default to display comment list.
entity_get_display($entity_type, $bundle, 'default')
->setComponent($field_name, array(
'label' => 'hidden',
'type' => 'comment_default',
'weight' => 20,
))
->save();
}
$this->addBodyField($entity_type, $field_name);
}
/**
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
*/
public function addBodyField($entity_type, $field_name) {
// Create the field if needed.
$field = $this->entityManager->getStorageController('field_entity')->load('comment.comment_body');
if (!$field) {
$field = $this->entityManager->getStorageController('field_entity')->create(array(
'name' => 'comment_body',
'type' => 'text_long',
'entity_type' => 'comment',
));
$field->save();
}
// Create the instance if needed, field name defaults to 'comment'.
$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' => $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', $comment_bundle, 'default')
->setComponent('comment_body', array(
'type' => 'text_textarea',
))
->save();
// Assign display settings for the 'default' view mode.
entity_get_display('comment', $comment_bundle, 'default')
->setComponent('comment_body', array(
'label' => 'hidden',
'type' => 'text_default',
'weight' => 0,
))
->save();
}
}
/**
*/
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($commented_entity_type, $sample_bundle, $field_name);