diff --git a/group.module b/group.module index eec510a..a30f37b 100644 --- a/group.module +++ b/group.module @@ -633,12 +633,6 @@ function group_form_alter(&$form, FormStateInterface $form_state, $form_id) { $actions = $form['actions'] ?? []; foreach (Element::children($actions) as $name) { - // Remove preview button as it redirects back to the wrong form. - if ($name == 'preview') { - unset($form['actions'][$name]); - continue; - } - // Skip buttons without submit handlers. if (empty($form['actions'][$name]['#submit'])) { continue; diff --git a/modules/gnode/gnode.module b/modules/gnode/gnode.module index 4972219..7b1681e 100644 --- a/modules/gnode/gnode.module +++ b/modules/gnode/gnode.module @@ -6,6 +6,7 @@ */ use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Url; use Drupal\group\Entity\GroupInterface; use Drupal\node\NodeTypeInterface; @@ -41,3 +42,25 @@ function gnode_entity_operation(EntityInterface $entity) { return $operations; } + +/** + * Implements hook_form_BASE_FORM_ID_alter(). + * + * Alter the URL for the back link for \Drupal\node\Form\NodePreviewForm form. + */ +function gnode_form_node_preview_form_select_alter(&$form, FormStateInterface $form_state) { + /** @var \Drupal\node\NodeInterface $node_preview */ + $node_preview = $form_state->getBuildInfo()['args'][0] ?? NULL; + if ($node_preview && $node_preview->isNew()) { + $store = \Drupal::service('tempstore.private')->get('node_preview'); + if (($node_form_state = $store->get($node_preview->uuid())) + && ($group = $node_form_state->get('group')) + && $group instanceof GroupInterface + && ($plugin_id = $node_form_state->get('group_relation'))) { + $form['backlink']['#url'] = Url::fromRoute('entity.group_content.create_form', [ + 'group' => $group->id(), + 'plugin_id' => $plugin_id, + ]); + } + } +} diff --git a/modules/gnode/gnode.services.yml b/modules/gnode/gnode.services.yml index 9632843..6b286d6 100644 --- a/modules/gnode/gnode.services.yml +++ b/modules/gnode/gnode.services.yml @@ -9,3 +9,11 @@ services: class: 'Drupal\gnode\Plugin\Group\RelationHandler\GroupNodePermissionProvider' arguments: ['@group.relation_handler.permission_provider'] shared: false + + # Decorating the node preview access check service. + gnode.access_check.node.preview.decorator: + class: Drupal\gnode\Access\NodePreviewAccessCheck + decorates: access_check.node.preview + decoration_priority: 10 + public: false + arguments: ['@gnode.access_check.node.preview.decorator.inner', '@entity_type.manager', '@group_relation_type.manager', '@tempstore.private'] diff --git a/modules/gnode/src/Access/NodePreviewAccessCheck.php b/modules/gnode/src/Access/NodePreviewAccessCheck.php new file mode 100644 index 0000000..17dfb31 --- /dev/null +++ b/modules/gnode/src/Access/NodePreviewAccessCheck.php @@ -0,0 +1,92 @@ +innerService = $inner_service; + $this->groupRelationTypeManager = $group_relation_type_manager; + $this->tempStoreFactory = $temp_store_factory; + parent::__construct($entity_type_manager); +} + + /** + * Checks access to the grouped node preview page. + * + * @param \Drupal\Core\Session\AccountInterface $account + * The currently logged in account. + * @param \Drupal\node\NodeInterface $node_preview + * The node that is being previewed. + * + * @return \Drupal\Core\Access\AccessResultInterface + * The access result. + */ + public function access(AccountInterface $account, NodeInterface $node_preview) { + if ($node_preview->isNew()) { + $store = $this->tempStoreFactory->get('node_preview'); + if (($form_state = $store->get($node_preview->uuid())) + && ($group = $form_state->get('group')) + && $group instanceof GroupInterface + && ($plugin_id = $form_state->get('group_relation'))) { + return $this->groupRelationTypeManager->getAccessControlHandler($plugin_id) + ->entityCreateAccess($group, $account, TRUE); + } + } + + return $this->innerService->access($account, $node_preview); + } + +}