You are here

ReferencePreviewConverter.php in Bibliography & Citation 2.0.x

File

modules/bibcite_entity/src/ParamConverter/ReferencePreviewConverter.php
View source
<?php

namespace Drupal\bibcite_entity\ParamConverter;

use Drupal\Core\TempStore\PrivateTempStoreFactory;
use Symfony\Component\Routing\Route;
use Drupal\Core\ParamConverter\ParamConverterInterface;

/**
 * Provides upcasting for a reference entity in preview.
 */
class ReferencePreviewConverter implements ParamConverterInterface {

  /**
   * Stores the tempstore factory.
   *
   * @var \Drupal\Core\TempStore\PrivateTempStoreFactory
   */
  protected $tempStoreFactory;

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

  /**
   * {@inheritdoc}
   */
  public function convert($value, $definition, $name, array $defaults) {
    $store = $this->tempStoreFactory
      ->get('bibcite_reference_preview');
    if ($form_state = $store
      ->get($value)) {
      return $form_state
        ->getFormObject()
        ->getEntity();
    }
  }

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

}

Classes

Namesort descending Description
ReferencePreviewConverter Provides upcasting for a reference entity in preview.