You are here

function webform_preprocess_table in Webform 8.5

Same name and namespace in other branches
  1. 6.x includes/webform.theme.inc \webform_preprocess_table()

Implements hook_preprocess_table() for table templates.

File

includes/webform.theme.inc, line 338
Theme hooks, preprocessor, and suggestions.

Code

function webform_preprocess_table(&$variables) {

  // Add links to 'Translate' webform tab.
  if (\Drupal::routeMatch()
    ->getRouteName() === 'entity.webform.config_translation_overview') {

    /** @var \Drupal\webform\WebformInterface $webform */
    $webform = \Drupal::routeMatch()
      ->getParameter('webform');
    foreach ($variables['rows'] as &$row) {

      // Check first cell.
      if (!isset($row['cells'][0]['content']) || !is_array($row['cells'][0]['content']) || !isset($row['cells'][0]['content']['#markup'])) {
        continue;
      }

      // Check last cell edit link.
      if (!isset($row['cells'][1]['content']) || !is_array($row['cells'][1]['content']) || !isset($row['cells'][1]['content']['#links']) || !is_array($row['cells'][1]['content']['#links']) || !isset($row['cells'][1]['content']['#links']['edit'])) {
        continue;
      }

      // Get language from edit link.
      $route_parameters = $row['cells'][1]['content']['#links']['edit']['url']
        ->getRouteParameters();
      $langcode = isset($route_parameters['langcode']) ? $route_parameters['langcode'] : NULL;
      $language = \Drupal::languageManager()
        ->getLanguage($langcode);

      // Convert the first cell in the row to a link.
      $row['cells'][0]['content'] = [
        '#type' => 'link',
        '#url' => $webform
          ->toUrl('canonical', [
          'language' => $language,
        ]),
        '#title' => $row['cells'][0]['content'],
      ];
    }
  }
}