3 namespace Drupal\node\Form;
5 use Drupal\Core\Config\ConfigFactoryInterface;
6 use Drupal\Core\Entity\EntityInterface;
7 use Drupal\Core\Entity\EntityManagerInterface;
8 use Drupal\Core\Form\FormBase;
9 use Drupal\Core\Form\FormStateInterface;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
14 * Contains a form for switching the view mode of a node during preview.
16 class NodePreviewForm extends FormBase {
19 * The entity manager service.
21 * @var \Drupal\Core\Entity\EntityManagerInterface
23 protected $entityManager;
28 * @var \Drupal\Core\Config\ConfigFactoryInterface
30 protected $configFactory;
35 public static function create(ContainerInterface $container) {
36 return new static($container->get('entity.manager'), $container->get('config.factory'));
40 * Constructs a new NodePreviewForm.
42 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
43 * The entity manager service.
44 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
45 * The configuration factory.
47 public function __construct(EntityManagerInterface $entity_manager, ConfigFactoryInterface $config_factory) {
48 $this->entityManager = $entity_manager;
49 $this->configFactory = $config_factory;
55 public function getFormId() {
56 return 'node_preview_form_select';
63 * An associative array containing the structure of the form.
64 * @param \Drupal\Core\Form\FormStateInterface $form_state
65 * The current state of the form.
66 * @param \Drupal\Core\Entity\EntityInterface $node
67 * The node being previews
72 public function buildForm(array $form, FormStateInterface $form_state, EntityInterface $node = NULL) {
73 $view_mode = $node->preview_view_mode;
75 $query_options = ['query' => ['uuid' => $node->uuid()]];
76 $query = $this->getRequest()->query;
77 if ($query->has('destination')) {
78 $query_options['query']['destination'] = $query->get('destination');
83 '#title' => $this->t('Back to content editing'),
84 '#url' => $node->isNew() ? Url::fromRoute('node.add', ['node_type' => $node->bundle()]) : $node->urlInfo('edit-form'),
85 '#options' => ['attributes' => ['class' => ['node-preview-backlink']]] + $query_options,
88 // Always show full as an option, even if the display is not enabled.
89 $view_mode_options = ['full' => $this->t('Full')] + $this->entityManager->getViewModeOptionsByBundle('node', $node->bundle());
91 // Unset view modes that are not used in the front end.
92 unset($view_mode_options['default']);
93 unset($view_mode_options['rss']);
94 unset($view_mode_options['search_index']);
98 '#value' => $node->uuid(),
101 $form['view_mode'] = [
103 '#title' => $this->t('View mode'),
104 '#options' => $view_mode_options,
105 '#default_value' => $view_mode,
107 'data-drupal-autosubmit' => TRUE,
113 '#value' => $this->t('Switch'),
115 'class' => ['js-hide'],
125 public function submitForm(array &$form, FormStateInterface $form_state) {
126 $route_parameters = [
127 'node_preview' => $form_state->getValue('uuid'),
128 'view_mode_id' => $form_state->getValue('view_mode'),
132 $query = $this->getRequest()->query;
133 if ($query->has('destination')) {
134 $options['query']['destination'] = $query->get('destination');
135 $query->remove('destination');
137 $form_state->setRedirect('entity.node.preview', $route_parameters, $options);