8b6125b87a8449bf62cb5c3f1f669b809ebdd84e
[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 class NodePreviewForm extends FormBase {
17
18   /**
19    * The entity manager service.
20    *
21    * @var \Drupal\Core\Entity\EntityManagerInterface
22    */
23   protected $entityManager;
24
25   /**
26    * The config factory.
27    *
28    * @var \Drupal\Core\Config\ConfigFactoryInterface
29    */
30   protected $configFactory;
31
32   /**
33    * {@inheritdoc}
34    */
35   public static function create(ContainerInterface $container) {
36     return new static($container->get('entity.manager'), $container->get('config.factory'));
37   }
38
39   /**
40    * Constructs a new NodePreviewForm.
41    *
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.
46    */
47   public function __construct(EntityManagerInterface $entity_manager, ConfigFactoryInterface $config_factory) {
48     $this->entityManager = $entity_manager;
49     $this->configFactory = $config_factory;
50   }
51
52   /**
53    * {@inheritdoc}
54    */
55   public function getFormId() {
56     return 'node_preview_form_select';
57   }
58
59   /**
60    * Form constructor.
61    *
62    * @param array $form
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
68    *
69    * @return array
70    *   The form structure.
71    */
72   public function buildForm(array $form, FormStateInterface $form_state, EntityInterface $node = NULL) {
73     $view_mode = $node->preview_view_mode;
74
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');
79     }
80
81     $form['backlink'] = [
82       '#type' => 'link',
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,
86     ];
87
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());
90
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']);
95
96     $form['uuid'] = [
97       '#type' => 'value',
98       '#value' => $node->uuid(),
99     ];
100
101     $form['view_mode'] = [
102       '#type' => 'select',
103       '#title' => $this->t('View mode'),
104       '#options' => $view_mode_options,
105       '#default_value' => $view_mode,
106       '#attributes' => [
107         'data-drupal-autosubmit' => TRUE,
108       ]
109     ];
110
111     $form['submit'] = [
112       '#type' => 'submit',
113       '#value' => $this->t('Switch'),
114       '#attributes' => [
115         'class' => ['js-hide'],
116       ],
117     ];
118
119     return $form;
120   }
121
122   /**
123    * {@inheritdoc}
124    */
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'),
129     ];
130
131     $options = [];
132     $query = $this->getRequest()->query;
133     if ($query->has('destination')) {
134       $options['query']['destination'] = $query->get('destination');
135       $query->remove('destination');
136     }
137     $form_state->setRedirect('entity.node.preview', $route_parameters, $options);
138   }
139
140 }