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.
18 class NodePreviewForm extends FormBase {
21 * The entity manager service.
23 * @var \Drupal\Core\Entity\EntityManagerInterface
25 protected $entityManager;
30 * @var \Drupal\Core\Config\ConfigFactoryInterface
32 protected $configFactory;
37 public static function create(ContainerInterface $container) {
38 return new static($container->get('entity.manager'), $container->get('config.factory'));
42 * Constructs a new NodePreviewForm.
44 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
45 * The entity manager service.
46 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
47 * The configuration factory.
49 public function __construct(EntityManagerInterface $entity_manager, ConfigFactoryInterface $config_factory) {
50 $this->entityManager = $entity_manager;
51 $this->configFactory = $config_factory;
57 public function getFormId() {
58 return 'node_preview_form_select';
65 * An associative array containing the structure of the form.
66 * @param \Drupal\Core\Form\FormStateInterface $form_state
67 * The current state of the form.
68 * @param \Drupal\Core\Entity\EntityInterface $node
69 * The node being previews
74 public function buildForm(array $form, FormStateInterface $form_state, EntityInterface $node = NULL) {
75 $view_mode = $node->preview_view_mode;
77 $query_options = ['query' => ['uuid' => $node->uuid()]];
78 $query = $this->getRequest()->query;
79 if ($query->has('destination')) {
80 $query_options['query']['destination'] = $query->get('destination');
85 '#title' => $this->t('Back to content editing'),
86 '#url' => $node->isNew() ? Url::fromRoute('node.add', ['node_type' => $node->bundle()]) : $node->urlInfo('edit-form'),
87 '#options' => ['attributes' => ['class' => ['node-preview-backlink']]] + $query_options,
90 // Always show full as an option, even if the display is not enabled.
91 $view_mode_options = ['full' => $this->t('Full')] + $this->entityManager->getViewModeOptionsByBundle('node', $node->bundle());
93 // Unset view modes that are not used in the front end.
94 unset($view_mode_options['default']);
95 unset($view_mode_options['rss']);
96 unset($view_mode_options['search_index']);
100 '#value' => $node->uuid(),
103 $form['view_mode'] = [
105 '#title' => $this->t('View mode'),
106 '#options' => $view_mode_options,
107 '#default_value' => $view_mode,
109 'data-drupal-autosubmit' => TRUE,
115 '#value' => $this->t('Switch'),
117 'class' => ['js-hide'],
127 public function submitForm(array &$form, FormStateInterface $form_state) {
128 $route_parameters = [
129 'node_preview' => $form_state->getValue('uuid'),
130 'view_mode_id' => $form_state->getValue('view_mode'),
134 $query = $this->getRequest()->query;
135 if ($query->has('destination')) {
136 $options['query']['destination'] = $query->get('destination');
137 $query->remove('destination');
139 $form_state->setRedirect('entity.node.preview', $route_parameters, $options);