You are here

public function PollPostRenderCache::renderViewForm in Poll 8

Callback for #post_render_cache; replaces placeholder with poll view form.

Parameters

int $id: The poll ID.

string $view_mode: The view mode the poll should be rendered with.

string $langcode: The langcode in which the poll should be rendered.

Return value

array A renderable array containing the poll form.

File

src/PollPostRenderCache.php, line 50

Class

PollPostRenderCache
Defines a service for poll post render cache callbacks.

Namespace

Drupal\poll

Code

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);
    }

    /** @var \Drupal\poll\Form\PollViewForm $form_object */
    $form_object = \Drupal::service('class_resolver')
      ->getInstanceFromDefinition('Drupal\\poll\\Form\\PollViewForm');
    $form_object
      ->setPoll($poll);
    return \Drupal::formBuilder()
      ->getForm($form_object, \Drupal::request(), $view_mode);
  }
  else {
    return [
      '#markup' => '',
    ];
  }
}