b75d0626299e10c42eaad36206b2cb0a697a071d
[yaffs-website] / web / core / modules / node / src / ParamConverter / NodePreviewConverter.php
1 <?php
2
3 namespace Drupal\node\ParamConverter;
4
5 use Drupal\user\PrivateTempStoreFactory;
6 use Symfony\Component\Routing\Route;
7 use Drupal\Core\ParamConverter\ParamConverterInterface;
8
9 /**
10  * Provides upcasting for a node entity in preview.
11  */
12 class NodePreviewConverter implements ParamConverterInterface {
13
14   /**
15    * Stores the tempstore factory.
16    *
17    * @var \Drupal\user\PrivateTempStoreFactory
18    */
19   protected $tempStoreFactory;
20
21   /**
22    * Constructs a new NodePreviewConverter.
23    *
24    * @param \Drupal\user\PrivateTempStoreFactory $temp_store_factory
25    *   The factory for the temp store object.
26    */
27   public function __construct(PrivateTempStoreFactory $temp_store_factory) {
28     $this->tempStoreFactory = $temp_store_factory;
29   }
30
31   /**
32    * {@inheritdoc}
33    */
34   public function convert($value, $definition, $name, array $defaults) {
35     $store = $this->tempStoreFactory->get('node_preview');
36     if ($form_state = $store->get($value)) {
37       return $form_state->getFormObject()->getEntity();
38     }
39   }
40
41   /**
42    * {@inheritdoc}
43    */
44   public function applies($definition, $name, Route $route) {
45     if (!empty($definition['type']) && $definition['type'] == 'node_preview') {
46       return TRUE;
47     }
48     return FALSE;
49   }
50
51 }