diff --git a/core/modules/comment/src/CommentForm.php b/core/modules/comment/src/CommentForm.php index 19449cc..e8d71a5 100644 --- a/core/modules/comment/src/CommentForm.php +++ b/core/modules/comment/src/CommentForm.php @@ -217,6 +217,15 @@ public function form(array $form, FormStateInterface $form_state) { '#value' => ($comment->id() ? !$comment->getOwnerId() : $this->currentUser->isAnonymous()), ); + // Used for calculating the page number in the redirect URL. + if (!$view_mode = $form_state->get('entity_view_mode')) { + $view_mode = 'default'; + } + $form['entity_view_mode'] = array( + '#type' => 'hidden', + '#default_value' => $view_mode, + ); + return parent::form($form, $form_state, $comment); } @@ -381,9 +390,21 @@ public function save(array $form, FormStateInterface $form_state) { else { drupal_set_message($this->t('Your comment has been posted.')); } - // Find the current display page for this comment. - $display_settings = entity_get_display($entity->getEntityTypeId(), $entity->bundle(), 'default') + + // Find view mode in which the submitted form was rendered. + $form_view_mode = $form_state->getValue('entity_view_mode', 'default'); + $display_settings = entity_get_display($entity->getEntityTypeId(), $entity->bundle(), $form_view_mode) ->getComponent($field_name); + // Find view mode for locating the current display page for this comment. + $redirect = isset($display_settings['settings']['form_redirect']) ? $display_settings['settings']['form_redirect'] : 'entity_comment.default'; + if (strpos($redirect, 'entity_comment.') === 0) { + $redirect_view_mode = substr($redirect, 15); + if ($redirect_view_mode != $form_view_mode) { + $display_settings = entity_get_display($entity->getEntityTypeId(), $entity->bundle(), $redirect_view_mode) + ->getComponent($field_name); + } + } + // Redirect to the newly posted comment. $field_definition = $this->entityManager->getFieldStorageDefinitions($entity->getEntityTypeId())[$field_name]; $comment_type = $this->entityManager->getStorage('comment_type')->load($field_definition->getSetting('comment_type')); $page = $this->entityManager->getStorage('comment')->getDisplayOrdinal($comment, $comment_type->getThreadingMode(), $display_settings['settings']['per_page']); @@ -391,7 +412,6 @@ public function save(array $form, FormStateInterface $form_state) { if ($page > 0) { $query['page'] = $page; } - // Redirect to the newly posted comment. $uri->setOption('query', $query); $uri->setOption('fragment', 'comment-' . $comment->id()); } diff --git a/core/modules/comment/src/CommentPostRenderCache.php b/core/modules/comment/src/CommentPostRenderCache.php index 7bf100d..04115b9 100644 --- a/core/modules/comment/src/CommentPostRenderCache.php +++ b/core/modules/comment/src/CommentPostRenderCache.php @@ -69,7 +69,9 @@ public function renderForm(array $element, array $context) { 'pid' => NULL, ); $comment = $this->entityManager->getStorage('comment')->create($values); - $form = $this->entityFormBuilder->getForm($comment); + $form = $this->entityFormBuilder->getForm($comment, 'default', array( + 'entity_view_mode' => $context['entity_view_mode'], + )); // @todo: This only works as long as assets are still tracked in a global // static variable, see https://drupal.org/node/2238835 $markup = drupal_render($form); diff --git a/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php b/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php index e84fcd4..5ae89c0 100644 --- a/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php +++ b/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php @@ -11,6 +11,7 @@ use Drupal\comment\CommentManagerInterface; use Drupal\comment\CommentStorageInterface; use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface; +use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Entity\EntityViewBuilderInterface; use Drupal\Core\Entity\EntityFormBuilderInterface; use Drupal\Core\Extension\ModuleHandlerInterface; @@ -68,6 +69,7 @@ public static function defaultSettings() { 'show_links' => static::LINKS_NONE, 'per_page' => 50, 'form_location' => CommentItemInterface::FORM_BELOW, + 'form_redirect' => 'entity_comment.default', ) + parent::defaultSettings(); } @@ -86,6 +88,13 @@ public static function defaultSettings() { protected $currentUser; /** + * The entity manager. + * + * @var \Drupal\Core\Entity\EntityManagerInterface + */ + protected $entityManager; + + /** * The comment render controller. * * @var \Drupal\Core\Entity\EntityViewBuilderInterface @@ -138,7 +147,8 @@ public static function create(ContainerInterface $container, array $configuratio $container->get('entity.form_builder'), $container->get('module_handler'), $container->get('comment.manager'), - $container->get('comment.link_builder') + $container->get('comment.link_builder'), + $container->get('entity.manager') ); } @@ -173,9 +183,12 @@ public static function create(ContainerInterface $container, array $configuratio * The comment manager service. * @param \Drupal\comment\CommentLinkBuilderInterface $comment_link_builder * The link builder service. + * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager + * The entity manager. */ - public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, AccountInterface $current_user, CommentStorageInterface $comment_storage, EntityViewBuilderInterface $comment_view_builder, EntityFormBuilderInterface $entity_form_builder, ModuleHandlerInterface $module_handler, CommentManagerInterface $comment_manager, CommentLinkBuilderInterface $comment_link_builder) { + public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, AccountInterface $current_user, CommentStorageInterface $comment_storage, EntityViewBuilderInterface $comment_view_builder, EntityFormBuilderInterface $entity_form_builder, ModuleHandlerInterface $module_handler, CommentManagerInterface $comment_manager, CommentLinkBuilderInterface $comment_link_builder, EntityManagerInterface $entity_manager) { parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings); + $this->entityManager = $entity_manager; $this->viewBuilder = $comment_view_builder; $this->storage = $comment_storage; $this->currentUser = $current_user; @@ -248,6 +261,7 @@ public function viewElements(FieldItemListInterface $items) { 'entity_type' => $entity->getEntityTypeId(), 'entity_id' => $entity->id(), 'field_name' => $field_name, + 'entity_view_mode' => $this->viewMode, ); $placeholder = drupal_render_cache_generate_placeholder($callback, $context); $output['comment_form'] = array( @@ -309,6 +323,25 @@ public function settingsForm(array $form, FormStateInterface $form_state) { '#title' => t('Show reply form on the same page as comments'), '#default_value' => $this->getSetting('form_location'), ); + // Compile 'redirect' options; leave room for future extension by prefixing + // with 'entity:' + $options = array(); + $entity_type = $this->fieldDefinition->getFieldStorageDefinition()->getTargetEntityTypeId(); + foreach ($this->entityManager->getViewModeOptions($entity_type) as $key => $label) { + $options['entity_comment.' . $key] = $this->t('Location of comment, rendered in @view_mode mode', array('@view_mode' => $label)); + } + $element['form_redirect'] = array( + '#type' => 'select', + '#title' => t('After submitting a new reply, redirect to'), + '#options' => $options, + '#default_value' => $this->getSetting('form_redirect'), + // This does not (yet?) work when the form is on a separate page. + '#states' => array( + 'invisible' => array( + ':input[name="fields[comment][settings_edit_form][settings][form_location]"]' => array('checked' => FALSE), + ), + ), + ); $element['pager_id'] = array( '#type' => 'select', '#title' => $this->t('Pager ID'), @@ -329,18 +362,34 @@ public function settingsSummary() { static::LINKS_PAGE => $this->t('with full-page style links'), static::LINKS_TEASER => $this->t('with teaser style links'), ); + + $redirect = $this->getSetting('form_redirect'); + if (strpos($redirect, 'entity_comment.') === 0) { + $redirect = substr($redirect, 15); + // Try to switch machine name for label. + $entity_type = $this->fieldDefinition->getFieldStorageDefinition()->getTargetEntityTypeId(); + $options = $this->entityManager->getViewModeOptions($entity_type); + if (isset($options[$redirect])) { + $redirect = $options[$redirect]; + } + } + else { + $redirect = 'default location'; + } + $variables = array( '@id' => $this->getSetting('pager_id'), '@form' => $this->getSetting('form_location') ? $this->t('inline form') : $this->t('form on separate page'), + '@redirect' => $redirect, '@per_page' => $this->getSetting('per_page'), '@links' => $links_map[$this->getSetting('show_links')], ); if ($this->getSetting('pager_id')) { // Only include pager details in summary if we're using a non-standard // pager id. - return array($this->t('Showing @per_page comments with @form, using pager ID @id and @links', $variables)); + return array($this->t('Showing @per_page comments with @form (redirecting to @redirect), using pager ID @id and @links', $variables)); } - return array($this->t('Showing @per_page comments with @form and @links', $variables)); + return array($this->t('Showing @per_page comments with @form (redirecting to @redirect) and @links', $variables)); } }