You are here

private function ModuleActivitiesForm::activitiesToForm in Opigno module 8

Same name and namespace in other branches
  1. 3.x src/Form/ModuleActivitiesForm.php \Drupal\opigno_module\Form\ModuleActivitiesForm::activitiesToForm()

Build Module activities list table.

1 call to ModuleActivitiesForm::activitiesToForm()
ModuleActivitiesForm::buildForm in src/Form/ModuleActivitiesForm.php
Form constructor.

File

src/Form/ModuleActivitiesForm.php, line 90

Class

ModuleActivitiesForm
Class ModuleActivitiesForm.

Namespace

Drupal\opigno_module\Form

Code

private function activitiesToForm(array &$form, array $activities, OpignoModuleInterface $opigno_module, array $activity_types) {
  $current_user = \Drupal::currentUser();

  // Build activities table headers.
  $headers = [
    $this
      ->t('Question'),
    $this
      ->t('Type'),
    $this
      ->t('Actions'),
    $this
      ->t('Max score'),
    [
      'data' => $this
        ->t('Delete'),
      'class' => [
        'checkbox',
      ],
    ],
    $this
      ->t('Weight'),
  ];
  $form['activities_list']['#header'] = $headers;
  $activities_list =& $form['activities_list'];
  foreach ($activities as $activity) {
    $activity_id = $activity->id . '-' . $activity->vid;
    $activities_list[$activity_id]['#attributes']['class'][] = 'draggable';
    $activities_list[$activity_id]['#weight'] = $activity->weight;

    /* @todo Check for view activity outside module permission */

    // Table title field.
    $activities_list[$activity_id]['title'] = Link::createFromRoute($activity->name, 'entity.opigno_activity.canonical', [
      'opigno_activity' => $activity->id,
    ])
      ->toRenderable();

    // Output Activity type.
    $activities_list[$activity_id]['type'] = [
      '#plain_text' => !empty($activity_types[$activity->type]) ? $activity_types[$activity->type]
        ->label() : '',
    ];

    // Check for permission and output activity edit link.
    if ($current_user
      ->hasPermission('edit activity entities')) {
      $activities_list[$activity_id]['actions'] = Link::createFromRoute($this
        ->t('Edit'), 'entity.opigno_activity.edit_form', [
        'opigno_activity' => $activity->id,
      ], [
        'query' => [
          'destination' => Url::createFromRequest(\Drupal::request())
            ->toString(),
        ],
      ])
        ->toRenderable();
    }
    else {
      $activities_list[$activity_id]['actions'] = [
        '#plain_text' => '',
      ];
    }
    $activities_list[$activity_id]['max_score'] = [
      '#type' => 'textfield',
      '#default_value' => is_numeric($activity->max_score) ? $activity->max_score : 0,
      '#element_validate' => [
        [
          $this,
          'maxScoreValidate',
        ],
      ],
    ];
    $activities_list[$activity_id]['delete'] = [
      '#type' => 'checkbox',
      '#default_value' => 0,
      '#attributes' => [
        'class' => [
          'checkbox',
        ],
      ],
      '#wrapper_attributes' => [
        'class' => [
          'checkbox',
        ],
      ],
    ];
    $activities_list[$activity_id]['weight'] = [
      '#type' => 'weight',
      '#title' => $this
        ->t('Weight'),
      '#title_display' => 'invisible',
      '#default_value' => $activity->weight,
      '#delta' => 500,
      // Classify the weight element for #tabledrag.
      '#attributes' => [
        'class' => [
          'activities-list-order-weight',
        ],
      ],
    ];
  }
}