You are here

public function OpignoAnswerForm::buildForm in Opigno module 8

Same name and namespace in other branches
  1. 3.x src/Form/OpignoAnswerForm.php \Drupal\opigno_module\Form\OpignoAnswerForm::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 EntityForm::buildForm

File

src/Form/OpignoAnswerForm.php, line 27

Class

OpignoAnswerForm
Form controller for Answer edit forms.

Namespace

Drupal\opigno_module\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {

  /* @var $entity \Drupal\opigno_module\Entity\OpignoAnswer */
  $form = parent::buildForm($form, $form_state);

  // Hide revision_log_message field.
  unset($form['revision_log_message']);
  $entity = $this->entity;
  $activity = $entity
    ->getActivity();

  /** @var \Drupal\opigno_module\Entity\OpignoModule $module */
  $module = $entity
    ->getModule();
  $form['activity'] = [
    '#type' => 'label',
    '#title' => $activity->value,
  ];
  $form['module'] = [
    '#type' => 'label',
    '#title' => $module->value,
  ];

  // Backwards navigation.
  $form['actions']['back'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Back'),
    '#submit' => [
      '::backwardsNavigation',
    ],
  ];

  // Check for enabled option.
  // Also check that user already has at least 1 answered activity.
  // Check that user is not on the first activity in the module.
  $activity_link_type = OpignoGroupContext::getActivityLinkType();
  $attempt = $module
    ->getModuleActiveAttempt($this
    ->currentUser(), $activity_link_type);
  if ($attempt !== NULL) {
    $activities = $module
      ->getModuleActivities();
    $first_activity = reset($activities);
    $first_activity = $first_activity !== FALSE ? OpignoActivity::load($first_activity->id) : NULL;
    $current_activity = \Drupal::routeMatch()
      ->getParameter('opigno_activity');
    $has_first_activity = $first_activity !== NULL;
    $has_current_activity = $current_activity !== NULL;
    $is_on_first_activity = $has_first_activity && $has_current_activity && $first_activity
      ->id() === $current_activity
      ->id();

    // Disable back navigation for first content first activity.
    $cid = OpignoGroupContext::getCurrentGroupContentId();
    if ($cid) {
      $content = OpignoGroupManagedContent::load($cid);
      $parents = $content
        ->getParentsLinks();
      if (!$module
        ->getBackwardsNavigation() || empty($parents) && $is_on_first_activity) {
        $form['actions']['back']['#attributes']['disabled'] = TRUE;
      }
    }
  }
  else {
    $form['actions']['back']['#access'] = FALSE;
    $form['actions']['submit']['#access'] = FALSE;
  }

  /* @var $answer_service \Drupal\opigno_module\ActivityAnswerManager */
  $answer_service = \Drupal::service('plugin.manager.activity_answer');
  $answer_activity_type = $activity
    ->getType();
  if ($answer_service
    ->hasDefinition($answer_activity_type)) {
    $answer_instance = $answer_service
      ->createInstance($answer_activity_type);
    $answer_instance
      ->answeringForm($form);
  }

  // Remove 'delete' button.
  unset($form['actions']['delete']);
  return $form;
}