diff --git a/core/modules/edit/edit.module b/core/modules/edit/edit.module index a6ee046..4d0adfc 100644 --- a/core/modules/edit/edit.module +++ b/core/modules/edit/edit.module @@ -147,7 +147,9 @@ function edit_library_info() { function edit_preprocess_field(&$variables) { $element = $variables['element']; $entity = $element['#object']; - $variables['attributes']['data-edit-id'] = $entity->entityType() . '/' . $entity->id() . '/' . $element['#field_name'] . '/' . $element['#language'] . '/' . $element['#view_mode']; + if (empty($entity->in_preview)) { + $variables['attributes']['data-edit-id'] = $entity->entityType() . '/' . $entity->id() . '/' . $element['#field_name'] . '/' . $element['#language'] . '/' . $element['#view_mode']; + } } /** diff --git a/core/modules/filter/lib/Drupal/filter/Tests/FilterFormatAccessTest.php b/core/modules/filter/lib/Drupal/filter/Tests/FilterFormatAccessTest.php index 8667a5b..a210cfa 100644 --- a/core/modules/filter/lib/Drupal/filter/Tests/FilterFormatAccessTest.php +++ b/core/modules/filter/lib/Drupal/filter/Tests/FilterFormatAccessTest.php @@ -235,7 +235,7 @@ function testFormatWidgetPermissions() { $this->assertText($edit[$body_value_key], 'Old body found in preview.'); // Save and verify that only the title was changed. - $this->drupalPost(NULL, $new_edit, t('Save')); + $this->drupalPost('node/' . $node->nid . '/edit', $new_edit, t('Save')); $this->assertNoText($edit['title'], 'Old title not found.'); $this->assertText($new_edit['title'], 'New title found.'); $this->assertText($edit[$body_value_key], 'Old body found.'); diff --git a/core/modules/menu/menu.module b/core/modules/menu/menu.module index d13655e..0dd818c 100644 --- a/core/modules/menu/menu.module +++ b/core/modules/menu/menu.module @@ -544,6 +544,13 @@ function menu_form_node_form_alter(&$form, $form_state) { $node = $form_state['controller']->getEntity($form_state); $link = $node->menu; $type = $node->type; + // In case we're in preview and there is no mlid yet but a title + // has been supplied, trick the menu form to store its information. + if (!empty($node->in_preview) && empty($link['mlid']) && !empty($link['link_title'])) { + $link['mlid'] = TRUE; + // @todo move all other properties to attributes as well ? + $link['options']['attributes']['title'] = $link['description']; + } $options = menu_parent_options(menu_get_menus(), $link, $type); // If no possible parent menu items were found, there is nothing to display. if (empty($options)) { diff --git a/core/modules/node/images/btn-arrow-preview.png b/core/modules/node/images/btn-arrow-preview.png new file mode 100644 index 0000000..4ef43c0 --- /dev/null +++ b/core/modules/node/images/btn-arrow-preview.png @@ -0,0 +1,4 @@ +‰PNG + + IHDR + |9”0 fIDAT[cøÿÿ?2–nÎ@æ0qÃX€xH ,$xBš·ÃÀ‚jí`-(‚¿~ÿ Îî]{EhÇ6/Øuh\†½fn<~D9¬õ/ŠØjá˻ÐAX Æäw䆉\Ê IEND®B`‚ \ No newline at end of file diff --git a/core/modules/node/lib/Drupal/node/NodeFormController.php b/core/modules/node/lib/Drupal/node/NodeFormController.php index 63b4189..e161bec 100644 --- a/core/modules/node/lib/Drupal/node/NodeFormController.php +++ b/core/modules/node/lib/Drupal/node/NodeFormController.php @@ -26,6 +26,16 @@ class NodeFormController extends EntityFormController { * Overrides Drupal\Core\Entity\EntityFormController::prepareEntity(). */ protected function prepareEntity(EntityInterface $node) { + + // Check if we can retrieve a node from the tempstore. + $tempstore_id = drupal_container()->get('request')->query->get('tempstore_id'); + if ($tempstore_id) { + $node->tempstore_id = $tempstore_id; + } + else { + $node->tempstore_id = $node->uuid; + } + // Set up default values, if required. $node_options = variable_get('node_options_' . $node->type, array('status', 'promote')); // If this is a new node, fill in the default values. @@ -58,13 +68,20 @@ protected function prepareEntity(EntityInterface $node) { public function form(array $form, array &$form_state, EntityInterface $node) { $user_config = config('user.settings'); - // Some special stuff when previewing a node. - if (isset($form_state['node_preview'])) { - $form['#prefix'] = $form_state['node_preview']; - $node->in_preview = TRUE; - } - else { - unset($node->in_preview); + + // Check if we can retrieve a node from the tempstore. + $tempstore_id = drupal_container()->get('request')->query->get('tempstore_id'); + if (!empty($tempstore_id) && ($data = node_tempstore_load($tempstore_id))) { + $node = $data; + $form_state['entity'] = $node; + + // Restore the destination if we previewed the node. + if (isset($node->destination)) { + $form['node_destination'] = array( + '#type' => 'value', + '#value' => $node->destination, + ); + } } // Override the default CSS class name, since the user-defined node type @@ -386,12 +403,24 @@ public function submit(array $form, array &$form_state) { * A reference to a keyed array containing the current state of the form. */ public function preview(array $form, array &$form_state) { - // @todo Remove this: we should not have explicit includes in autoloaded - // classes. - module_load_include('inc', 'node', 'node.pages'); - drupal_set_title(t('Preview'), PASS_THROUGH); - $form_state['node_preview'] = node_preview($this->getEntity($form_state)); - $form_state['rebuild'] = TRUE; + $entity = $this->getEntity($form_state); + $entity->in_preview = TRUE; + + // In case the query destination parameter is set, store it on the entity + // and remove it so we aren't redirected. + $request = drupal_container()->get('request'); + $destination = $request->query->get('destination'); + if (!empty($destination)) { + $entity->destination = $destination; + unset($_GET['destination']); + } + // In case we already previewed the node, but we want to preview it again. + elseif (isset($form_state['values']['node_destination'])) { + $entity->destination = $form_state['values']['node_destination']; + } + + drupal_container()->get('user.tempstore')->get('node')->set($entity->tempstore_id, $entity); + $form_state['redirect'] = 'node/preview/' . $entity->tempstore_id; } /** @@ -454,6 +483,14 @@ public function save(array $form, array &$form_state) { $form_state['rebuild'] = TRUE; } + // In case we visited the node preview, override the redirect. + if (isset($form_state['values']['node_destination'])) { + $form_state['redirect'] = $form_state['values']['node_destination']; + } + + // Remove from tempstore. + drupal_container()->get('user.tempstore')->get('node')->delete($node->tempstore_id); + // Clear the page and block caches. cache_invalidate_tags(array('content' => TRUE)); } diff --git a/core/modules/node/lib/Drupal/node/Plugin/Core/Entity/Node.php b/core/modules/node/lib/Drupal/node/Plugin/Core/Entity/Node.php index c4d8109..cbbc37e 100644 --- a/core/modules/node/lib/Drupal/node/Plugin/Core/Entity/Node.php +++ b/core/modules/node/lib/Drupal/node/Plugin/Core/Entity/Node.php @@ -210,6 +210,13 @@ protected function init() { } /** + * The node tempstore id. + * + * @var integer + */ + public $tempstore_id; + + /** * Implements Drupal\Core\Entity\EntityInterface::id(). */ public function id() { diff --git a/core/modules/node/lib/Drupal/node/Tests/PagePreviewTest.php b/core/modules/node/lib/Drupal/node/Tests/PagePreviewTest.php index 28b04cd..c6906b8 100644 --- a/core/modules/node/lib/Drupal/node/Tests/PagePreviewTest.php +++ b/core/modules/node/lib/Drupal/node/Tests/PagePreviewTest.php @@ -109,13 +109,30 @@ function testPagePreview() { $this->drupalPost('node/add/page', $edit, t('Preview')); // Check that the preview is displaying the title, body and term. - $this->assertTitle(t('Preview | Drupal'), 'Basic page title is preview.'); + $this->assertTitle(t('@title | Drupal', array('@title' => $edit[$title_key])), 'Basic page title is preview.'); $this->assertText($edit[$title_key], 'Title displayed.'); $this->assertText($edit[$body_key], 'Body displayed.'); $this->assertText($edit[$term_key], 'Term displayed.'); - - // Check that the title, body and term fields are displayed with the - // correct values. + $this->assertLink(t('Back to content editing')); + + // Get the tempstore id. + $url = parse_url($this->getUrl()); + $paths = explode('/', $url['path']); + $tempstore_id = array_pop($paths); + + // Switch view mode. We'll remove the body from the body from the teaser + // view mode. + entity_get_display('node', 'page', 'teaser') + ->removeComponent('body') + ->save(); + $view_mode_edit = array('view_mode' => 'teaser'); + $this->drupalPost('node/preview/' . $tempstore_id, $view_mode_edit, t('Switch')); + $this->assertRaw('view-mode-teaser', 'View mode teaser class found.'); + $this->assertNoText($edit[$body_key], 'Body not displayed.'); + + // Check that the title and body fields are displayed with the correct + // values after going back to the content edit page. + $this->clickLink(t('Back to content editing')); $this->assertFieldByName($title_key, $edit[$title_key], 'Title field displayed.'); $this->assertFieldByName($body_key, $edit[$body_key], 'Body field displayed.'); $this->assertFieldByName($term_key, $edit[$term_key], 'Term field displayed.'); @@ -141,12 +158,14 @@ function testPagePreviewWithRevisions() { $this->drupalPost('node/add/page', $edit, t('Preview')); // Check that the preview is displaying the title, body and term. - $this->assertTitle(t('Preview | Drupal'), 'Basic page title is preview.'); + $this->assertTitle(t('@title | Drupal', array('@title' => $edit[$title_key])), 'Basic page title is preview.'); $this->assertText($edit[$title_key], 'Title displayed.'); $this->assertText($edit[$body_key], 'Body displayed.'); $this->assertText($edit[$term_key], 'Term displayed.'); - // Check that the title, body and term fields are displayed with the correct values. + // Check that the title and body fields are displayed with the correct + // values after going back to the content edit page. + $this->clickLink(t('Back to content editing')); $this->assertFieldByName($title_key, $edit[$title_key], 'Title field displayed.'); $this->assertFieldByName($body_key, $edit[$body_key], 'Body field displayed.'); $this->assertFieldByName($term_key, $edit[$term_key], 'Term field displayed.'); diff --git a/core/modules/node/node.module b/core/modules/node/node.module index fe6db9d..0d9653b 100644 --- a/core/modules/node/node.module +++ b/core/modules/node/node.module @@ -158,10 +158,6 @@ function node_theme() { 'variables' => array('content' => NULL), 'file' => 'node.pages.inc', ), - 'node_preview' => array( - 'variables' => array('node' => NULL), - 'file' => 'node.pages.inc', - ), 'node_admin_overview' => array( 'variables' => array('name' => NULL, 'type' => NULL), 'file' => 'content_types.inc', @@ -975,6 +971,23 @@ function node_load($nid = NULL, $reset = FALSE) { } /** + * Load a node from the tempstore. + * + * @param int $tempstore_id + * The ID of a tempstore object. + * + * @return \Drupal\node\Node|false + * A fully-populated node entity, or FALSE if the node is not found. + */ +function node_tempstore_load($tempstore_id) { + $node = drupal_container()->get('user.tempstore')->get('node')->get($tempstore_id); + if ($node) { + return $node; + } + return FALSE; +} + +/** * Loads a node revision from the database. * * @param int $nid @@ -1159,7 +1172,7 @@ function template_preprocess_node(&$variables) { $uri = $node->uri(); $variables['node_url'] = url($uri['path'], $uri['options']); $variables['label'] = check_plain($node->label()); - $variables['page'] = $variables['view_mode'] == 'full' && node_is_page($node); + $variables['page'] = ($variables['view_mode'] == 'full' && node_is_page($node)) || (!empty($node->node_page_title)); // Make useful flags and node data available. // @todo: The comment properties only exist if comment.module is enabled, but @@ -1746,6 +1759,13 @@ function node_menu() { 'title' => 'View', 'type' => MENU_DEFAULT_LOCAL_TASK, ); + $items['node/preview/%node_tempstore'] = array( + 'page callback' => 'node_preview', + 'page arguments' => array(2), + 'access callback' => 'node_access_preview', + 'access arguments' => array(2), + 'file' => 'node.pages.inc', + ); $items['node/%node/edit'] = array( 'title' => 'Edit', 'page callback' => 'node_page_edit', @@ -2207,6 +2227,103 @@ function node_page_view(EntityInterface $node) { } /** + * Implements hook_page_build(). + */ +function node_page_build(&$page) { + + // Add 'Back to content edit editing' link on preview page. + $node = menu_get_object('node_tempstore', 2); + if ($node) { + + $page['page_top']['node-preview'] = array( + '#type' => 'container', + '#attributes' => array( + 'class' => array('node-preview-container', 'container-inline') + ), + ); + + $form = drupal_get_form('node_preview_form_select', $node); + $page['page_top']['node-preview']['view-mode'] = $form; + } +} + +/** + * Get the preview form selection box. + * + * @param Drupal\node\Node $node + * A node entity. + * + * @return array $form + * The view mode preview selection form. + */ +function node_preview_form_select(array $form, array $form_state, Node $node) { + + // Always add the 'default' view mode. + $view_mode_options = array('default' => t('Full')); + + // Get view mode options. + $view_modes = entity_get_view_modes('node'); + foreach ($view_modes as $key => $info) { + if (!empty($info['custom_settings'])) { + $view_mode_options[$key] = $info['label']; + + // In case this is the 'full' view mode, rename default. + if ($key == 'full') { + $view_mode_options[$key] = t('Default'); + } + } + } + + $path = 'node/add/' . $node->bundle(); + if (!empty($node->nid)) { + $path = 'node/' . $node->nid . '/edit'; + } + + $view_mode = drupal_container()->get('request')->query->get('view_mode'); + if (empty($view_mode)) { + $view_mode = 'full'; + } + + $form['backlink'] = array( + '#type' => 'link', + '#title' => t('Back to content editing'), + '#href' => $path, + // Add the "overlay-restore" class to indicate this link should restore + // the context in which the preview demonstration page was opened. + '#options' => array('query' => array('tempstore_id' => $node->tempstore_id) ,'attributes' => array('class' => array('node-preview-backlink button-action', 'overlay-restore'))), + ); + + $form['tempstore_id'] = array( + '#type' => 'value', + '#value' => $node->tempstore_id + ); + + $form['view_mode'] = array( + '#type' => 'select', + '#title' => t('Select a view mode'), + '#title_display' => 'invisible', + '#options' => $view_mode_options, + '#default_value' => $view_mode, + ); + $form['submit'] = array( + '#type' => 'submit', + '#value' => t('Switch'), + '#attributes' => array( + 'class' => array('node-preview-switch-button') + ) + ); + + return $form; +} + +/** + * Submit handler for the node preview view mode selection form. + */ +function node_preview_form_select_submit(array $form, array &$form_state) { + $form_state['redirect'] = array('node/preview/' . $form_state['values']['tempstore_id'], array('query' => array('view_mode' => $form_state['values']['view_mode']))); +} + +/** * Implements hook_update_index(). */ function node_update_index() { @@ -2588,6 +2705,24 @@ function node_node_access($node, $op, $account) { } /** + * Access callback: Checks a user's permission for previewing a node. + * + * @param Drupal\node\Node $node + * The node entity to be previewed. + * + * @return bool + * TRUE if the user has access to the preview, FALSE otherwise. + * + * @see node_menu() + */ +function node_access_preview(Node $node) { + if (node_access('create', $node) || node_access('update', $node)) { + return TRUE; + } + return FALSE; +} + +/** * Helper function to generate standard node permission list for a given type. * * @param $name @@ -3596,6 +3731,9 @@ function node_library_info() { 'js' => array( drupal_get_path('module', 'node') . '/node.preview.js' => array(), ), + 'css' => array( + drupal_get_path('module', 'node') . '/node.preview.css' => array(), + ), 'dependencies' => array( array('system', 'jquery'), array('system', 'drupal'), diff --git a/core/modules/node/node.pages.inc b/core/modules/node/node.pages.inc index 863babd..d206d47 100644 --- a/core/modules/node/node.pages.inc +++ b/core/modules/node/node.pages.inc @@ -119,79 +119,49 @@ function node_add($node_type) { * @param \Drupal\Core\Entity\EntityInterface $node * The node to preview. * - * @return - * An HTML-formatted string of a node preview. - * - * @see node_form_build_preview() + * @return array + * A form array as expected by drupal_render(). */ function node_preview(EntityInterface $node) { - if (node_access('create', $node) || node_access('update', $node)) { - _field_invoke_multiple('load', 'node', array($node->nid => $node)); - // Load the user's name when needed. - if (isset($node->name)) { - // The use of isset() is mandatory in the context of user IDs, because - // user ID 0 denotes the anonymous user. - if ($user = user_load_by_name($node->name)) { - $node->uid = $user->uid; - } - else { - $node->uid = 0; // anonymous user - } - } - elseif ($node->uid) { - $user = user_load($node->uid); - $node->name = $user->name; + // Set status to true so we don't get the 'unpublished' CSS. + $node->status = TRUE; + + _field_invoke_multiple('load', 'node', array($node->nid => $node)); + // Load the author's name when needed. + if (isset($node->name)) { + // The use of isset() is mandatory in the context of user IDs, because + // user ID 0 denotes the anonymous user. + if ($user = user_load_by_name($node->name)) { + $node->uid = $user->uid; } - - $node->changed = REQUEST_TIME; - - // Display a preview of the node. - if (!form_get_errors()) { - $node->in_preview = TRUE; - $output = theme('node_preview', array('node' => $node)); - unset($node->in_preview); + else { + $node->uid = 0; } - - return $output; } -} - -/** - * Returns HTML for a node preview for display during node creation and editing. - * - * @param $variables - * An associative array containing: - * - node: The node entity which is being previewed. - * - * @see NodeFormController::preview() - * @see node_preview() - * - * @ingroup themeable - */ -function theme_node_preview($variables) { - $node = $variables['node']; + elseif ($node->uid) { + $user = user_load($node->uid); + $node->name = $user->name; + } - $output = ''; + $node->changed = REQUEST_TIME; - $elements = node_view($node, 'teaser'); - $elements['#attached']['library'][] = array('node', 'drupal.node.preview'); - $trimmed = drupal_render($elements); - $elements = node_view($node, 'full'); - $full = drupal_render($elements); - - // Do we need to preview trimmed version of post as well as full version? - if ($trimmed != $full) { - drupal_set_message(t('The trimmed version of your post shows what your post looks like when promoted to the main page or when exported for syndication. You can insert the delimiter "<!--break-->" (without the quotes) to fine-tune where your post gets split.')); - $output .= '