You are here

class AdvPollPostRenderCache in Advanced Poll 8

Basically a copy of the original PollPostRenderCache class with factory-style logic for the renderViewForm to select which poll type to display.

Hierarchy

Expanded class hierarchy of AdvPollPostRenderCache

1 string reference to 'AdvPollPostRenderCache'
advpoll.services.yml in ./advpoll.services.yml
advpoll.services.yml
1 service uses AdvPollPostRenderCache
advpoll.post_render_cache in ./advpoll.services.yml
Drupal\advpoll\AdvPollPostRenderCache

File

src/AdvPollPostRenderCache.php, line 12

Namespace

Drupal\advpoll
View source
class AdvPollPostRenderCache implements TrustedCallbackInterface {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Constructs a new PollPostRenderCache object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity manager service.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * @inheritDoc
   */
  public static function trustedCallbacks() {
    return [
      'renderViewForm',
    ];
  }

  /**
   * Callback for #post_render_cache; replaces placeholder with poll view form.
   *
   * @param int $id
   *   The poll ID.
   * @param string $view_mode
   *   The view mode the poll should be rendered with.
   * @param string $langcode
   *   The langcode in which the poll should be rendered.
   *
   * @return array
   *   A renderable array containing the poll form.
   */
  public function renderViewForm($id, $view_mode, $langcode = NULL) {

    /** @var \Drupal\poll\PollInterface $poll */
    $poll = $this->entityTypeManager
      ->getStorage('poll')
      ->load($id);
    if ($poll) {
      if ($langcode && $poll
        ->hasTranslation($langcode)) {
        $poll = $poll
          ->getTranslation($langcode);
      }
      $form_object = \Drupal::service('class_resolver')
        ->getInstanceFromDefinition('Drupal\\advpoll\\Form\\ApprovalPollViewForm');
      $form_object
        ->setPoll($poll);
      return \Drupal::formBuilder()
        ->getForm($form_object, \Drupal::request(), $view_mode);
    }
    else {
      return [
        '#markup' => '',
      ];
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AdvPollPostRenderCache::$entityTypeManager protected property The entity type manager.
AdvPollPostRenderCache::renderViewForm public function Callback for #post_render_cache; replaces placeholder with poll view form.
AdvPollPostRenderCache::trustedCallbacks public static function @inheritDoc Overrides TrustedCallbackInterface::trustedCallbacks
AdvPollPostRenderCache::__construct public function Constructs a new PollPostRenderCache object.
TrustedCallbackInterface::THROW_EXCEPTION constant Untrusted callbacks throw exceptions.
TrustedCallbackInterface::TRIGGER_SILENCED_DEPRECATION constant Untrusted callbacks trigger silenced E_USER_DEPRECATION errors.
TrustedCallbackInterface::TRIGGER_WARNING constant Untrusted callbacks trigger E_USER_WARNING errors.