diff --git a/core/includes/common.inc b/core/includes/common.inc index ace37c8..4ced045 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -3909,8 +3909,8 @@ function drupal_render_cache_generate_placeholder($callback, array $context, $to */ function drupal_pre_render_render_cache_placeholder($element) { $callback = $element['#callback']; - if (!is_callable($callback)) { - throw new Exception(t('#callback must be a callable function.')); + if (!is_callable($callback) && strpos($callback, ':') === FALSE) { + throw new Exception(t('#callback must be a callable function or of the form service_id:method.')); } $context = $element['#context']; if (!is_array($context)) { @@ -3949,18 +3949,26 @@ function _drupal_render_process_post_render_cache(array &$elements) { // is passed to the callback as well. This token is used to uniquely // identify the placeholder in the markup. $modified_elements = $elements; + /** @var \Drupal\Core\Controller\ControllerResolverInterface $controller_resolver */ + $controller_resolver = \Drupal::service('controller_resolver'); foreach ($elements['#post_render_cache'] as $callback => $options) { + if (strpos($callback, '::') == FALSE) { + $callable = $controller_resolver->getControllerFromDefinition($callback); + } + else { + $callable = $callback; + } foreach ($elements['#post_render_cache'][$callback] as $token => $context) { // The advanced option, when setting #post_render_cache directly. if (is_numeric($token)) { - $modified_elements = call_user_func_array($callback, array($modified_elements, $context)); + $modified_elements = call_user_func_array($callable, array($modified_elements, $context)); } // The simple option, when using the standard placeholders, and hence // also when using #type => render_cache_placeholder. else { // Call #post_render_cache callback to generate the element that will // fill in the placeholder. - $generated_element = call_user_func_array($callback, array($context)); + $generated_element = call_user_func_array($callable, array($context)); // Update #attached based on the generated element. if (isset($generated_element['#attached'])) { diff --git a/core/modules/comment/comment.services.yml b/core/modules/comment/comment.services.yml index 76fdbc4..187d22c 100644 --- a/core/modules/comment/comment.services.yml +++ b/core/modules/comment/comment.services.yml @@ -18,3 +18,7 @@ services: arguments: ['@entity.manager'] tags: - { name: route_enhancer} + + comment.post_render_cache: + class: Drupal\comment\CommentPostRenderCache + arguments: ['@entity.manager', '@entity.form_builder'] diff --git a/core/modules/comment/lib/Drupal/comment/CommentPostRenderCache.php b/core/modules/comment/lib/Drupal/comment/CommentPostRenderCache.php new file mode 100644 index 0000000..4541d79 --- /dev/null +++ b/core/modules/comment/lib/Drupal/comment/CommentPostRenderCache.php @@ -0,0 +1,68 @@ +entityManager = $entity_manager; + $this->entityFormBuilder = $entity_form_builder; + } + /** + * #post_render_cache callback; replaces placeholder with comment form. + * + * @param array $context + * An array with the following keys: + * - entity_type: an entity type + * - entity_id: an entity ID + * - field_name: a comment field name + * + * @return array + * A renderable array containing the comment form. + */ + public function renderForm($context) { + $entity = $this->entityManager->getStorage($context['entity_type'])->load($context['entity_id']); + $values = array( + 'entity_type' => $entity->getEntityTypeId(), + 'entity_id' => $entity->id(), + 'field_id' => $entity->getEntityTypeId() . '__' . $context['field_name'], + 'pid' => NULL, + ); + $comment = $this->entityManager->getStorage('comment')->create($values); + return $this->entityFormBuilder->getForm($comment); + } + +} diff --git a/core/modules/comment/lib/Drupal/comment/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php b/core/modules/comment/lib/Drupal/comment/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php index 639cbb0..895e5f3 100644 --- a/core/modules/comment/lib/Drupal/comment/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php +++ b/core/modules/comment/lib/Drupal/comment/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php @@ -164,7 +164,7 @@ public function viewElements(FieldItemListInterface $items) { else { $output['comment_form'] = array( '#type' => 'render_cache_placeholder', - '#callback' => '\Drupal\comment\Plugin\Field\FieldFormatter\CommentDefaultFormatter::renderForm', + '#callback' => 'comment.post_render_cache:renderForm', '#context' => array( 'entity_type' => $entity->getEntityTypeId(), 'entity_id' => $entity->id(), @@ -188,23 +188,6 @@ public function viewElements(FieldItemListInterface $items) { } /** - * #post_render_cache callback; replaces placeholder with comment form. - * - * @param array $context - * An array with the following keys: - * - entity_type: an entity type - * - entity_id: an entity ID - * - field_name: a comment field name - * - * @return array - * A renderable array containing the comment form. - */ - public static function renderForm(array $context) { - $entity = entity_load($context['entity_type'], $context['entity_id']); - return comment_add($entity, $context['field_name']); - } - - /** * {@inheritdoc} */ public function settingsForm(array $form, array &$form_state) {