0

I am new to drupal 8. I have created a form listing in admin.Now i want to add three or more links for operation(Edit/Delete/Suspend) in single TD columns. Can anyone let me know how to do this? I tried by concatenation two links but did not work. I am creating links as below.

$query = \Drupal::database()->select('vacations', 'v');
    $query->fields('v');
    $table_sort = $query->extend('Drupal\Core\Database\Query\TableSortExtender')->orderByHeader($header);
    $pager = $table_sort->extend('Drupal\Core\Database\Query\PagerSelectExtender')->limit(10);
    //$results = $query->execute()->fetchAll();
    $results = $pager->execute();
    // Initialize an empty array
    $output = array();
// Next, loop through the $results array
    foreach ($results as $key => $result) {
      $approve_link = Link::fromTextAndUrl(t('Approve'), Url::fromRoute('vacation_forms.operations', ['ID' => $result->id, 'action' => 'approve']))->toString();
      $reject_link = Link::fromTextAndUrl(t('Reject'), Url::fromRoute('vacation_forms.operations', ['ID' => $result->id, 'action' => 'reject']))->toString();
      $newLink = $approve_link; // this works
      //$newLink = $approve_link . "|" . $reject_link; //this does not work it shows plain html in table columns
      $output[$result->id] = [
        //'id' => $result->id,
        'employee_name' => $result->employee_name,
        'employee_email' => $result->employee_email,
        'vacation_start_date' => $result->vacation_start_date,
        'vacation_end_date' => $result->vacation_end_date,
        'leave_days' => $result->leave_days,
        'status' => $result->status,
        'operations' => $newLink,
      ];
    }
    $form['table'] = [
      '#type' => 'tableselect',
      '#header' => $header,
      '#options' => $output,
      '#empty' => t('No users found'),
    ];

I am using Drupal 8.

4
  • Welcome to Drupal Answers! So far you are just stating facts which aren't totally clear. You need to show all the code you are using; that is only a minimal part. We cannot say what you are doing wrong without seeing the code. Then, we aren't a write the code for me service, so a request like this is all my code; fill the holes with the missing code is not acceptable.
    – avpaderno
    Commented May 6, 2017 at 7:12
  • I only want to show to links "Approve | Reject". Approve and reject both will be href links. Below is the full function for your reference. Commented May 6, 2017 at 11:59
  • The above $output array is in for each loop after querying the DB. Two links one for approve and second for reject is required. Every admin listing has these links. But I am not able to show them in a single column. Remember I am using Drupal 8. Commented May 6, 2017 at 12:04
  • I have updated the question and put my full code.Please check reply. Commented May 6, 2017 at 12:11

1 Answer 1

1

After a long research i was able to acheive this as follows:

$approve_link = Link::fromTextAndUrl(t('Approve'), Url::fromRoute('vacation_forms.operations', ['ID' => $result->id, 'action' => 'approve']))->toString();
$reject_link = Link::fromTextAndUrl(t('Reject'), Url::fromRoute('vacation_forms.operations', ['ID' => $result->id, 'action' => 'reject']))->toString();
$mainLink = t('@linkApprove | @linkReject', array('@linkApprove' => $approve_link, '@linkReject' => $reject_link));

and then use $mainLink in foreach loop. Thanks for all your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.