NodePreviewConverter.php 1.31 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
<?php

/**
 * @file
 * Contains \Drupal\node\ParamConverter\NodePreviewConverter.
 */

namespace Drupal\node\ParamConverter;

use Drupal\Core\Entity\EntityManagerInterface;
use Symfony\Component\Routing\Route;
use Drupal\Core\ParamConverter\ParamConverterInterface;
use Drupal\user\TempStoreFactory;

/**
 * Provides upcasting for a node entity in preview.
 */
class NodePreviewConverter implements ParamConverterInterface {

  /**
   * Stores the tempstore factory.
   *
   * @var \Drupal\user\TempStoreFactory
   */
  protected $tempStoreFactory;

  /**
   * Constructs a new NodePreviewConverter.
   *
   * @param \Drupal\user\TempStoreFactory $temp_store_factory
   *   The factory for the temp store object.
   */
  public function __construct(TempStoreFactory $temp_store_factory) {
    $this->tempStoreFactory = $temp_store_factory;
  }

  /**
   * {@inheritdoc}
   */
40
  public function convert($value, $definition, $name, array $defaults) {
41 42
    $store = $this->tempStoreFactory->get('node_preview');
    if ($form_state = $store->get($value)) {
43
      return $form_state->getFormObject()->getEntity();
44 45 46 47 48 49 50 51 52 53 54 55 56 57
    }
  }

  /**
   * {@inheritdoc}
   */
  public function applies($definition, $name, Route $route) {
    if (!empty($definition['type']) && $definition['type'] == 'node_preview') {
      return TRUE;
    }
    return FALSE;
  }

}