language.admin.inc 30.6 KB
Newer Older
1 2 3 4 5 6 7
<?php

/**
 * @file
 * Administration functions for language.module.
 */

8
use Drupal\Core\Language\Language;
9
use Drupal\Core\Language\LanguageManager;
10 11
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

12 13 14 15 16
/**
 * User interface for the language overview screen.
 */
function language_admin_overview_form($form, &$form_state) {
  drupal_static_reset('language_list');
17
  $languages = language_list();
18 19 20 21 22
  $default = language_default();

  $form['languages'] = array(
    '#languages' => $languages,
    '#language_default' => $default,
23
    '#type' => 'table',
24 25 26 27 28
    '#header' => array(
      t('Name'),
      t('Weight'),
      t('Operations'),
    ),
29 30 31
    '#tabledrag' => array(
      array('order', 'sibling', 'language-order-weight'),
    ),
32 33 34
  );

  foreach ($languages as $langcode => $language) {
35
    $form['languages'][$langcode]['#attributes']['class'][] = 'draggable';
36
    $form['languages'][$langcode]['name'] = array(
37
      '#markup' => check_plain($language->name),
38 39 40 41 42 43 44 45 46
    );
    $form['languages'][$langcode]['weight'] = array(
      '#type' => 'weight',
      '#title' => t('Weight for @title', array('@title' => $language->name)),
      '#title_display' => 'invisible',
      '#default_value' => $language->weight,
      '#attributes' => array(
        'class' => array('language-order-weight'),
      ),
47
      '#delta' => 30,
48
    );
49
    $links = array();
50 51 52 53 54 55 56 57
    $links['edit'] = array(
      'title' => t('edit'),
      'href' => 'admin/config/regional/language/edit/' . $langcode,
    );
    if ($langcode != $default->langcode) {
      $links['delete'] = array(
        'title' => t('delete'),
        'href' => 'admin/config/regional/language/delete/' . $langcode,
58 59
      );
    }
60 61 62 63 64
    $form['languages'][$langcode]['operations'] = array(
      '#type' => 'operations',
      '#links' => $links,
      '#weight' => 100,
    );
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
  }

  $form['actions'] = array('#type' => 'actions');
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save configuration'),
  );

  return $form;
}

/**
 * Process language overview form submissions, updating existing languages.
 */
function language_admin_overview_form_submit($form, &$form_state) {
80
  $languages = language_list();
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112

  foreach ($languages as $langcode => $language) {
    $language->weight = $form_state['values']['languages'][$langcode]['weight'];
    language_save($language);
  }

  drupal_set_message(t('Configuration saved.'));
}

/**
 * User interface for the language addition screen.
 */
function language_admin_add_form($form, &$form_state) {
  $predefined_languages = language_admin_predefined_list();
  $predefined_languages['custom'] = t('Custom language...');
  $predefined_default = !empty($form_state['values']['predefined_langcode']) ? $form_state['values']['predefined_langcode'] : key($predefined_languages);
  $form['predefined_langcode'] = array(
    '#type' => 'select',
    '#title' => t('Language name'),
    '#default_value' => $predefined_default,
    '#options' => $predefined_languages,
  );
  $form['predefined_submit'] = array(
    '#type' => 'submit',
    '#value' => t('Add language'),
    '#limit_validation_errors' => array(array('predefined_langcode'), array('predefined_submit')),
    '#states' => array(
      'invisible' => array(
        'select#edit-predefined-langcode' => array('value' => 'custom'),
      ),
    ),
    '#validate' => array('language_admin_add_predefined_form_validate'),
113
    '#submit' => array('language_admin_add_form_submit'),
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
  );

  $form['custom_language'] = array(
    '#type' => 'container',
    '#states' => array(
      'visible' => array(
        'select#edit-predefined-langcode' => array('value' => 'custom'),
      ),
    ),
  );
  _language_admin_common_controls($form['custom_language']);
  $form['custom_language']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Add custom language'),
    '#validate' => array('language_admin_add_custom_form_validate'),
129
    '#submit' => array('language_admin_add_form_submit'),
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
  );

  return $form;
}

/**
 * Editing screen for a particular language.
 *
 * @param $langcode
 *   Language code of the language to edit.
 */
function language_admin_edit_form($form, &$form_state, $language) {
  _language_admin_common_controls($form, $language);
  $form['actions'] = array('#type' => 'actions');
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save language')
  );
  $form['#submit'][] = 'language_admin_edit_form_submit';
  $form['#validate'][] = 'language_admin_edit_form_validate';
  return $form;
}

/**
 * Common elements of the language addition and editing form.
 *
 * @param $form
 *   A parent form item (or empty array) to add items below.
 * @param $language
 *   Language object to edit.
 */
function _language_admin_common_controls(&$form, $language = NULL) {
  if (!is_object($language)) {
163
    $language = new Language(array('langcode' => NULL, 'name' => NULL,));
164
  }
165
  if (isset($language->langcode)) {
166 167 168
    $form['langcode_view'] = array(
      '#type' => 'item',
      '#title' => t('Language code'),
169
      '#markup' => $language->langcode
170 171 172
    );
    $form['langcode'] = array(
      '#type' => 'value',
173
      '#value' => $language->langcode
174 175 176 177 178 179 180 181
    );
  }
  else {
    $form['langcode'] = array(
      '#type' => 'textfield',
      '#title' => t('Language code'),
      '#maxlength' => 12,
      '#required' => TRUE,
182 183
      '#default_value' => '',
      '#disabled' => FALSE,
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
      '#description' => t('Use language codes as <a href="@w3ctags">defined by the W3C</a> for interoperability. <em>Examples: "en", "en-gb" and "zh-hant".</em>', array('@w3ctags' => 'http://www.w3.org/International/articles/language-tags/')),
    );
  }
  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Language name'),
    '#maxlength' => 64,
    '#default_value' => @$language->name,
    '#required' => TRUE,
  );
  $form['direction'] = array(
    '#type' => 'radios',
    '#title' => t('Direction'),
    '#required' => TRUE,
    '#description' => t('Direction that text in this language is presented.'),
    '#default_value' => @$language->direction,
200
    '#options' => array(Language::DIRECTION_LTR => t('Left to right'), Language::DIRECTION_RTL => t('Right to left')),
201 202 203 204 205 206 207 208 209 210 211 212 213
  );
  return $form;
}

/**
 * Element specific validator for the Add language button.
 */
function language_admin_add_predefined_form_validate($form, &$form_state) {
  $langcode = $form_state['values']['predefined_langcode'];
  if ($langcode == 'custom') {
    form_set_error('predefined_langcode', t('Fill in the language details and save the language with <em>Add custom language</em>.'));
  }
  else {
214 215
    if ($language = language_load($langcode)) {
      form_set_error('predefined_langcode', t('The language %language (%langcode) already exists.', array('%language' => $language->name, '%langcode' => $langcode)));
216 217 218 219 220 221 222 223 224 225 226
    }
  }
}

/**
 * Validate the language addition form on custom language button.
 */
function language_admin_add_custom_form_validate($form, &$form_state) {
  if ($form_state['values']['predefined_langcode'] == 'custom') {
    $langcode = $form_state['values']['langcode'];
    // Reuse the editing form validation routine if we add a custom language.
227
    language_admin_edit_form_validate($form['custom_language'], $form_state);
228

229 230
    if ($language = language_load($langcode)) {
      form_set_error('langcode', t('The language %language (%langcode) already exists.', array('%language' => $language->name, '%langcode' => $langcode)));
231 232 233 234 235 236 237 238
    }
  }
  else {
    form_set_error('predefined_langcode', t('Use the <em>Add language</em> button to save a predefined language.'));
  }
}

/**
239
 * Process the custom and predefined language addition form submission.
240
 */
241
function language_admin_add_form_submit($form, &$form_state) {
242
  $langcode = $form_state['values']['predefined_langcode'];
243 244 245
  if ($langcode == 'custom') {
    $langcode = $form_state['values']['langcode'];
    // Custom language form.
246
    $language = new Language(array(
247 248 249
      'langcode' => $langcode,
      'name' => $form_state['values']['name'],
      'direction' => $form_state['values']['direction'],
250
    ));
251 252
  }
  else {
253
    $language = new Language(array('langcode' => $langcode));
254 255 256 257 258 259 260
  }
  // Save the language and inform the user that it happened.
  $language = language_save($language);
  drupal_set_message(t('The language %language has been created and can now be used.', array('%language' => $language->name)));

  // Tell the user they have the option to add a language switcher block
  // to their theme so they can switch between the languages.
261
  drupal_set_message(t('Use one of the language switcher blocks to allow site visitors to switch between languages. You can enable these blocks on the <a href="@block-admin">block administration page</a>.', array('@block-admin' => url('admin/structure/block'))));
262

263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
  $form_state['redirect'] = 'admin/config/regional/language';
}

/**
 * Validate the language editing form. Reused for custom language addition too.
 */
function language_admin_edit_form_validate($form, &$form_state) {
  // Ensure sane field values for langcode and name.
  if (!isset($form['langcode_view']) && preg_match('@[^a-zA-Z_-]@', $form_state['values']['langcode'])) {
    form_set_error('langcode', t('%field may only contain characters a-z, underscores, or hyphens.', array('%field' => $form['langcode']['#title'])));
  }
  if ($form_state['values']['name'] != check_plain($form_state['values']['name'])) {
    form_set_error('name', t('%field cannot contain any markup.', array('%field' => $form['name']['#title'])));
  }
}

/**
 * Process the language editing form submission.
 */
function language_admin_edit_form_submit($form, &$form_state) {
283
  // Prepare a language object for saving.
284 285 286 287 288 289 290 291 292 293 294 295 296
  $languages = language_list();
  $langcode = $form_state['values']['langcode'];
  $language = $languages[$langcode];
  $language->name = $form_state['values']['name'];
  $language->direction = $form_state['values']['direction'];
  language_save($language);
  $form_state['redirect'] = 'admin/config/regional/language';
}

/**
 * User interface for the language deletion confirmation screen.
 */
function language_admin_delete_form($form, &$form_state, $language) {
297
  $langcode = $language->langcode;
298

299
  if (language_default()->langcode == $langcode) {
300 301 302 303
    drupal_set_message(t('The default language cannot be deleted.'));
    drupal_goto('admin/config/regional/language');
  }

304
  // For other languages, warn the user that data loss is ahead.
305 306 307
  $languages = language_list();

  if (!isset($languages[$langcode])) {
308
    throw new NotFoundHttpException();
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
  }
  else {
    $form['langcode'] = array('#type' => 'value', '#value' => $langcode);
    return confirm_form($form, t('Are you sure you want to delete the language %language?', array('%language' => $languages[$langcode]->name)), 'admin/config/regional/language', t('Deleting a language will remove all interface translations associated with it, and posts in this language will be set to be language neutral. This action cannot be undone.'), t('Delete'), t('Cancel'));
  }
}

/**
 * Process language deletion submissions.
 */
function language_admin_delete_form_submit($form, &$form_state) {
  $langcode = $form_state['values']['langcode'];
  $languages = language_list();
  $language = $languages[$langcode];

  $success = language_delete($langcode);

  if ($success) {
327
    $t_args = array('%language' => $language->name, '%langcode' => $language->langcode);
328 329 330 331 332 333 334 335 336 337 338
    drupal_set_message(t('The %language (%langcode) language has been removed.', $t_args));
  }

  $form_state['redirect'] = 'admin/config/regional/language';
}

/**
 * Prepare a language code list for unused predefined languages.
 */
function language_admin_predefined_list() {
  $languages = language_list();
339
  $predefined = LanguageManager::getStandardLanguageList();
340 341 342 343 344 345 346 347 348 349
  foreach ($predefined as $key => $value) {
    if (isset($languages[$key])) {
      unset($predefined[$key]);
      continue;
    }
    $predefined[$key] = t($value[0]);
  }
  asort($predefined);
  return $predefined;
}
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430

/**
 * Builds the configuration form for language negotiation.
 */
function language_negotiation_configure_form() {
  language_negotiation_include();

  $form = array(
    '#submit' => array('language_negotiation_configure_form_submit'),
    '#theme' => 'language_negotiation_configure_form',
    '#language_types' => language_types_get_configurable(FALSE),
    '#language_types_info' => language_types_info(),
    '#language_negotiation_info' => language_negotiation_info(),
  );

  foreach ($form['#language_types'] as $type) {
    language_negotiation_configure_form_table($form, $type);
  }

  $form['actions'] = array('#type' => 'actions');
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save settings'),
  );

  return $form;
}

/**
 * Builds a language negotiation method configuration table.
 */
function language_negotiation_configure_form_table(&$form, $type) {
  $info = $form['#language_types_info'][$type];

  $table_form = array(
    '#title' => t('@type language detection', array('@type' => $info['name'])),
    '#tree' => TRUE,
    '#description' => $info['description'],
    '#language_negotiation_info' => array(),
    '#show_operations' => FALSE,
    'weight' => array('#tree' => TRUE),
  );

  $negotiation_info = $form['#language_negotiation_info'];
  $enabled_methods = variable_get("language_negotiation_$type", array());
  $methods_weight = variable_get("language_negotiation_methods_weight_$type", array());

  // Add missing data to the methods lists.
  foreach ($negotiation_info as $method_id => $method) {
    if (!isset($methods_weight[$method_id])) {
      $methods_weight[$method_id] = isset($method['weight']) ? $method['weight'] : 0;
    }
  }

  // Order methods list by weight.
  asort($methods_weight);

  foreach ($methods_weight as $method_id => $weight) {
    // A language method might be no more available if the defining module has
    // been disabled after the last configuration saving.
    if (!isset($negotiation_info[$method_id])) {
      continue;
    }

    $enabled = isset($enabled_methods[$method_id]);
    $method = $negotiation_info[$method_id];

    // List the method only if the current type is defined in its 'types' key.
    // If it is not defined default to all the configurable language types.
    $types = array_flip(isset($method['types']) ? $method['types'] : $form['#language_types']);

    if (isset($types[$type])) {
      $table_form['#language_negotiation_info'][$method_id] = $method;
      $method_name = check_plain($method['name']);

      $table_form['weight'][$method_id] = array(
        '#type' => 'weight',
        '#title' => t('Weight for !title language detection method', array('!title' => drupal_strtolower($method_name))),
        '#title_display' => 'invisible',
        '#default_value' => $weight,
        '#attributes' => array('class' => array("language-method-weight-$type")),
431
        '#delta' => 20,
432 433 434 435 436 437 438 439 440 441
      );

      $table_form['title'][$method_id] = array('#markup' => $method_name);

      $table_form['enabled'][$method_id] = array(
        '#type' => 'checkbox',
        '#title' => t('Enable !title language detection method', array('!title' => drupal_strtolower($method_name))),
        '#title_display' => 'invisible',
        '#default_value' => $enabled,
      );
442
      if ($method_id === LANGUAGE_NEGOTIATION_SELECTED) {
443 444 445 446 447 448 449 450
        $table_form['enabled'][$method_id]['#default_value'] = TRUE;
        $table_form['enabled'][$method_id]['#attributes'] = array('disabled' => 'disabled');
      }

      $table_form['description'][$method_id] = array('#markup' => filter_xss_admin($method['description']));

      $config_op = array();
      if (isset($method['config'])) {
451 452 453 454
        $config_op['configure'] = array(
          'title' => t('Configure'),
          'href' => $method['config'],
        );
455 456 457
        // If there is at least one operation enabled show the operation column.
        $table_form['#show_operations'] = TRUE;
      }
458 459 460 461
      $table_form['operation'][$method_id] = array(
       '#type' => 'operations',
       '#links' => $config_op,
      );
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
    }
  }

  $form[$type] = $table_form;
}

/**
 * Returns HTML for the language negotiation configuration form.
 *
 * @param $variables
 *   An associative array containing:
 *   - form: A render element representing the form.
 *
 * @ingroup themeable
 */
function theme_language_negotiation_configure_form($variables) {
  $form = $variables['form'];
  $output = '';

  foreach ($form['#language_types'] as $type) {
    $rows = array();
483
    $title = '<h4 class="label">' . $form[$type]['#title'] . '</h4>';
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
    $description = '<div class="description">' . $form[$type]['#description'] . '</div>';

    foreach ($form[$type]['title'] as $id => $element) {
      // Do not take form control structures.
      if (is_array($element) && element_child($id)) {
        $row = array(
          'data' => array(
            '<strong>' . drupal_render($form[$type]['title'][$id]) . '</strong>',
            drupal_render($form[$type]['description'][$id]),
            drupal_render($form[$type]['enabled'][$id]),
            drupal_render($form[$type]['weight'][$id]),
          ),
          'class' => array('draggable'),
        );
        if ($form[$type]['#show_operations']) {
          $row['data'][] = drupal_render($form[$type]['operation'][$id]);
        }
        $rows[] = $row;
      }
    }

    $header = array(
      array('data' => t('Detection method')),
      array('data' => t('Description')),
      array('data' => t('Enabled')),
      array('data' => t('Weight')),
    );

    // If there is at least one operation enabled show the operation column.
    if ($form[$type]['#show_operations']) {
      $header[] = array('data' => t('Operations'));
    }

517 518 519 520 521
    $build = array(
      '#theme' => 'table',
      '#header' => $header,
      '#rows' => $rows,
      '#attributes' => array('id' => "language-negotiation-methods-$type"),
522
    );
523
    $table  = drupal_render($build);
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
    $table .= drupal_render_children($form[$type]);

    drupal_add_tabledrag("language-negotiation-methods-$type", 'order', 'sibling', "language-method-weight-$type");

    $output .= '<div class="form-item">' . $title . $description . $table . '</div>';
  }

  $output .= drupal_render_children($form);
  return $output;
}

/**
 * Submit handler for language negotiation settings.
 */
function language_negotiation_configure_form_submit($form, &$form_state) {
  $configurable_types = $form['#language_types'];

  foreach ($configurable_types as $type) {
    $method_weights = array();
    $enabled_methods = $form_state['values'][$type]['enabled'];
544
    $enabled_methods[LANGUAGE_NEGOTIATION_SELECTED] = TRUE;
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
    $method_weights_input = $form_state['values'][$type]['weight'];

    foreach ($method_weights_input as $method_id => $weight) {
      if ($enabled_methods[$method_id]) {
        $method_weights[$method_id] = $weight;
      }
    }

    language_negotiation_set($type, $method_weights);
    variable_set("language_negotiation_methods_weight_$type", $method_weights_input);
  }

  // Update non-configurable language types and the related language negotiation
  // configuration.
  language_types_set();

  $form_state['redirect'] = 'admin/config/regional/language/detection';
  drupal_set_message(t('Language negotiation configuration saved.'));
}

565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
/**
 * Builds the browser language negotiation method configuration form.
 */
function language_negotiation_configure_browser_form($form, &$form_state) {
  $form = array();

  // Initialize a language list to the ones available, including English.
  $languages = language_list();

  $existing_languages = array();
  foreach ($languages as $langcode => $language) {
    $existing_languages[$langcode] = $language->name;
  }

  // If we have no languages available, present the list of predefined languages
  // only. If we do have already added languages, set up two option groups with
  // the list of existing and then predefined languages.
  if (empty($existing_languages)) {
    $language_options = language_admin_predefined_list();
    $default = key($language_options);
  }
  else {
    $default = key($existing_languages);
    $language_options = array(
      t('Existing languages') => $existing_languages,
      t('Languages not yet added') => language_admin_predefined_list()
    );
  }

  $form['mappings'] = array(
    '#tree' => TRUE,
    '#theme' => 'language_negotiation_configure_browser_form_table',
  );

  $mappings = language_get_browser_drupal_langcode_mappings();
  foreach ($mappings as $browser_langcode => $drupal_langcode) {
    $form['mappings'][$browser_langcode] = array(
      'browser_langcode' => array(
        '#type' => 'textfield',
        '#default_value' => $browser_langcode,
        '#size' => 20,
        '#required' => TRUE,
      ),
      'drupal_langcode' => array(
        '#type' => 'select',
        '#options' => $language_options,
        '#default_value' => $drupal_langcode,
        '#required' => TRUE,
      ),
    );
  }

  // Add empty row.
  $form['new_mapping'] = array(
619
    '#type' => 'details',
620
    '#title' => t('Add a new mapping'),
621
    '#collapsed' => TRUE,
622 623 624 625 626 627 628 629 630 631 632
    '#tree' => TRUE,
  );
  $form['new_mapping']['browser_langcode'] = array(
    '#type' => 'textfield',
    '#title' => t('Browser language code'),
    '#description' => t('Use language codes as <a href="@w3ctags">defined by the W3C</a> for interoperability. <em>Examples: "en", "en-gb" and "zh-hant".</em>', array('@w3ctags' => 'http://www.w3.org/International/articles/language-tags/')),
    '#default_value' => '',
    '#size' => 20,
  );
  $form['new_mapping']['drupal_langcode'] = array(
    '#type' => 'select',
633
    '#title' => t('Drupal language'),
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
    '#options' => $language_options,
    '#default_value' => '',
  );

  $form['actions']['#type'] = 'actions';
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save configuration'),
  );

  return $form;
}

/**
 * Theme browser configuration form as table.
 *
 * @param $variables
 *   An associative array containing:
 *   - form: A render element representing the form.
 *
 * @ingroup themeable
 */
function theme_language_negotiation_configure_browser_form_table($variables) {
  $form = $variables['form'];
  $rows = array();
  foreach (element_children($form, TRUE) as $key) {
    $row = array();
    $row[] = drupal_render($form[$key]['browser_langcode']);
    $row[] = drupal_render($form[$key]['drupal_langcode']);
663 664 665 666 667 668 669
    $links = array();
    $links['delete'] = array(
      'title' => t('Delete'),
      'href' => "admin/config/regional/language/detection/browser/delete/$key",
      'attributes' => array(
        'class' => array('image-style-link'),
      ),
670
    );
671 672 673 674 675 676 677 678
    $row[] = array(
      'data' => array(
        '#type' => 'operations',
        '#links' => $links,
      ),
    );

    $rows[] = $row;
679 680 681 682 683 684 685 686
  }

  $header = array(
    t('Browser language code'),
    t('Drupal language'),
    t('Operations'),
  );

687 688 689 690 691 692 693
  $table = array(
    '#theme' => 'table',
    '#header' => $header,
    '#rows' => $rows,
    '#attributes' => array('id' => 'lang-neg-browser'),
  );
  $output = drupal_render($table);
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771

  return $output;
}

/**
 * Browser language negotiation form validation.
 */
function language_negotiation_configure_browser_form_validate($form, &$form_state) {
  // Array to check if all browser language codes are unique.
  $unique_values = array();

  // Check all mappings.
  $mappings = array();
  if (isset($form_state['values']['mappings'])) {
    $mappings = $form_state['values']['mappings'];
    foreach ($mappings as $key => $data) {
      // Make sure browser_langcode is unique.
      if (array_key_exists($data['browser_langcode'], $unique_values)) {
        form_set_error('mappings][' . $key . '][browser_langcode', t('Browser language codes must be unique.'));
      }
      elseif (preg_match('/[^a-z\-]/', $data['browser_langcode'])) {
        form_set_error('mappings][' . $key . '][browser_langcode', t('Browser language codes can only contain lowercase letters and a hyphen(-).'));
      }
      $unique_values[$data['browser_langcode']] = $data['drupal_langcode'];
    }
  }

  // Check new mapping.
  $data = $form_state['values']['new_mapping'];
  if (!empty($data['browser_langcode'])) {
    // Make sure browser_langcode is unique.
    if (array_key_exists($data['browser_langcode'], $unique_values)) {
      form_set_error('mappings][' . $key . '][browser_langcode', t('Browser language codes must be unique.'));
    }
    elseif (preg_match('/[^a-z\-]/', $data['browser_langcode'])) {
      form_set_error('mappings][' . $key . '][browser_langcode', t('Browser language codes can only contain lowercase letters and a hyphen(-).'));
    }
    $unique_values[$data['browser_langcode']] = $data['drupal_langcode'];
  }

  $form_state['mappings'] = $unique_values;
}

/**
 * Browser language negotiation form submit.
 */
function language_negotiation_configure_browser_form_submit($form, &$form_state) {
  $mappings = $form_state['mappings'];
  if (!empty($mappings)) {
    language_set_browser_drupal_langcode_mappings($mappings);
  }
  $form_state['redirect'] = 'admin/config/regional/language/detection';
}

/**
 * Form for deleting a browser language negotiation mapping.
 */
function language_negotiation_configure_browser_delete_form($form, &$form_state, $browser_langcode) {
  $form_state['browser_langcode'] = $browser_langcode;
  $question = t('Are you sure you want to delete %browser_langcode?', array(
    '%browser_langcode' => $browser_langcode,
  ));
  $path = 'admin/config/regional/language/detection/browser';
  return confirm_form($form, $question, $path, '');
}

/**
 * Form submit handler to delete a browser language negotiation mapping.
 */
function language_negotiation_configure_browser_delete_form_submit($form, &$form_state) {
  $browser_langcode = $form_state['browser_langcode'];
  $mappings = language_get_browser_drupal_langcode_mappings();
  if (array_key_exists($browser_langcode, $mappings)) {
    unset($mappings[$browser_langcode]);
    language_set_browser_drupal_langcode_mappings($mappings);
  }
  $form_state['redirect'] = 'admin/config/regional/language/detection/browser';
}
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799

/**
 * Returns the content language settings form.
 */
function language_content_settings_page() {
  return drupal_get_form('language_content_settings_form', language_entity_supported());
}

/**
 * Form constructor for the content language settings form.
 *
 * @param array $supported
 *   Entity types with language support.
 *
 * @see language_content_settings_form_submit()
 *
 * @ingroup forms
 */
function language_content_settings_form(array $form, array $form_state, array $supported) {
  $entity_info = entity_get_info();
  $labels = array();
  $default = array();

  foreach ($supported as $entity_type) {
    $labels[$entity_type] = isset($entity_info[$entity_type]['label']) ? $entity_info[$entity_type]['label'] : $entity_type;
    $default[$entity_type] = FALSE;

    // Check whether we have any custom setting.
800
    foreach (entity_get_bundles($entity_type) as $bundle => $bundle_info) {
801
      $conf = language_get_default_configuration($entity_type, $bundle);
802
      if (!empty($conf['language_show']) || $conf['langcode'] != 'site_default') {
803 804 805 806 807 808
        $default[$entity_type] = $entity_type;
      }
      $language_configuration[$entity_type][$bundle] = $conf;
    }
  }

809 810
  asort($labels);

811 812 813 814
  $path = drupal_get_path('module', 'language');
  $form = array(
    '#labels' => $labels,
    '#attached' => array(
815
      'css' => array($path . '/css/language.admin.css'),
816 817 818 819 820 821 822 823 824 825 826 827
    ),
  );

  $form['entity_types'] = array(
    '#title' => t('Custom language settings'),
    '#type' => 'checkboxes',
    '#options' => $labels,
    '#default_value' => $default,
  );

  $form['settings'] = array('#tree' => TRUE);

828
  foreach ($labels as $entity_type => $label) {
829 830 831
    $info = $entity_info[$entity_type];

    $form['settings'][$entity_type] = array(
832
      '#title' => $label,
833
      '#type' => 'container',
834
      '#entity_type' => $entity_type,
835
      '#theme' => 'language_content_settings_table',
836
      '#bundle_label' => isset($info['bundle_label']) ? $info['bundle_label'] : $label,
837 838 839 840 841 842 843
      '#states' => array(
        'visible' => array(
          ':input[name="entity_types[' . $entity_type . ']"]' => array('checked' => TRUE),
        ),
      ),
    );

844
    foreach (entity_get_bundles($entity_type) as $bundle => $bundle_info) {
845 846
      $form['settings'][$entity_type][$bundle]['settings'] = array(
        '#type' => 'item',
847
        '#label' => $bundle_info['label'],
848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942
        'language' => array(
          '#type' => 'language_configuration',
          '#entity_information' => array(
            'entity_type' => $entity_type,
            'bundle' => $bundle,
          ),
          '#default_value' => $language_configuration[$entity_type][$bundle],
        ),
      );
    }
  }

  $form['actions'] = array('#type' => 'actions');
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );

  return $form;
}

/**
 * Implements hook_preprocess_HOOK() for theme_language_content_settings_table().
 */
function template_preprocess_language_content_settings_table(&$variables) {
  // Add a render element representing the bundle language settings table.
  $element = $variables['element'];

  $header = array(
    array(
      'data' => $element['#bundle_label'],
      'class' => array('bundle'),
    ),
    array(
      'data' => t('Configuration'),
      'class' => array('operations'),
    ),
  );

  $rows = array();
  foreach (element_children($element) as $bundle) {
    $rows[$bundle] = array(
      'data' => array(
        array(
          'data' => array(
            '#prefix' => '<label>',
            '#suffix' => '</label>',
            '#markup' => check_plain($element[$bundle]['settings']['#label']),
          ),
          'class' => array('bundle'),
        ),
        array(
          'data' => $element[$bundle]['settings'],
          'class' => array('operations'),
        ),
      ),
      'class' => array('bundle-settings'),
    );
  }

  $variables['build'] = array(
    '#title' => $element['#title'],
    '#header' => $header,
    '#rows' => $rows,
    '#theme' => 'table',
  );
}

/**
 * Returns HTML for an administration settings table.
 *
 * @param array $variables
 *   An associative array containing:
 *   - build: A render element representing a table of bundle content language
 *     settings for a particular entity type.
 *
 * @ingroup themable
 */
function theme_language_content_settings_table($variables) {
  return '<h4>' . $variables['build']['#title'] . '</h4>' . drupal_render($variables['build']);
}

/**
 * Form submission handler for language_content_settings_form().
 */
function language_content_settings_form_submit(array $form, array &$form_state) {
  $entity_types = $form_state['values']['entity_types'];
  $settings = &$form_state['values']['settings'];
  foreach ($settings as $entity_type => $entity_settings) {
    foreach ($entity_settings as $bundle => $bundle_settings) {
      language_save_default_configuration($entity_type, $bundle, $bundle_settings['settings']['language']);
    }
  }
  drupal_set_message(t('Settings successfully updated.'));
}