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

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

8
use Drupal\Component\Utility\String;
9
use Drupal\Core\Language\LanguageManager;
10 11 12 13 14 15

/**
 * Prepare a language code list for unused predefined languages.
 */
function language_admin_predefined_list() {
  $languages = language_list();
16
  $predefined = LanguageManager::getStandardLanguageList();
17 18 19 20 21 22 23 24 25 26
  foreach ($predefined as $key => $value) {
    if (isset($languages[$key])) {
      unset($predefined[$key]);
      continue;
    }
    $predefined[$key] = t($value[0]);
  }
  asort($predefined);
  return $predefined;
}
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

/**
 * 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();
43
    $title = '<h2>' . $form[$type]['#title'] . '</h2>';
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
    $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'));
    }

77
    $build = array(
78
      '#type' => 'table',
79 80 81
      '#header' => $header,
      '#rows' => $rows,
      '#attributes' => array('id' => "language-negotiation-methods-$type"),
82 83 84 85 86 87 88
      '#tabledrag' => array(
        array(
          'action' => 'order',
          'relationship' => 'sibling',
          'group' => "language-method-weight-$type",
        ),
      ),
89
    );
90

91 92
    $table  = drupal_render($form[$type]['configurable']);
    $table .= drupal_render($build);
93 94
    $table .= drupal_render_children($form[$type]);

95
    $output .= '<div class="form-item table-language-group table-' . $type . '-wrapper">' . $title . $description . $table . '</div>';
96 97 98 99 100 101
  }

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

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
/**
 * 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']);
118 119 120 121 122 123 124
    $links = array();
    $links['delete'] = array(
      'title' => t('Delete'),
      'href' => "admin/config/regional/language/detection/browser/delete/$key",
      'attributes' => array(
        'class' => array('image-style-link'),
      ),
125
    );
126 127 128 129 130 131 132 133
    $row[] = array(
      'data' => array(
        '#type' => 'operations',
        '#links' => $links,
      ),
    );

    $rows[] = $row;
134 135 136 137 138 139 140 141
  }

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

142 143 144 145 146 147 148
  $table = array(
    '#theme' => 'table',
    '#header' => $header,
    '#rows' => $rows,
    '#attributes' => array('id' => 'lang-neg-browser'),
  );
  $output = drupal_render($table);
149 150 151 152

  return $output;
}

153 154
/**
 * Returns the content language settings form.
155
 *
156 157
 * @deprecated in Drupal 8.x-dev, will be removed before Drupal 8.0.
 *   Use \Drupal\language\Controller\LanguageController::contentSettings().
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
 */
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) {
174
  $entity_types = \Drupal::entityManager()->getDefinitions();
175 176 177
  $labels = array();
  $default = array();

178 179 180
  foreach ($supported as $entity_type_id) {
    $labels[$entity_type_id] = $entity_types[$entity_type_id]->getLabel() ?: $entity_type_id;
    $default[$entity_type_id] = FALSE;
181 182

    // Check whether we have any custom setting.
183 184
    foreach (entity_get_bundles($entity_type_id) as $bundle => $bundle_info) {
      $conf = language_get_default_configuration($entity_type_id, $bundle);
185
      if (!empty($conf['language_show']) || $conf['langcode'] != 'site_default') {
186
        $default[$entity_type_id] = $entity_type_id;
187
      }
188
      $language_configuration[$entity_type_id][$bundle] = $conf;
189 190 191
    }
  }

192 193
  asort($labels);

194 195 196 197
  $path = drupal_get_path('module', 'language');
  $form = array(
    '#labels' => $labels,
    '#attached' => array(
198
      'css' => array($path . '/css/language.admin.css'),
199 200 201 202 203 204 205 206 207 208 209 210
    ),
  );

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

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

211 212
  foreach ($labels as $entity_type_id => $label) {
    $entity_type = $entity_types[$entity_type_id];
213

214
    $form['settings'][$entity_type_id] = array(
215
      '#title' => $label,
216
      '#type' => 'container',
217
      '#entity_type' => $entity_type_id,
218
      '#theme' => 'language_content_settings_table',
219
      '#bundle_label' => $entity_type->getBundleLabel() ?: $label,
220 221
      '#states' => array(
        'visible' => array(
222
          ':input[name="entity_types[' . $entity_type_id . ']"]' => array('checked' => TRUE),
223 224 225 226
        ),
      ),
    );

227 228
    foreach (entity_get_bundles($entity_type_id) as $bundle => $bundle_info) {
      $form['settings'][$entity_type_id][$bundle]['settings'] = array(
229
        '#type' => 'item',
230
        '#label' => $bundle_info['label'],
231 232 233
        'language' => array(
          '#type' => 'language_configuration',
          '#entity_information' => array(
234
            'entity_type' => $entity_type_id,
235 236
            'bundle' => $bundle,
          ),
237
          '#default_value' => $language_configuration[$entity_type_id][$bundle],
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
        ),
      );
    }
  }

  $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>',
278
            '#markup' => String::checkPlain($element[$bundle]['settings']['#label']),
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
          ),
          '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) {
  $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.'));
}
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342

/**
 * Helper function to disable the language switcher blocks.
 *
 * @param array $language_types
 *   Array containing all language types whose language switchers need to be
 *   disabled.
 */
function _language_disable_language_switcher(array $language_types) {
  $blocks = _block_rehash();
  foreach ($language_types as $language_type) {
    foreach ($blocks as $block) {
      if (strpos($block->id, 'language_switcher_' . substr($language_type, 9)) !== FALSE) {
        $block->delete();
      }
    }
  }
}