You are here

public function WebformHelpVideoForm::buildForm in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Form/WebformHelpVideoForm.php \Drupal\webform\Form\WebformHelpVideoForm::buildForm()

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

src/Form/WebformHelpVideoForm.php, line 51

Class

WebformHelpVideoForm
Help video form.

Namespace

Drupal\webform\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $id = NULL) {
  $this->videoId = str_replace('-', '_', $id);
  $video = $this->helpManager
    ->getVideo($this->videoId);
  if (!$video) {
    throw new NotFoundHttpException();
  }
  $form['#title'] = $video['title'];

  // Content.
  if (is_array($video['content'])) {
    $form['content'] = $video['content'];
  }
  else {
    $form['content'] = [
      '#markup' => $video['content'],
    ];
  }

  // Video.
  if ($video['youtube_id']) {
    $form['video'] = [
      '#theme' => 'webform_help_video_youtube',
      '#youtube_id' => $video['youtube_id'],
    ];
  }

  // Related resources.
  if ($video_links = $this->helpManager
    ->getVideoLinks($this->videoId)) {
    $form['resources'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Additional resources'),
      'links' => [
        '#theme' => 'links',
        '#links' => $video_links,
      ],
    ];
  }

  // Actions.
  if ($this
    ->isDialog()) {
    $form['modal_actions'] = [
      '#type' => 'actions',
    ];
    $form['modal_actions']['close'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Close'),
      '#ajax' => [
        'callback' => '::closeDialog',
        'event' => 'click',
      ],
      '#attributes' => [
        'class' => [
          'button',
          'button--primary',
        ],
      ],
    ];
    if ($this
      ->getRequest()->query
      ->get('more') && $this
      ->currentUser()
      ->hasPermission('access webform help')) {
      $form['modal_actions']['more'] = [
        '#type' => 'link',
        '#title' => $this
          ->t('▶ Watch more videos'),
        '#url' => Url::fromRoute('webform.help'),
        '#attributes' => [
          'class' => [
            'button',
          ],
        ],
      ];
    }
  }
  $form['#attached']['library'][] = 'webform/webform.help';
  return $form;
}