/** * #post_render_cache callback; replaces placeholder with comment form. * * @param array $element * The renderable array that contains the to be replaced placeholder. * @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 * - comment_type: the comment type * * @return array * A renderable array containing the comment form. */ public function renderForm(array $element, array $context) { $values = array('entity_type' => $context['entity_type'], 'entity_id' => $context['entity_id'], 'field_name' => $context['field_name'], 'comment_type' => $context['comment_type'], 'pid' => NULL); $comment = $this->entityManager->getStorage('comment')->create($values); $form = $this->entityFormBuilder->getForm($comment); $markup = $this->renderer->render($form); $callback = 'comment.post_render_cache:renderForm'; $placeholder = $this->generatePlaceholder($callback, $context); $element['#markup'] = str_replace($placeholder, $markup, $element['#markup']); $element = $this->renderer->mergeBubbleableMetadata($element, $form); return $element; }
/** * #post_render_cache callback; replaces placeholder with comment form. * * @param array $element * The renderable array that contains the to be replaced placeholder. * @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(array $element, array $context) { $field_name = $context['field_name']; $entity = $this->entityManager->getStorage($context['entity_type'])->load($context['entity_id']); $field_storage = FieldStorageConfig::loadByName($entity->getEntityTypeId(), $field_name); $values = array('entity_type' => $entity->getEntityTypeId(), 'entity_id' => $entity->id(), 'field_name' => $field_name, 'comment_type' => $field_storage->getSetting('bundle'), 'pid' => NULL); $comment = $this->entityManager->getStorage('comment')->create($values); $form = $this->entityFormBuilder->getForm($comment); $markup = $this->renderer->render($form); $callback = 'comment.post_render_cache:renderForm'; $placeholder = $this->generatePlaceholder($callback, $context); $element['#markup'] = str_replace($placeholder, $markup, $element['#markup']); $element = Renderer::mergeBubbleableMetadata($element, $form); return $element; }
/** * Invokes the page attachment hooks. * * @param array &$page * A #type 'page' render array, for which the page attachment hooks will be * invoked and to which the results will be added. * * @throws \LogicException * * @internal * * @see hook_page_attachments() * @see hook_page_attachments_alter() */ public function invokePageAttachmentHooks(array &$page) { // Modules can add attachments. $attachments = []; foreach ($this->moduleHandler->getImplementations('page_attachments') as $module) { $function = $module . '_page_attachments'; $function($attachments); } if (array_diff(array_keys($attachments), ['#attached', '#post_render_cache']) !== []) { throw new \LogicException('Only #attached and #post_render_cache may be set in hook_page_attachments().'); } // Modules and themes can alter page attachments. $this->moduleHandler->alter('page_attachments', $attachments); \Drupal::theme()->alter('page_attachments', $attachments); if (array_diff(array_keys($attachments), ['#attached', '#post_render_cache']) !== []) { throw new \LogicException('Only #attached and #post_render_cache may be set in hook_page_attachments_alter().'); } // Merge the attachments onto the $page render array. $page = Renderer::mergeBubbleableMetadata($page, $attachments); }