diff --git a/core/modules/node/node.module b/core/modules/node/node.module index 48d55b7..fa6af54 100644 --- a/core/modules/node/node.module +++ b/core/modules/node/node.module @@ -1127,8 +1127,8 @@ function node_view_multiple($nodes, $view_mode = 'teaser', $langcode = NULL) { */ function node_page_build(&$page) { // Add 'Back to content edit editing' link on preview page. - $node = \Drupal::request()->attributes->get('node_preview'); - if ($node) { + $attributes = \Drupal::request()->attributes; + if ($attributes->get(RouteObjectInterface::ROUTE_NAME) == 'node.preview') { $page['page_top']['node-preview'] = array( '#type' => 'container', '#attributes' => array( @@ -1136,63 +1136,12 @@ function node_page_build(&$page) { ), ); - $form = drupal_get_form('node_preview_form_select', $node); + $form = \Drupal::formBuilder()->getForm('\Drupal\node\Form\NodePreviewForm', $attributes->get('node_preview')); $page['page_top']['node-preview']['view-mode'] = $form; } } /** - * Get the preview form selection box. - */ -function node_preview_form_select(array $form, array $form_state, EntityInterface $node) { - - $view_mode = $node->preview_view_mode; - - $query_options = !$node->id() ? array('query' => array('uuid' => $node->uuid())) : array(); - $form['backlink'] = array( - '#type' => 'link', - '#title' => t('Back to content editing'), - '#href' => $node->id() ? 'node/' . $node->id() . '/edit' : 'node/add/' . $node->bundle(), - '#options' => array('attributes' => array('class' => array('node-preview-backlink button-action'))) + $query_options, - ); - - $view_mode_options = \Drupal::entityManager()->getViewModeOptions('node'); - foreach ($view_mode_options as $key => $value) { - $view_mode_options[$key] = t('@value view mode', array('@value' => $value)); - } - - $form['uuid'] = array( - '#type' => 'value', - '#value' => $node->uuid(), - ); - - $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']['uuid'] . '/' . $form_state['values']['view_mode']); -} - -/** * Implements hook_form_FORM_ID_alter(). * * Alters the System module's site information settings form to add a global diff --git a/core/modules/node/src/Form/NodePreviewForm.php b/core/modules/node/src/Form/NodePreviewForm.php new file mode 100644 index 0000000..13d14cf --- /dev/null +++ b/core/modules/node/src/Form/NodePreviewForm.php @@ -0,0 +1,115 @@ +get('entity.manager')); + } + + /** + * Constructs a new NodePreviewForm. + * + * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager + * The entity manager service. + */ + public function __construct(EntityManagerInterface $entity_manager) { + $this->entityManager = $entity_manager; + } + + /** + * {@inheritdoc} + */ + public function getFormId() { + return 'node_preview_form_select'; + } + + /** + * Form constructor. + * + * @param array $form + * An associative array containing the structure of the form. + * @param array $form_state + * An associative array containing the current state of the form. + * @param \Drupal\Core\Entity\EntityInterface $node + * The node being previews + * + * @return array + * The form structure. + */ + public function buildForm(array $form, array &$form_state, EntityInterface $node = NULL) { + $view_mode = $node->preview_view_mode; + + $query_options = !$node->id() ? array('query' => array('uuid' => $node->uuid())) : array(); + $form['backlink'] = array( + '#type' => 'link', + '#title' => t('Back to content editing'), + '#href' => $node->id() ? 'node/' . $node->id() . '/edit' : 'node/add/' . $node->bundle(), + '#options' => array('attributes' => array('class' => array('node-preview-backlink button-action'))) + $query_options, + ); + + $view_mode_options = $this->entityManager->getViewModeOptions('node'); + foreach ($view_mode_options as $key => $value) { + $view_mode_options[$key] = t('@value view mode', array('@value' => $value)); + } + + $form['uuid'] = array( + '#type' => 'value', + '#value' => $node->uuid(), + ); + + $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; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, array &$form_state) { + $form_state['redirect_route'] = new Url('node.preview', array( + 'node_preview' => $form_state['values']['uuid'], + 'view_mode_id' => $form_state['values']['view_mode'], + )); + } + +} diff --git a/core/modules/system/src/Tests/Database/RegressionTest.php b/core/modules/system/src/Tests/Database/RegressionTest.php index f38b49f..78eb3c7 100644 --- a/core/modules/system/src/Tests/Database/RegressionTest.php +++ b/core/modules/system/src/Tests/Database/RegressionTest.php @@ -17,7 +17,7 @@ class RegressionTest extends DatabaseTestBase { * * @var array */ - public static $modules = array('node'); + public static $modules = array('node', 'user'); public static function getInfo() { return array( diff --git a/core/modules/system/src/Tests/Entity/EntityTypedDataDefinitionTest.php b/core/modules/system/src/Tests/Entity/EntityTypedDataDefinitionTest.php index 30eb212..0ee06c7 100644 --- a/core/modules/system/src/Tests/Entity/EntityTypedDataDefinitionTest.php +++ b/core/modules/system/src/Tests/Entity/EntityTypedDataDefinitionTest.php @@ -34,7 +34,7 @@ class EntityTypedDataDefinitionTest extends DrupalUnitTestBase { * * @var array */ - public static $modules = array('filter', 'text', 'node'); + public static $modules = array('filter', 'text', 'node', 'user'); public static function getInfo() { return array(