public function LanguageCombinationWidget::formElement

File

translators/tmgmt_local/skills/src/Plugin/Field/FieldWidget/LanguageCombinationWidget.php, line 26

Class

LanguageCombinationWidget
Plugin implementation of the 'tmgmt_language_combination_default' widget.

Namespace

Drupal\tmgmt_language_combination\Plugin\Field\FieldWidget

Code

public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
  if ($form_state
    ->get('list_all_languages')) {
    $languages_options = tmgmt_language_combination_languages_predefined_list();
  }
  else {
    $languages_options = array();
    foreach (\Drupal::languageManager()
      ->getLanguages() as $code => $language) {
      $languages_options[$code] = $language
        ->getName();
    }
  }
  $options = array(
    '_none' => t('- None -'),
  ) + $languages_options;
  $element['language_from'] = array(
    '#type' => 'select',
    '#title' => t('From'),
    '#options' => $options,
    '#default_value' => isset($items[$delta]) ? $items[$delta]->language_from : '',
    '#attributes' => array(
      'class' => array(
        'from-language',
      ),
    ),
  );
  $element['language_to'] = array(
    '#type' => 'select',
    '#title' => t('To'),
    '#options' => $options,
    '#default_value' => isset($items[$delta]) ? $items[$delta]->language_to : '',
    '#attributes' => array(
      'class' => array(
        'to-language',
      ),
    ),
  );
  return $element;
}