You are here

public function ModulePreviewForm::buildForm in Opigno module 3.x

Same name and namespace in other branches
  1. 8 src/Form/ModulePreviewForm.php \Drupal\opigno_module\Form\ModulePreviewForm::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/ModulePreviewForm.php, line 29

Class

ModulePreviewForm
Add External package form.

Namespace

Drupal\opigno_module\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $route_params = \Drupal::routeMatch()
    ->getParameters();
  $opigno_module = $route_params
    ->get('opigno_module');
  $activities = array_values($opigno_module
    ->getModuleActivities());
  $count = count($activities);
  if ($activities) {

    // Add wrapper for ajax.
    $form['#prefix'] = '<div id="activity-wrapper">';
    $form['#suffix'] = '</div>';
    $activity = OpignoActivity::load($activities[$this->step - 1]->id);
    $form['activity'] = [
      '#type' => 'inline_template',
      '#template' => '<iframe style="{{ style }}" src="{{ url }}"></iframe>',
      '#context' => [
        'url' => '/admin/structure/opigno-activity/preview/' . $activity
          ->id(),
        'style' => "width: 100%; height: 100%; border: 0;",
      ],
    ];
  }
  $form['navigation'] = [
    '#prefix' => '<div class="activities-navigation">',
    '#suffix' => '</div>',
  ];
  if ($this->step != 1) {
    $form['navigation']['previous_button'] = array(
      '#type' => 'submit',
      '#name' => 'previous_button',
      '#value' => t('Back'),
      '#ajax' => [
        'callback' => '::ajaxFormRefreshCallback',
        'event' => 'click',
        'wrapper' => 'activity-wrapper',
      ],
    );
  }
  if ($count > 1) {
    $form['navigation']['step'] = [
      '#markup' => $this->step . '/' . $count,
      '#prefix' => '<div class="activities-count">',
      '#suffix' => '</div>',
    ];
  }
  if ($this->step < $count) {
    $form['navigation']['next_button'] = array(
      '#type' => 'submit',
      '#name' => 'next_button',
      '#value' => t('Next'),
      '#ajax' => [
        'callback' => '::ajaxFormRefreshCallback',
        'event' => 'click',
        'wrapper' => 'activity-wrapper',
      ],
    );
  }
  $form['#attached']['library'][] = 'opigno_module/opigno_module_preview';
  return $form;
}