You are here

public function ParagraphsTableFormatter::datatablesOption in Paragraphs table 8

Datatable Options.

1 call to ParagraphsTableFormatter::datatablesOption()
ParagraphsTableFormatter::viewElements in src/Plugin/Field/FieldFormatter/ParagraphsTableFormatter.php
Builds a renderable array for a field value.

File

src/Plugin/Field/FieldFormatter/ParagraphsTableFormatter.php, line 887

Class

ParagraphsTableFormatter
Plugin implementation of the 'paragraphs_table_formatter' formatter.

Namespace

Drupal\paragraphs_table\Plugin\Field\FieldFormatter

Code

public function datatablesOption($header, $components, $langcode = 'en') {
  $datatable_options = [
    'bExpandable' => TRUE,
    'bInfo' => TRUE,
    'dom' => 'Bfrtip',
    "scrollX" => TRUE,
    'bStateSave' => FALSE,
    "ordering" => TRUE,
    'searching' => TRUE,
    'bMultiFilter' => FALSE,
    'bMultiFilter_position' => "header",
  ];
  foreach ($header as $field_name => $field_label) {
    $datatable_options['aoColumnHeaders'][] = $field_label;
    $column_options = [
      'name' => $field_name,
      'data' => $field_name,
      'orderable' => TRUE,
      'type' => 'html',
    ];

    // Attempt to autodetect the type of field
    // in order to handle sorting correctly.
    if (!empty($components[$field_name]) && in_array($components[$field_name]['type'], [
      'number_decimal',
      'number_integer',
      'number_float',
      'list_float',
      'list_integer',
    ])) {
      $column_options['type'] = 'html-num';
    }
    if (!empty($components[$field_name]) && in_array($components[$field_name]['type'], [
      'datetime',
      'date',
      'datestamp',
    ])) {
      $column_options['type'] = 'date-fr';
    }
    $datatable_options['columns'][] = $column_options;
  }
  $languages = [
    'af' => 'Afrikaans',
    'am' => 'Amharic',
    'ar' => 'Arabic',
    'az' => 'Azerbaijan',
    'be' => 'Belarusian',
    'bg' => 'Bulgarian',
    'ca' => 'Catalan',
    'cs' => 'Czech',
    'cy' => 'Welsh',
    'da' => 'Danish',
    'de' => 'German',
    'el' => 'Greek',
    'eo' => 'Esperanto',
    'es' => 'Spanish',
    'et' => 'Estonian',
    'eu' => 'Basque',
    'fa' => 'Persian',
    'fi' => 'Finnish',
    'fr' => 'French',
    'ga' => 'Irish',
    'gl' => 'Galician',
    'gu' => 'Gujarati',
    'he' => 'Hebrew',
    'hi' => 'Hindi',
    'hr' => 'Croatian',
    'hu' => 'Hungarian',
    'hy' => 'Armenian',
    'id' => 'Indonesian',
    'is' => 'Icelandic',
    'it' => 'Italian',
    'ja' => 'Japanese',
    'ka' => 'Georgian',
    'kk' => 'Kazakh',
    'km' => 'Khmer',
    'ko' => 'Korean',
    'ku' => 'Kurdish',
    'ky' => 'Kyrgyz',
    'lo' => 'Lao',
    'lt' => 'Lithuanian',
    'lv' => 'Latvian',
    'mk' => 'Macedonian',
    'ml' => 'Malay',
    'mn' => 'Mongolian',
    'ne' => 'Nepali',
    'nl' => 'Dutch',
    'nb' => 'Norwegian-Bokmal',
    'nn' => 'Norwegian-Nynorsk',
    'pa' => 'Punjabi',
    'pl' => 'Polish',
    'pt' => 'Portuguese',
    'ro' => 'Romanian',
    'ru' => 'Russian',
    'si' => 'Sinhala',
    'sk' => 'Slovak',
    'sl' => 'Slovenian',
    'sq' => 'Albanian',
    'sr' => 'Serbian',
    'sv' => 'Swedish',
    'sw' => 'Swahili',
    'ta' => 'Tamil',
    'te' => 'telugu',
    'th' => 'Thai',
    'tr' => 'Turkish',
    'uk' => 'Ukrainian',
    'ur' => 'Urdu',
    'vi' => 'Vietnamese',
    'fil' => 'Filipino',
    'zh-hans' => 'Chinese',
    'zh-hant' => 'Chinese',
  ];
  if (!empty($languages[$langcode])) {
    $cdn_lang = '//cdn.datatables.net/plug-ins/';
    $version = '1.10.21';
    $language_url = $cdn_lang . $version . '/i18n/' . $languages[$langcode] . '.json';
    $datatable_options['language']['url'] = $language_url;
  }
  return $datatable_options;
}