You are here

public function EntityTranslationDefaultHandler::entityFormLanguageWidget in Entity Translation 7

Overrides EntityTranslationHandlerInterface::entityFormLanguageWidget

See also

EntityTranslationHandlerInterface::entityFormLanguageWidget()

1 call to EntityTranslationDefaultHandler::entityFormLanguageWidget()
EntityTranslationDefaultHandler::entityForm in includes/translation.handler.inc

File

includes/translation.handler.inc, line 1463
Default translation handler for the translation module.

Class

EntityTranslationDefaultHandler
Class implementing the default entity translation behaviours.

Code

public function entityFormLanguageWidget(&$form, &$form_state) {
  if (entity_translation_enabled($this->entityType, $this->bundle)) {
    $is_new = $this
      ->isNewEntity();
    $is_translation = !empty($form_state['entity_translation']['is_translation']);
    $translations = $this
      ->getTranslations();
    $settings = entity_translation_settings($this->entityType, $this->bundle);
    $languages = entity_translation_languages($this->entityType, $this->entity);
    $options = count($translations->data) > 1 || !empty($settings['exclude_language_none']) ? array() : array(
      LANGUAGE_NONE => t('Language neutral'),
    );
    foreach ($languages as $langcode => $language) {

      // Disable languages for existing translations, so it is not possible to
      // switch this entity to some language which is already in the
      // translation set.
      if (!isset($translations->data[$langcode]) || empty($translations->data[$langcode]['source'])) {
        $options[$langcode] = t($language->name);
      }
    }
    $langcode = $is_new ? $this
      ->getDefaultLanguage() : $this
      ->getLanguage();
    $language_key = $this
      ->getLanguageKey();
    $form[$language_key] = array(
      '#type' => 'select',
      '#title' => t('Language'),
      '#default_value' => $langcode,
      '#options' => $options,
      '#access' => empty($settings['hide_language_selector']),
      '#disabled' => $is_translation || !$is_new && !empty($settings['lock_language']),
      '#multilingual' => TRUE,
    );
    if ($is_translation) {

      // @todo Consider supporting the ability to change translation language.
      $form[$language_key]['#title'] = t('Original language');
    }
  }

  // Prepend an empty form element to the form array so that we can update the
  // form language before any other form handler has been called.
  $form = array(
    'entity_translation_entity_form_language_update' => array(
      '#element_validate' => array(
        'entity_translation_entity_form_language_update',
      ),
      '#entity_type' => $this->entityType,
    ),
  ) + $form;
}