b06a65712fd42c06d67439e2d06e2aa990cbae07
[yaffs-website] / web / core / modules / node / src / Form / NodePreviewForm.php
1 <?php
2
3 namespace Drupal\node\Form;
4
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;
10 use Drupal\Core\Url;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
12
13 /**
14  * Contains a form for switching the view mode of a node during preview.
15  *
16  * @internal
17  */
18 class NodePreviewForm extends FormBase {
19
20   /**
21    * The entity manager service.
22    *
23    * @var \Drupal\Core\Entity\EntityManagerInterface
24    */
25   protected $entityManager;
26
27   /**
28    * The config factory.
29    *
30    * @var \Drupal\Core\Config\ConfigFactoryInterface
31    */
32   protected $configFactory;
33
34   /**
35    * {@inheritdoc}
36    */
37   public static function create(ContainerInterface $container) {
38     return new static($container->get('entity.manager'), $container->get('config.factory'));
39   }
40
41   /**
42    * Constructs a new NodePreviewForm.
43    *
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.
48    */
49   public function __construct(EntityManagerInterface $entity_manager, ConfigFactoryInterface $config_factory) {
50     $this->entityManager = $entity_manager;
51     $this->configFactory = $config_factory;
52   }
53
54   /**
55    * {@inheritdoc}
56    */
57   public function getFormId() {
58     return 'node_preview_form_select';
59   }
60
61   /**
62    * Form constructor.
63    *
64    * @param array $form
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
70    *
71    * @return array
72    *   The form structure.
73    */
74   public function buildForm(array $form, FormStateInterface $form_state, EntityInterface $node = NULL) {
75     $view_mode = $node->preview_view_mode;
76
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');
81     }
82
83     $form['backlink'] = [
84       '#type' => 'link',
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,
88     ];
89
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());
92
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']);
97
98     $form['uuid'] = [
99       '#type' => 'value',
100       '#value' => $node->uuid(),
101     ];
102
103     $form['view_mode'] = [
104       '#type' => 'select',
105       '#title' => $this->t('View mode'),
106       '#options' => $view_mode_options,
107       '#default_value' => $view_mode,
108       '#attributes' => [
109         'data-drupal-autosubmit' => TRUE,
110       ]
111     ];
112
113     $form['submit'] = [
114       '#type' => 'submit',
115       '#value' => $this->t('Switch'),
116       '#attributes' => [
117         'class' => ['js-hide'],
118       ],
119     ];
120
121     return $form;
122   }
123
124   /**
125    * {@inheritdoc}
126    */
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'),
131     ];
132
133     $options = [];
134     $query = $this->getRequest()->query;
135     if ($query->has('destination')) {
136       $options['query']['destination'] = $query->get('destination');
137       $query->remove('destination');
138     }
139     $form_state->setRedirect('entity.node.preview', $route_parameters, $options);
140   }
141
142 }