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

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

8
use Drupal\Component\Utility\SafeMarkup;
9
use Drupal\Core\Render\Element;
10
use Drupal\Core\Template\Attribute;
11
use Drupal\Core\Url;
12

13
/**
14
 * Prepares variables for language negotiation configuration form.
15
 *
16 17 18
 * Default template: language-content-configuration-form.html.twig.
 *
 * @param array $variables
19 20 21
 *   An associative array containing:
 *   - form: A render element representing the form.
 */
22 23 24
function template_preprocess_language_negotiation_configure_form(&$variables) {
  $form =& $variables['form'];
  $variables['language_types'] = array();
25 26 27

  foreach ($form['#language_types'] as $type) {
    $header = array(
28 29 30 31
      t('Detection method'),
      t('Description'),
      t('Enabled'),
      t('Weight'),
32 33 34 35
    );

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

39
    $table = array(
40
      '#type' => 'table',
41
      '#header' => $header,
42
      '#attributes' => array('id' => 'language-negotiation-methods-' . $type),
43 44 45 46
      '#tabledrag' => array(
        array(
          'action' => 'order',
          'relationship' => 'sibling',
47
          'group' => 'language-method-weight-' . $type,
48 49
        ),
      ),
50
    );
51

52 53
    foreach ($form[$type]['title'] as $id => $element) {
      // Do not take form control structures.
54
      if (is_array($element) && Element::child($id)) {
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
        $table[$id]['#attributes']['class'][] = 'draggable';
        $table[$id]['#weight'] = $element['#weight'];

        $table[$id]['title'] = array(
          '#prefix' => '<strong>',
          $form[$type]['title'][$id],
          '#suffix' => '</strong>',
        );
        $table[$id]['description'] = $form[$type]['description'][$id];
        $table[$id]['enabled'] = $form[$type]['enabled'][$id];
        $table[$id]['weight'] = $form[$type]['weight'][$id];
        if ($form[$type]['#show_operations']) {
          $table[$id]['operation'] = $form[$type]['operation'][$id];
        }
        // Unset to prevent rendering along with children.
        unset($form[$type]['title'][$id]);
        unset($form[$type]['description'][$id]);
        unset($form[$type]['enabled'][$id]);
        unset($form[$type]['weight'][$id]);
        unset($form[$type]['operation'][$id]);
      }
    }

    // Unset configurable to prevent rendering twice with children.
    $configurable = isset($form[$type]['configurable']) ? $form[$type]['configurable'] : NULL;
    unset($form[$type]['configurable']);
81

82 83 84 85 86 87 88
    $variables['language_types'][] = array(
      'type' => $type,
      'title' => $form[$type]['#title'],
      'description' => $form[$type]['#description'],
      'configurable' => $configurable,
      'table' => $table,
      'children' => $form[$type],
89
      'attributes' => new Attribute(),
90 91 92
    );
    // Prevent the type from rendering with the remaining form child elements.
    unset($form[$type]);
93 94
  }

95
  $variables['children'] = $form;
96 97
}

98 99 100 101 102 103 104 105 106 107 108 109
/**
 * 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();
110
  foreach (Element::children($form, TRUE) as $key) {
111 112 113
    $row = array();
    $row[] = drupal_render($form[$key]['browser_langcode']);
    $row[] = drupal_render($form[$key]['drupal_langcode']);
114 115 116
    $links = array();
    $links['delete'] = array(
      'title' => t('Delete'),
117
      'url' => Url::fromRoute('language.negotiation_browser_delete', ['browser_langcode' => $key]),
118
    );
119 120 121 122 123 124 125 126
    $row[] = array(
      'data' => array(
        '#type' => 'operations',
        '#links' => $links,
      ),
    );

    $rows[] = $row;
127 128 129 130
  }

  $header = array(
    t('Browser language code'),
131
    t('Site language'),
132 133 134
    t('Operations'),
  );

135
  $table = array(
136
    '#type' => 'table',
137 138
    '#header' => $header,
    '#rows' => $rows,
139 140
    '#empty' => t('No browser language mappings available.'),
    '#attributes' => array('id' => 'language-negotiation-browser'),
141 142
  );
  $output = drupal_render($table);
143 144 145 146

  return $output;
}

147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
/**
 * 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();
166
  foreach (Element::children($element) as $bundle) {
167 168 169 170 171 172
    $rows[$bundle] = array(
      'data' => array(
        array(
          'data' => array(
            '#prefix' => '<label>',
            '#suffix' => '</label>',
173
            '#markup' => SafeMarkup::checkPlain($element[$bundle]['settings']['#label']),
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
          ),
          '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,
190
    '#type' => 'table',
191 192 193 194 195 196 197 198 199 200 201
  );
}

/**
 * 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.
 *
202
 * @ingroup themeable
203 204 205 206
 */
function theme_language_content_settings_table($variables) {
  return '<h4>' . $variables['build']['#title'] . '</h4>' . drupal_render($variables['build']);
}